0

我有一个滑块,我以这种方式从数据库中获取数据:

$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 张相同,我想我需要编写一些动态的东西,我尝试了这段代码:

slider.data=[{"id":"slide-img-<?php echo $ban->id ?>","client":"","desc":""}];

但它不起作用,希望有人能帮助我。谢谢!

4

1 回答 1

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> 
于 2012-10-03T00:30:05.317 回答