2016-11-22 88 views
-2

好吧,所以我想要顯示4個隨機圖像,並顯示它們從數據庫中。隨機性似乎工作,但我不明白如何顯示4個不同的結果。它只顯示1個結果4次。如果您有任何反饋,請讓我知道謝謝。如何顯示隨機數據從MySQl與PHP

<div class="similar-entrees-section food-tabs"> 
<h3>Similar entrees</h3> 
<?php if ($food['catId']=1):?> 
<?php $random=$db->fetchRow("select * from foods where catId=1 ORDER BY RAND() LIMIT 4"); ?> 
<div class="row"> 

      <div class="col-xs-6 col-sm-3"> 
      <a href="#" class="food-block"> 
       <div class="food-img"><img src="img/menu-items/<?php echo $random['image'] ; ?>" class="img-responsive" alt=""></div> 
       <h5>Cheddar Omelet 
       +&nbsp;Chicken Sausage <span class="chilly"></span> </h5> 
       </a> 
       </div> 

      <div class="col-xs-6 col-sm-3"> 
      <a href="#" class="food-block"> 
       <div class="food-img"><img src="img/menu-items/<?php echo $random['image'] ; ?>" class="img-responsive" alt=""></div> 
       <h5>Gluten Free Breaded 
       Chicken <span class="chilly"></span></h5> 
       </a> 
       </div> 

      <div class="col-xs-6 col-sm-3"> 
      <a href="#" class="food-block"> 
       <div class="food-img"><img src="img/menu-items/<?php echo $random['image'] ; ?>" class="img-responsive" alt=""></div> 
       <h5>Mesquite Grilled 
       Chicken Breast</h5> 
       </a> 
      </div> 

      <div class="col-xs-6 col-sm-3"> 
      <a href="#" class="food-block"> 
       <div class="food-img"><img src="img/menu-items/<?php echo $random['image'] ; ?>" class="img-responsive" alt=""></div> 
       <h5>Mesquite Grilled 
       Chicken Breast <span class="chilly"></span></h5> 
       </a> 
      </div> 


      </div> 
<?php endif; ?> 
+0

現在,您只需輸出相同的圖像四次。您需要創建FOR循環並遍歷結果 –

+2

如果($ food ['catId'] = 1)'您的if將不起作用''將始終等於true,您需要使用==。 – mic

回答

-1

fetchRow應該只是一排,而不是一切?

然後你使用<?php echo $random['image'] ; ?>這將只是一次又一次顯示同一行。

您可能想要使用類似getAll()(如果您的PEAR正在使用)來獲取所有結果,然後循環播放它們。

也許這樣

<div class="similar-entrees-section food-tabs"> 
    <h3>Similar entrees</h3> 
    <?php if ($food['catId'] = 1): ?> 
    <?php $random = $db->getAll("select * from foods where catId = 1 ORDER BY RAND() LIMIT 4"); ?> 
    <div class="row"> 
     <?php foreach ($random as $r) { ?> 
      <div class="col-xs-6 col-sm-3"> 
       <a href="#" class="food-block"> 
        <div class="food-img"> 
         <img src="img/menu-items/<?php echo $r['image']; ?>" class="img-responsive" alt=""></div> 
        <h5>Cheddar Omelet +&nbsp;Chicken Sausage <span class="chilly"></span></h5> 
       </a> 
      </div> 
     <?php } ?> 
    </div> 
<?php endif; ?> 
+0

爲什麼投了票? –

0

首先你需要

<?php 
    $res = $db->query("select * from foods where catId=1 ORDER BY RAND() LIMIT 4"); 
    $images = $db->getAll($res); 
?> 

更換

<?php $random=$db->fetchRow("select * from foods where catId=1 ORDER BY RAND() LIMIT 4"); ?> 

,前每個圖像的,而不是

<?php echo $random['image'] ; ?> 

你應該使用

<?php echo $images[0]['image'] ; ?> 

第一個圖像

<?php echo $images[1]['image'] ; ?> 

第二,等等

我也建議你instrad具有HTML代碼爲每個圖像有一個循環,最終會生成所有圖像的html。看看如何循環瀏覽結果,請看下面的鏈接。

https://pear.php.net/manual/en/package.database.mdb.intro-fetch.php

+0

我得到這個致命錯誤的錯誤:調用未定義的方法數據庫:: getAll() – David

+0

你可以請檢查PHP手冊有關正確的語法?我沒有使用什麼驅動程序/版本,但應該有這樣的東西。如果你找到它,只需讓我知道更新我的答案:) – jsalatas