2015-08-16 29 views
2

while循環不顯示從數據庫收集的所有數據..只顯示頁面中最後一個收集的項目。而不是全部展示。while循環不顯示從數據庫收集的所有數據

<?php 
    include "connect_to_mysql.php"; 
    $dynamicList = ""; 
    $sql = mysql_query("SELECT * FROM product ORDER BY date DESC LIMIT 6"); 
    $productCount = mysql_num_rows($sql); // count the output amount 
    if ($productCount > 0) { 

    //here is problem in this while loop.. dont know why it is not gathering all the items. 


     while($row = mysql_fetch_array($sql)){ 
     $id = $row["id"]; 
     $product_name = $row["p_name"]; 
     $price = $row["price"]; 
     $date_added = strftime("%b %d, %Y", strtotime($row["date"])); 
     $details = $row["details"]; 
     $category = $row["category"]; 
     $subcategory = $row["sub_category"]; 
     $category2 = $row["category2"]; 
     $img_name = $row["img_name"]; 
     $v_id = $row["v_id"]; 
     $v_name = $row["v_name"]; 
     $v_number = $row["v_number"]; 
     $v_email = $row["v_email"]; 

     $dynamicList=" 

      <div id=\"single_product\" style=\"float:left; margin-left:20px; padding:10px;\"> 

      <h3> $product_name </h3> 
      <h3> <img src='pics/$img_name' width='200px' height='200px'/> </h3> 
      <p><b><center> RUP $price </center></b></p> 

      <a href=\"details.php\" style=\"float:left; font-size:20px;\">Details</a> 
      <a href=\"cart.php\" style=\"float:right; font-size:20px;\">Add To Cart</a> 

      </div>    
     "; 

     } 
    } else { 
     $dynamicList = "We have no products listed in our store yet"; 
    } 
    mysql_close(); 
?> 

我是stucked這裏PLZ讓我離開我從這裏走出

+0

刪除'LIMIT 6' ,'LIMIT 6'表示只有最後6行 – Shehary

+0

@Shehary這將解決什麼。現在有6個結果,只顯示1個結果會有什麼區別? – RiggsFolly

+0

@Shehary。我試過這個..仍然沒有工作。 –

回答

2

您正在使用

$dynamicList="... 

在你的循環。我假設你稍後在代碼中迴應$dynamicList。您需要使用的文字串聯,而不僅僅是一個=

所以做

$dynamicList .= "... 

而且每次都會添加到字符串一輪循環,而不是覆蓋從查詢它

+0

啊..讓我檢查這個兄弟 –

+0

感謝brotha ...現在工作。 –

0

嘗試刪除您:

$productCount = mysql_num_rows($sql); // count the output amount 

你已經在你的整個循環讀取行,並給予一個else語句如果循環是空的。你不一定要數它們。

我幾個月前遇到過類似的事情,並且認爲這是你現在遇到的同樣的問題。

棒while循環內的if語句:

if ($productCount > 0) { 

然後while循環上面放:

$productCount = 0;

而while循環中,你的if語句前:

$productCount++; 

編輯:

我剛剛發現了一些東西,你實際上並沒有呼應你的動態列表。你正在設置一個變量但不顯示它?

+0

雖然沒有真正解決OP問題,但它是 – RiggsFolly

+0

我試圖刪除..但仍然沒有工作時,我刪除了 –

+0

我已經更新了我的答案 –