【问题标题】:while loop not showing all the data gathered from databasewhile 循环未显示从数据库收集的所有数据
【发布时间】:2015-11-09 04:10:50
【问题描述】:

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();
?>

我被困在这里,请把我从这里弄出来

【问题讨论】:

  • 从查询中删除LIMIT 6,LIMIT 6 表示只有最后6行
  • @Shehary 那会解决什么问题。现在有 6 个结果,只显示 1 100 个结果会有什么不同????
  • @Shehary。我试过这个..还是不行。
  • @RiggsFolly 但如果想要获取所有记录,OP 仍然必须取消限制
  • 我们必须假设 OP 只需要 6 行。但他确实希望看到所有 6 个,而不仅仅是最后一个

标签: php mysql loops while-loop nested-loops


【解决方案1】:

尝试删除您的:

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

您已经在整个循环中获取行并在循环为空时给出 else 语句。你不一定要数他们。

几个月前我遇到过类似的问题,我认为这与您现在遇到的问题相同。

将 if 语句粘贴到您的 while 循环中:

if ($productCount > 0) {

然后放到while循环上面:

$productCount = 0;

在 while 循环中,在 if 语句之前:

$productCount++;

编辑:

我刚刚注意到其他事情,您实际上并没有呼应您的动态列表。您正在设置一个变量但不显示它?

【讨论】:

  • 实际上并不能解决 OP 问题,是吗
  • 我尝试删除它.. 但是当我删除它时仍然没有工作
  • Aaa 有人打败了我。无论如何,愉快的编码。
【解决方案2】:

你正在使用

$dynamicList="...

在你的循环中。我假设您稍后会在代码中回显$dynamicList。您需要使用文本连接,而不仅仅是 =

也是这样

$dynamicList .= "...

它会在每次循环时添加到字符串而不是覆盖它

【讨论】:

  • 啊.. 让我看看这个兄弟
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-08
  • 2019-09-27
  • 1970-01-01
  • 2015-04-23
相关资源
最近更新 更多