我的数据库包含来自我所在地区的优惠。因此,每笔交易都包含一个位置,deal_id(主键),它适用的星期几以及交易本身。
理想情况下,我列出了包含表格标题的数据:
位置,星期一,星期二,星期三......等等。
每行只有一个实例。问题是,我无法实现这一点,到目前为止,它将星期一交易从第一行的第一个位置开始。然后下一行与每天的星期二交易相同,如下所示:
以下是将我的数据转换为HTML表格的PHP代码: 我认为错误在我的循环中。不确定如何从一个$行切换到下一个
<!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>
答案 0 :(得分:2)
简单的解决方案: 切换案例 到每一天。
编辑:每日优惠。
<!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>
答案 1 :(得分:0)
直接循环:
$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 [],但为了清楚起见,此处显示。