php - 在表格中垂直显示来自数据库的数据

标签 php html mysql

<?php

echo "<table class=\"table table-hover table-bordered\">";
echo "<thead>";
echo "<tr>";
$result=mysqli_query($con,"SELECT day FROM time_slot ORDER BY day;");

while($row=mysqli_fetch_array($result))
{
    $days = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
    $day = $row['day']-1;
    $day = $days[$day];
    echo '<th>';
    echo $day;
    echo '</th>';
}
echo "</tr>";
echo "</thead>";

require('../includes/connection.php');

$result=mysqli_query($con,"SELECT * FROM time_slot ORDER BY day DESC;");

while($row=mysqli_fetch_array($result))
{
    echo "<tbody>";  
    echo "<tr>";

这部分数据应该相对于天垂直显示在表格中

    //This code to split time into intervals

    $starttime = $row['start_time'];  // your start time
    $endtime = $row['end_time'];  // End time
    $duration = $row['slot_time'];  

    $array_of_time = array ();
    $start_time    = strtotime ($starttime); //change to strtotime
    $end_time      = strtotime ($endtime); //change to strtotime

    $add_mins  = $duration * 60;

    while ($start_time <= $end_time) // loop between time
    {
        echo '<td>';
        echo "<div align=\"center\">";
        print_r(date ("h:i", $start_time) ."-". date("h:i", $start_time += $add_mins));
        //
        echo "</br><input type=\"radio\" name=\"texi\" value=\"1\" ></input>";
        echo '</td>' ;
        echo '</div>';
    }
    echo "</tr>";
    echo "</tbody>";
}            

mysqli_close($con);

echo  "</table>
       </div>
       <input type=\"submit\" value=\"Book Slot\"  class=\"button_drop\">
       </form>";
?>

我想在表格中相对于日期垂直显示时间段。 这是此代码的输出:

Output

但是我想显示应该垂直显示的水平显示时间。例如。第一行1-3、3-5、5-7应显示在星期一下方,然后下一行应显示在星期二下方,依此类推。

最佳答案

尝试使用这样的查询:

SELECT day FROM time_slot ORDER BY time_slot, day;

这样应该可以更轻松地处理数据。我看不到你的确切数据结构,但看起来你应该得到 7x 01:00-03:00 , 然后 7x 03:00-05:00等。每组应按天排序。然后你可以写出你的<td>按照数据来自数据库的顺序排列。跟踪 day 何时值更改(或只数到 7)并开始新的 <tr>那时。

关于php - 在表格中垂直显示来自数据库的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41485370/

相关文章:

PHP 比较两个字符串中的一个字符不同,通配符方法

php - 无法使用 PHP 5.5 将数据添加到 webmatrix 3 中的 MySQL 5.7

html - 如何从 Chrome 网络抓取插件的可视弹出窗口中提取数据?

javascript - 插件在 WordPress 4.5 更新后抛出 TypeError

html - 如何使用css在一条线的末端和第二条线的基线之间创建指定的距离

mysql - 连接两个表,但获取第二个表中每组的最后一条记录 mysql

mysql - 如何将 order by 和 group by 结合在一起?条件 : order by one column group by another column

mysql - 内连接三个具有垂直和水平引用的表

php - 使用 PHP 在 Intl/ICU 中生成列表

PHP-MySQL : Synchronize two calendars