【问题标题】:Showing data correctly in html table with php使用 php 在 html 表中正确显示数据
【发布时间】:2015-04-15 16:22:01
【问题描述】:

您好,此表从数据库中提取数据。

<h2>Weekly appointment list</h2>

      <table class="table table-bordered table-hover">
        <thead>

          <tr>
            <th>Week Day</th>
            <th>Customers</th>
            <th>Selected service</th>
            <th>Time</th>
          </tr>
        </thead>

        <tbody>
          <tr>
            <td>Monday</td>


                <?php 
                    $date = date('Y-m-d', strtotime("this Monday"));
                    $sql = "SELECT * FROM appointment WHERE weekday = '$date'";
                    $query = mysqli_query($db, $sql);
                    $numRows = mysqli_num_rows($query);

                    if ($numRows > 0) {
                    while ($row = mysqli_fetch_array($query)) { ?>

                        <td><?php echo $row['user_name'] ?></td>
                        <td><?php echo $row['service'] ?></td>
                        <td><?php echo $row['time'] ?></td>

                    <?php } 
                        }
                     ?>
            </tr>

        </tbody>

但问题是当我有来自echo $row['user_name'] //user x, user y 的两个用户时,表格行会中断并显示如下内容:image link。看表行坏了。我想这样展示:expected table structure。所有客户都显示在客户列的特定日期行中。如何修复我的代码或表示方式。提前致谢。

【问题讨论】:

    标签: php html


    【解决方案1】:

    将您的 sql 查询更改为组合用户名、服务和时间。

     $sql ="SELECT group_concat(user_name) as user_name,group_concat(service) as service ,group_concat(time) as time FROM appointment WHERE weekday = '$date'";
    

    此查询将返回一行,其中包含所有用户和服务信息。

    【讨论】:

    • 优秀。这是我一直在寻找的东西。谢谢
    【解决方案2】:

    你想结合this之类的东西

    这样的:

    $res  = mysql_query(/**/);
    $rows = array();
    while($row = mysql_fetch_assoc($res)){
      array_push($rows, $row);
    }
    

    如果你有困难,请告诉我。

    【讨论】:

    • 这并不能修复/解决 OP 问题。您所做的只是将所有返回的行保存到 php 数组中。 OP 想要按天对逗号列表中的所有数据进行分组。
    • @Sean 是的,我想要逗号分隔列表中的用户名。谢谢
    • @Sean 将它与我发布的链接结合起来,是的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 2023-03-29
    • 2019-07-24
    • 2021-06-23
    • 1970-01-01
    相关资源
    最近更新 更多