1

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

7
  • Are you sure that glob returns values? Have you tested it? Commented Feb 25, 2016 at 23:46
  • 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 advance Commented Feb 25, 2016 at 23:53
  • 1
    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... Commented Feb 25, 2016 at 23:58
  • 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}' /> Commented Feb 26, 2016 at 0:04
  • 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. :) Commented Feb 26, 2016 at 0:06

2 Answers 2

1

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}'/>"  ;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.'">';
  }

?>

4 Comments

it works same as first code, thanx but howe i can use result of that script in <img src=""> html tag??I need that images that are listed to have parameters : data-parameters='{"zChangeable": true, "x": 215, "y": 200, "colors": "#000000", "removable": true, "draggable": true, "rotatable": true, "resizable": true, "price": 10, "boundingBox": "Base", "autoCenter": true}' />
it still displaying single image?
it display all the images but how can i put that echo in <img src=""> tag that apply atributes that images can be dragable, resizable ....
from your question, i thought you have difficulty in displaying the images, now i understood the problem. in that case it's not a correct answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.