使用PHP将MySQL查询结果显示到表中 [英] Display results from MySQL query into table using PHP

查看:112
本文介绍了使用PHP将MySQL查询结果显示到表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个时间表,它所做的是显示前一天的时间表以及谁已经预订了每个时段(这是一个广播电台。)

This is for a timetable and what it does is displays the previous day's timetable and who has booked each slot (this is for a radio station.)

在它显示谁按时间顺序预订了每个插槽,但我希望它能说明每个结果旁边的时间。查询输出24行(从午夜到23:00),并在每个插槽旁边显示(00:00,01:00,02:00,03:00 ... 21:00,22 :00等等。)

At the moment it displays who has booked each slot in chronological order however I would like it to say the time next to each result. The query outputs 24 rows (which is from Midnight to 23:00) and next to each slot I would like it to say (00:00, 01:00, 02:00, 03:00... 21:00, 22:00 so on so forth.)

这是我目前的代码:

This is my current code:

<?php 
include("../config.php");

#// Timetable Clearup Variabls
$yesterday = strtotime('yesterday');
$yesterdow = date('l',$yesterday);

echo "<table width=\"580px\" class=\"board\" border=\>";

$order = "SELECT * FROM timetable WHERE day = '$yesterdow'";
$result = mysql_query($order);

// Error checking
if (!$result) {
  // output error, take other action
}
else {
  while ($row=mysql_fetch_array($result)){
     // Append all results onto an array
     $rowset[] = $row;
  }
}
    foreach ($rowset as $row) {
  echo "<tr><td>" . htmlspecialchars($row['username']) . "</td></tr>";
}



?> 

您能帮忙吗?

Can you help?

推荐答案

我认为我们都非常看重一个非常简单的问题。您已在查询中使用 SELECT * ,因此您已经从表格中提取了所有三列。所以现在,你需要做的是在表格的每一行添加另一个单元格。

I think we're all looking too hard at a VERY simple problem. You are already using SELECT * in your query, so you're already fetching all three columns from your table. So now, all you need to do is add another cell to each row of your table.

echo "<tr><td>" . htmlspecialchars($row['username']) . "</td><td>" . htmlspecialchars($row['time']) . "</td></tr>";

为了确保以正确的顺序读取行,您应该添加一个 ORDER BY 到您的查询中:

And to make sure you are fetching your rows in the correct order, you should add an ORDER BY to your query:

SELECT * FROM timetable WHERE day = '$yesterdow' ORDER BY time

如果您不指定 ORDER BY 子句,你不能保证你会得到任何特定顺序的结果。

If you don't specify an ORDER BY clause, you have no guarantee that you will get the results in any particular order.

最后一件事,你循环两遍,不必要的。摆脱foreach循环,并将回声直接放在while循环中。

And one last thing, you are looping through the rows twice, unnecessarily. Get rid of the foreach loop and put the echo directly inside the while loop.

这篇关于使用PHP将MySQL查询结果显示到表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆