簡體   English   中英

用PHP在HTML表中正確顯示數據

[英]Showing data correctly in html table with php

您好,此表從數據庫中提取數據。

<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 看到表行已損壞。 我想顯示這種方式: 預期的表結構 所有客戶都顯示在“客戶”列中的特定日期行。 如何修正我的代碼或表示方式。 提前致謝。

更改您的sql查詢以將concat用戶名,服務和時間分組。

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

此查詢將返回一行,其中所有用戶和服務信息都位於一行中。

要像結合

用這樣的東西:

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

讓我知道你是否在掙扎。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM