在.JS文件中编写php而循环代码

时间:2013-01-16 12:18:56

标签: php javascript loops

在JS图像中,幻灯片文件脚本就像这样

slides :    [           // Slideshow Images
                                            {image : 'images/bgslide/r1.jpg', title : '<h1>Välkommen till Restaurangen SPICE</h1> <p>Bokabord/Beställa Online/ Smaka Gott<br><span></span></p>',},
                                            {image : 'images/bgslide/r2.jpg', title : '<h1>Thai Veg-Tallrik</h1> <p>Blandade Thai veg-rätter med sallad. Helgenserbjudande<br> <span>:89 </span></p>',},
                                            {image : 'images/bgslide/r3.jpg', title : '<h1>Välkommen till Restaurangen SPICE</h1> <p>Bokabord/Beställa Online/ Smaka Gott<br> <span></span> </p>',},
                                            {image : 'images/bgslide/r4.jpg', title : '<h1>Dessert</h1> <p>vaniljglass med körsbär<br><span>:59 </span></p>',}
                                    ],

但我想用php mysql制作这张幻灯片。想要使用while循环。我写的循环代码是

slides : [ // Slideshow Images <?php $q_f ="SELECT * FROM bgslide order by id" or die($query."<br/>".mysql_error()); $r_f = mysql_query($q_f); $comma=''; while($row = mysql_fetch_array($r_f)) { ?> <?php echo $comma; ?> {image : 'images/bgslide/<?php echo $row["bgimg"]; ?>', title : '<h1><?php echo $row["captitle"]; ?></h1> <p><?php echo $row["capdsc"]; ?><br><span><?php echo $row["price"]; $comma=', '; ?></span></p>',} <?php } ?> ], 它不起作用。任何人都可以帮我解决这个问题..?

1 个答案:

答案 0 :(得分:-1)

试试这个 -

<?php 
    $q_f ="SELECT * FROM bgslide order by id" or die($query."<br/>".mysql_error()); 
    $r_f = mysql_query($q_f); 
    $slide = ''; 
    while($row = mysql_fetch_array($r_f)) {
        $slide .= "{image : 'images/bgslide/".$row["bgimg"]."', title : '<h1>".$row["captitle"]."</h1> <p>".$row["capdsc"]."<br><span></span></p>',},";
    }
    ?>
slides :    [<?php echo $slide?>],