[英]Formatting HTML Table from SQL Database
我的数据库包含本地区域的交易。 因此,每笔交易都包含一个位置,deal_id(主键),适用的星期几以及交易本身。
理想情况下,我列出表头为的数据:
位置,星期一,星期二,星期三等等。
每行每个位置只有一个实例。 问题是,我无法做到这一点,到目前为止,它整天都在第一行的第一个位置进行周一交易。 然后,下一行与每天的星期二交易在同一位置,如下所示:
这是将我的数据转换为HTML表的PHP代码:我认为错误出在我的循环中。 不确定如何从一个$ row切换到下一个
<!DOCTYPE HTML>
<html lang=en>
<head>
<title>Ashdeals</title>
<meta charset = "UTF-8">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<?php
//Create Connection
$mysqli = new MySQLi("localhost", "root", "", "ashdeals");
$sql = "SELECT * FROM deals";
// Query Database
$result = $mysqli->query($sql);
//Count the returned rows
if ($result->num_rows !=0){
//Turn results into an array
echo "<table>
<tr>
<th>Location</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
<th>Sunday</th>
</tr>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
$location = $row['location'];
$deal = $row['deal'];
echo
"<tr>
<td>" . $location . "</td>
<td>" . $deal . "</td>
<td>" . $deal . "</td>
<td>" . $deal . "</td>
<td>" . $deal . "</td>
<td>" . $deal . "</td>
<td>" . $deal . "</td>
<td>" . $deal . "</td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
?>
</body>
</html>
简单的解决方案:每天更换机箱 。
编辑:每日交易。
<!DOCTYPE HTML>
<html lang=en>
<head>
<title>Ashdeals</title>
<meta charset = "UTF-8">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<?php
//Create Connection
$mysqli = new MySQLi("localhost", "root", "", "ashdeals");
$sql = "SELECT * FROM deals";
// Query Database
$result = $mysqli->query($sql);
//Count the returned rows
if ($result->num_rows !=0){
//Turn results into an array
echo "<table>
<tr>
<th>Location</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
<th>Sunday</th>
</tr>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
$location = $row['location'];
$deal = $row['deal'];
$day = $row['day'];
$moDeal = ""; // Monday Deal variable.
$tuDeal = ""; // Tuesday Deal variable.
$weDeal = ""; // Wednesday Deal variable.
$thDeal = ""; // Thursday Deal variable.
$frDeal = ""; // Friday Deal variable.
$saDeal = ""; // Saturday Deal variable.
$suDeal = ""; // Sunday Deal variable.
switch ($day) {
case 'Mo':
$moDeal = $deal;
break;
case 'Tu':
$tuDeal = $deal;
break;
case 'We':
$weDeal = $deal;
break;
case 'Th':
$thDeal = $deal;
break;
case 'Fr':
$frDeal = $deal;
break;
case 'Sa':
$saDeal = $deal;
break;
case 'Su':
$suDeal = $deal;
break;
default:
# In case some daily deal:
$moDeal = $deal;
$tuDeal = $deal;
$weDeal = $deal;
$thDeal = $deal;
$frDeal = $deal;
$saDeal = $deal;
$suDeal = $deal;
break;
}
echo
"<tr>
<td>" . $location . "</td>
<td>" . $moDeal . "</td>
<td>" . $tuDeal . "</td>
<td>" . $weDeal . "</td>
<td>" . $thDeal . "</td>
<td>" . $frDeal . "</td>
<td>" . $saDeal . "</td>
<td>" . $suDeal . "</td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
?>
</body>
</html>
只需循环:
$dayArray = ['Monday', 'Tuesday' ..... 'Sunday'];
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['locationField'] . "</td>";
foreach ($dayArray as $dayField) {
if ($dayField == $row['dealDayField'] ) {
echo "<td>" . $row['dealField'] . "</td>";
}
else {
echo "<td></td>";
}
}
echo "</tr>";
}
echo "</table>";
无需分配$ valueX并可以回显$ row [],但此处为了清楚起见而显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.