duanjianxu4288 2017-04-24 08:19
浏览 23
已采纳

显示mysql数据库中的所有项目

I have table product and table category in my database. I want to display all category dynamically in a table and inside the table of each category I am displaying all item belonged to that category too.

Those categories and items should be displayed like this : enter image description here

And here is my coding to do the work :

$fetch_cat = "SELECT * FROM tblcat"; //fetch from table category
$result = $conn->query($fetch_cat);
if ($result->num_rows > 0)
{
    while($cat_row = mysqli_fetch_assoc($result))
    {
        $cat_title = $cat_row['catName'];
        echo '<table>';
        echo '<tr>';
        echo '<td><img src="category/'.$cat_row['catImg'].'" /></td>';
        echo '<td>';
        echo '<ul class="content_init">';

        $stmt = "SELECT * FROM tblproduct WHERE prodCat = '".addslashes($cat_title)."' LIMIT 4"; //fetch from table product
        $result = $conn->query($stmt);
        if ($result->num_rows > 0)
        {
            while($row = mysqli_fetch_assoc($result))
            {
                echo '<li>';
                echo '<a href="#"><img style="height: 188px; width: 188px;" src="user_images/'.$row['prodImg'].'" />';
                echo '<br /><br />';
                echo '<h4>'.$row['prodName'].'</h4>';
                echo '<label><span>RM </span>'.$row['prodPrice'].'</label></a>';
                echo '</li>';
            }
        }

        echo '</ul>';
        echo '</td>';
        echo '</tr>';
        echo '</table>';
    }
}

Problem :

So the problem with my coding is, it can only display Category 1 with items belonged to it. The rest categories unable to be displayed. I guess my coding might not loop properly because of bad programming as it unable to display the expected output.

  • 写回答

3条回答 默认 最新

  • douduan9391 2017-04-24 08:26
    关注

    The $result variable was reused by the inner query thus the first result override.

    Change the variable name either of the $result variable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思