duanbi7247 2016-07-18 05:32
浏览 8
已采纳

将数据提取为表格格式

I am trying to fetch data from database in tabular format. I have taken count variable as a number of rows and 5 columns. When I execute this query all results are fetched in multiple row and single columns. Here is my code :

<?php
include("connect.php");
$all_product = mysqli_query($connect, "SELECT * FROM product WHERE user_id='$fetch_user_id'");
echo '<table style="background-color:white; margin-left:50px; margin-right:50px;">';
$count = mysqli_num_rows($all_product);
$count = ($count / 5) + 1;

for ($i = 0; $i < $count; $i++) {
    echo "<tr[$i]>";

    for ($m = 0; $m < 5; $m++) {
        echo "<td[$m]>";

        while ($all = mysqli_fetch_array($all_product)) {
            echo '<img src="' . $all['image'] . '" width="150px" height="150px" />
                <div class="search_details">
                <figcaption>' . $all['name'] . '</figcaption>
                <figcaption>by ' . $all['brand'] . '</figcaption><figcaption>&#8360;' . $all['price'] . '</figcaption>
                <figcaption>Save: &#8360;' . $local_price . ', (' . $all['discount'] . '%)</figcaption><figcaption>In stock:' . $all['stock'] . '</figcaption></div>';
        }

        echo "</td[$m]>";
    }

    echo "</tr[$i]>";
}

echo "</table>";
?>

Any help ? Where am I going wrong ? Thank you in advance

  • 写回答

1条回答 默认 最新

  • doujiao1981 2016-07-18 07:01
    关注

    I hope this helps:

     <?php
        $all_product = mysqli_query($connect, "SELECT * FROM product WHERE user_id='$fetch_user_id'");
        $i= 0;
        $m=5;
        echo '<table width="100%" margin-left="30px" style="background-color:white;">';
        while ($all = mysqli_fetch_array($all_product)) {
        if($i%5==0) {
        echo "<tr>";
                    }
        echo '<td style="width:20%">';
        echo '<img src="'.$all['image'].'" width="150px" height="150px" />
        <div class="search_details">
        <figcaption>'.$all['name'].'</figcaption>
        <figcaption>by '.$all['brand'].'</figcaption><figcaption>&#8360;'.$all['price'].'</figcaption>
        <figcaption>Save: &#8360;'.$local_price.', ('.$all['discount'].'%)</figcaption><figcaption>In stock: '.$all['stock'] .'</figcaption></div>';
        echo "</td>";
        if($i%$m==$m - 1){
        echo "</tr>";
                     }
        $i++;
                     }
        echo "</table>";
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?