我试图在beetwen我的php中添加一个空格,我需要在输出后添加一个空格并删除最后一个输出的逗号。这是我的代码
<div id="demo-1" data-zs-src='[
<?php
$sql = "SELECT * FROM slider ORDER BY no_urut ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($data = mysqli_fetch_assoc($result)) {
echo "\"images/slideshow/".$data['img']."\",";
}
}
?>
]' data-zs-overlay="dots"></div>
当我运行该代码时,就像这样
data-zs-src="["images/slideshow/img1.jpg","images/slideshow/img2.jpg","images/slideshow/img3.jpg",]"
但我希望代码像这样
data-zs-src="["images/slideshow/img1.jpg", "images/slideshow/img2.jpg", "images/slideshow/img3.jpg"]"
答案 0 :(得分:0)
您可以进行分配并检入单个if语句,使用结果形成数组,然后使用,
(逗号空格)内爆。
<div id="demo-1" data-zs-src="[
<?php
if($result=mysqli_query($conn,'SELECT `img` FROM `slider` ORDER BY `no_urut` ASC;') && mysqli_num_rows($result)){
while($data=mysqli_fetch_assoc($result)){
$imgs[]="\"images/slideshow/{$data['img']}\"";
}
echo implode(', ',$imgs);
}
?>
]" data-zs-overlay="dots"></div>
或者使用原始的单行风格:
<div id="demo-1" data-zs-src="[<?php if($result=mysqli_query($conn,'SELECT `img` FROM `slider` ORDER BY `no_urut` ASC;') && mysqli_num_rows($result)){while($data=mysqli_fetch_assoc($result)){$imgs[]="\"images/slideshow/{$data['img']}\"";}; echo implode(', ',$imgs);}?>]" data-zs-overlay="dots"></div>