1
votes

I have a php script which displays all photos from folder dir and I need to include an html tag that applies given data-parameters. the php code is:

?php

$directory = "images/*/";


$images = glob("" . $directory . "*.png" );

$imgs = '';

foreach($images as $image){ $imgs[] = "$image"; }

shuffle($imgs);


$imgs = array_slice($imgs, 0, 20);


foreach ($imgs as $img) {
   echo "<img src='$img' /> ";
  }

?>

and I need to put this in html tag as img src:

<img src="" title="Ring" data-parameters='{"zChangeable": true, "x": 215, "y": 200, "colors": "#000000", "removable": true, "draggable": true, "rotatable": true, "resizable": true, "price": 10, "boundingBox": "Base", "autoCenter": true}' />

I tried instead of echo " create new var with value $img and use but that gives me me only one image

2
Are you sure that glob returns values? Have you tested it?fusion3k
yes it works, but i dont know how to include it in that html tag with data-parameters. If someone has beater solution then my php code im glad to use it becouse im new in php. thanx in advancesd23
Sure it works? Have you tested it? ( print_r( $images ); die(); after glob line ) Because your code to output img is correct, so I bet that the problem is in the glob() command...fusion3k
i test it several time and if i put that code in exemple.php and include it in index.php with <?php include exemple.php?> it work just fine but dont have data-parameters='{"zChangeable": true, "x": 215, "y": 200, "colors": "#000000", "removable": true, "draggable": true, "rotatable": true, "resizable": true, "price": 10, "boundingBox": "Base", "autoCenter": true}' />sd23
For semantics sake! This will not fix the problem but try declaring $imgs = array(); rather than declaring it as a string. If its gonna be an array then its declaration should show that. :)Simon Willan

2 Answers

1
votes

I find solution :)

foreach ($imgs as $img) {

    $name = basename($img, ".png"); 
    echo "<img title='$name' src='$img' data-parameters='{\"zChangeable\": true, \"x\": 215, \"y\": 200, \"colors\": \"#000000\", \"removable\": true, \"draggable\": true, \"rotatable\": true, \"resizable\": true, \"price\": 10, \"boundingBox\": \"Base\", \"autoCenter\": true}'/>"  ;
}
0
votes

try this, will happy, if it's help

foreach (glob("images/*.png") as $image)
{
    $imgs[] = $image;
}

shuffle($imgs);


$imgs = array_slice($imgs, 0, 20);


foreach ($imgs as $img) {
   echo '<img src="'.$img.'">';
  }

?>