简体   繁体   中英

Showing data correctly in html table with php

Hello this table pulls data from a database.

<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>

But the problem is when i have two users from echo $row['user_name'] //user x, user y the table rows break and show something like this: image link . See the the table row is broken. I want to show this way: expected table structure . All customers are shown on the particular day row in customers column. How to fix my code or the way of representation. Thanks in advance.

change your sql query to group concat username,service and time.

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

This query will return one row with all the user and service information in one row.

you want to combine something like this

With something like this:

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

Let me know if you struggle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM