使用当前数据参数在imgsrc标记中添加PHP脚本

使用当前数据参数在imgsrc标记中添加PHP脚本,php,html,image,Php,Html,Image,我有一个php脚本,它显示文件夹dir中的所有照片,我需要包含一个应用给定数据参数的html标记。 php代码是: ?php $directory = "images/*/"; $images = glob("" . $directory . "*.png" ); $imgs = ''; foreach($images as $image){ $imgs[] = "$image"; } shuffle($imgs); $imgs = array_slice($imgs, 0, 2

我有一个php脚本,它显示文件夹dir中的所有照片,我需要包含一个应用给定数据参数的html标记。 php代码是:

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

?>
?php
$directory=“images/*/”;
$images=glob(“$directory.*.png”);
$imgs='';
foreach($imagesas$image){$imgs[]=“$image”;}
洗牌($imgs);
$imgs=阵列片($imgs,0,20);
foreach($imgs作为$img){
回声“;
}
?>
我需要把它作为img src放入html标记中:

<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}' />


我试着代替echo“用$img值创建新的var并使用,但这只给了我一张图片”

试试这个,如果有帮助,会很高兴的
foreach (glob("images/*.png") as $image)
{
    $imgs[] = $image;
}

shuffle($imgs);


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


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

?>
foreach(glob(“images/*.png”)作为$image)
{
$imgs[]=$image;
}
洗牌($imgs);
$imgs=阵列片($imgs,0,20);
foreach($imgs作为$img){
回声';
}
?>
我找到了解决方案:)

foreach($imgs作为$img){
$name=basename($img,.png”);
回声“;
}

你确定glob返回值吗?你测试过它吗?是的,它可以工作,但我不知道如何将它包含在带有数据参数的html标记中。如果有人有beater解决方案,那么我的php代码我很乐意使用它,因为我在php.thanx中是新的。它可以工作吗?你测试过它吗?(
print\r($images);die();
glob
行之后),因为您要输出img的代码是正确的,所以我打赌问题出在
glob()
命令中……我测试了几次,如果我将代码放在example.php中,并将其包含在index.php中,它工作正常,但没有数据参数='{“zChangeable”:true,“x”:215,“y”:200,“colors”:#000000,“removable”:true,“Dragable”:true,“rotatable”:true,“resizable”:true,“price”:10,“boundingBox”:“Base”,“autoCenter”:true}'/>出于语义考虑!这不会解决问题,但尝试声明
$imgs=array();
而不是将它声明为字符串。如果它将是一个数组,那么它的声明应该显示::)它的工作原理与第一个代码相同,但如何在html标记中使用该脚本的结果?我需要列出的图像具有参数:数据参数='{“zChangeable”:true,“x”:215,“y”:200,“colors”:“#000000”,“removable”“:true,“draggable”:true,“rotatable”:true,“resizeable”:true,“price”:10,“boundingBox”:“Base”,“autoCenter”:true}”/>它仍然显示单个图像?它显示所有图像,但我如何将该回显放在标签中,以应用图像可拖动、可调整大小的属性…从您的问题,我以为你在显示图像时有困难,现在我明白了问题所在。在这种情况下,这不是一个正确的答案
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}'/>"  ;
}