我有一个滑块,我以这种方式从数据库中获取数据:
$sql="SELECT * FROM `banners` ORDER BY `id` DESC LIMIT 4";
$banners = $rs->get_object_list($sql);
<?php foreach($banners as $ban) {
$imagen="imagenes/banners/$ban->id/img.jpg"; ?>
<img id="<?php echo $ban->id ?>" align="center" src="<?php echo $imagen ?>" class="slide" alt=""/>
<?php } ?>
<script type="text/javascript">
if(!window.slider) var slider={};
slider.data=[{"id":"1","client":"","desc":""},{"id":"2","client":"","desc":""},{"id":"3","client":"","desc":""},{"id":"4","client":"","desc":""}];
</script>
但我有一个问题,如果我上传例如3张图片,最后一张图片与3号图片相同,我想我需要编写一些dymanic,我尝试了这段代码:
slider.data=[{"id":"slide-img-<?php echo $ban->id ?>","client":"","desc":""}];
但它不起作用,希望有人可以帮助我。 谢谢!
答案 0 :(得分:1)
您可以执行以下操作:
<script type="text/javascript">
if(!window.slider) var slider={};
slider.data= [
<?php
$resultstr = array();
foreach ($banners as $ban) {
$resultstr[] = "{'id': $ban->id,'client':'','desc':''}";
}
$result = implode(",",$resultstr);
echo $result;
?>];
</script>