我正在努力解决一个已在此处部分解决的概念,但是我试图对其进行扩展并陷入困境。 我的查询从三个表中提取数据(我正在使用存储过程):
Select a.adID, a.typeID, l.description, l.available, l.cost, a.heading,
a.adDate, a.adActive, a.plannerID, t.typename
From advertisements as a, advert_types as t, locations as l
Where a.plannerID = 1 And a.typeID = t.typeID And l.locationID =
a.locationID
Order by a.adDate, a.typeID;
使用MySQL工作台中的查询编辑器,输出似乎正常,但我尝试使用Month(a.adDate)作为第一个标题,然后将字段t.typename作为第二个标题,写出结果,所以它应该看起来像这样:
January
___________________________________________________
Bistro Heading Date
------------------------------
E-newsletter $100 ...
Digital Ad $50 ...
Magazine Ad $150 ...
February
___________________________________________________
Kitchen Heading Date
-------------------------------
Social Media $200 ...
E-flyer Ad $180 ...
March
___________________________________________________
Restaurant Heading Date
-------------------------------
Front cover $500 ...
Back cover $350 ...
每个月下都有一个小标题(Bistro,厨房,餐厅等),然后每个相应的小标题下以及正确的月份下都应列出项目。
我的PHP代码是:
$query = "Call listads_per_planner('$planner');";
$result = mysqli_query($conn, $query) or die("Query failed: " .
mysqli_error($conn));
$num_rows = mysqli_num_rows($result);
$priorDate = null;
$priorType = null;
$counter = 0;
while ($row = $result->fetch_assoc()) {
$theDate = $row['adDate'];
$thestrDate = strtotime($theDate);
$theadYear = date('Y', $thestrDate);
$theadMonth = date('F', $thestrDate);
$adMonthInt = date('m', $thestrDate);
$typeName = $row['typename'];
$heading = $row['heading'];
$thetypeID = $row['typeID'];
if ($priorDate != $adMonthInt) {
if ($priorDate != null) {
echo "";
}
if ($counter > 0){
echo "<br /><br />\n";
}
echo "<h3>". $theadMonth ."</h3>\n";
echo "<hr style=\"padding: 0; margin: 0\"/>\n";
($priorDate = $adMonthInt);
}
if ($thetypeID != $priorType){
echo "<table border=\"0\" style=\"width: 100%; cellpadding: 2px;
cellspacing: 1px\" >\n";
echo "<tr>\n";
echo "<td style=\"background-color: #cccccc; heigh: 26px; width: 30%\">\n";
echo "<span style=\"margin-left: 3px\"><strong>". $typeName ."</strong>
</span></td>\n";
echo "<td style=\"background-color: #cccccc;\"><input type=\"text\"
name=\"heading\" size=\"22\" maxlength=\"100\" value=\"". $heading ."\">
</td>\n";
echo "<td style=\"background-color: #cccccc;\"><span style=\"margin-left:
2px\">Date: <select name=\"day\">\n";
echo "<option value=\"1\">1</option>\n";
echo "<option value=\"2\">2</option>\n";
echo "<option value=\"3\">3</option>\n";
echo "<option value=\"4\">4</option>\n";
echo "<option value=\"5\">5</option>\n";
//...
echo "<option value=\"31\">31</option>\n";
echo "</select> <select name=\"month\">\n";
echo "<option value=\"1\">January</option>\n";
echo "<option value=\"2\">February</option>\n";
echo "<option value=\"3\">March</option>\n";
echo "<option value=\"4\">April</option>\n";
//...
echo "<option value=\"12\">December</option>\n";
echo "</select></span></td>\n";
echo "<td style=\"background-color: #cccccc;\"><div align=\"right\">
</td>\n";
echo "<td style=\"background-color: #cccccc;\"><table border=\"0\"
style=\"width: 100%\" cellspacing=\"0\" cellpadding=\"0\"><tr><td><div
align=\"right\"><button type=\"submit\" class=\"btn btn-
success\">Save</button></div></td><td></td><td><div align=\"right\"><button
type=\"button\" class=\"btn btn-danger\">Remove</button></td></tr></table>
</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><strong>Location</strong> <a href=\"#\">> Add Location +</a></td>
<td><strong>Available</strong></td><td><strong>Cost</strong></td><td>
<strong>Default Cost</strong></td><td><strong>Delete?</strong></td>\n";
echo "</tr>\n";
$priorType = $thetypeID;
}
echo "<tr>\n";
echo "<td style=\"height: 26px\">". $row['description'] ."</td>\n";
echo "<td><input type=\"text\" name=\"avail\" size=\"2\" value=\"".
$row['available'] ."\"></td>\n";
echo "<td><input type=\"text\" name=\"cost\" size=\"7\" maxlength=\"8\"
value=\"". $row['cost'] ."\"></td>\n";
echo "<td>". $row['cost'] ."</td>\n";
echo "<td><div align=\"left\" style=\"padding-left: 4px\"><a href=\"#\"
title=\"Delete this location?\"><img src=\"img/delete.png\" border=\"0\">
</a></div></td>\n";
echo "</tr>\n";
$counter++;
}
echo "</table>\n";
我在浏览器输出中看到意外结果,子标题未正确填充,并且列表结果显示在错误的月份下。 如果有人能提出一些见识,我将不胜感激。我可以使它仅与单个标题一起使用,但似乎不能与副标题一起使用。 非常感谢。
更新:这是来自数据库的一些示例数据:
这是我得到的输出的屏幕截图:一月应该列出两个位置,而十一月应该只有一个位置。
答案 0 :(得分:1)
无论何时开始新的月份,都需要结束表以获取先前的类型ID,并将$priorType
重置为null
。否则,如果连续几个月具有相同的类型ID,它将不会启动新表。
开始新的类型ID时,您需要结束表以使用先前的类型ID。
$query = "Call listads_per_planner('$planner');";
$result = mysqli_query($conn, $query) or die("Query failed: " .
mysqli_error($conn));
$num_rows = mysqli_num_rows($result);
$priorDate = null;
$priorType = null;
$counter = 0;
while ($row = $result->fetch_assoc()) {
$theDate = $row['adDate'];
$thestrDate = strtotime($theDate);
$theadYear = date('Y', $thestrDate);
$theadMonth = date('F', $thestrDate);
$adMonthInt = date('m', $thestrDate);
$typeName = $row['typename'];
$heading = $row['heading'];
$thetypeID = $row['typeID'];
if ($priorDate != $adMonthInt) {
if ($priorDate != null) {
if ($priorType != null) {
echo "</table>\n";
$prioType = null;
}
echo "";
}
if ($counter > 0){
echo "<br /><br />\n";
}
echo "<h3>". $theadMonth ."</h3>\n";
echo "<hr style=\"padding: 0; margin: 0\"/>\n";
($priorDate = $adMonthInt);
}
if ($thetypeID != $priorType){
if ($priorType != null) {
echo "</table>\n";
}
echo "<table border=\"0\" style=\"width: 100%; cellpadding: 2px;
cellspacing: 1px\" >\n";
echo "<tr>\n";
echo "<td style=\"background-color: #cccccc; heigh: 26px; width: 30%\">\n";
echo "<span style=\"margin-left: 3px\"><strong>". $typeName ."</strong>
</span></td>\n";
echo "<td style=\"background-color: #cccccc;\"><input type=\"text\"
name=\"heading\" size=\"22\" maxlength=\"100\" value=\"". $heading ."\">
</td>\n";
echo "<td style=\"background-color: #cccccc;\"><span style=\"margin-left:
2px\">Date: <select name=\"day\">\n";
echo "<option value=\"1\">1</option>\n";
echo "<option value=\"2\">2</option>\n";
echo "<option value=\"3\">3</option>\n";
echo "<option value=\"4\">4</option>\n";
echo "<option value=\"5\">5</option>\n";
//...
echo "<option value=\"31\">31</option>\n";
echo "</select> <select name=\"month\">\n";
echo "<option value=\"1\">January</option>\n";
echo "<option value=\"2\">February</option>\n";
echo "<option value=\"3\">March</option>\n";
echo "<option value=\"4\">April</option>\n";
//...
echo "<option value=\"12\">December</option>\n";
echo "</select></span></td>\n";
echo "<td style=\"background-color: #cccccc;\"><div align=\"right\">
</td>\n";
echo "<td style=\"background-color: #cccccc;\"><table border=\"0\"
style=\"width: 100%\" cellspacing=\"0\" cellpadding=\"0\"><tr><td><div
align=\"right\"><button type=\"submit\" class=\"btn btn-
success\">Save</button></div></td><td></td><td><div align=\"right\"><button
type=\"button\" class=\"btn btn-danger\">Remove</button></td></tr></table>
</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><strong>Location</strong> <a href=\"#\">> Add Location +</a></td>
<td><strong>Available</strong></td><td><strong>Cost</strong></td><td>
<strong>Default Cost</strong></td><td><strong>Delete?</strong></td>\n";
echo "</tr>\n";
$priorType = $thetypeID;
}
echo "<tr>\n";
echo "<td style=\"height: 26px\">". $row['description'] ."</td>\n";
echo "<td><input type=\"text\" name=\"avail\" size=\"2\" value=\"".
$row['available'] ."\"></td>\n";
echo "<td><input type=\"text\" name=\"cost\" size=\"7\" maxlength=\"8\"
value=\"". $row['cost'] ."\"></td>\n";
echo "<td>". $row['cost'] ."</td>\n";
echo "<td><div align=\"left\" style=\"padding-left: 4px\"><a href=\"#\"
title=\"Delete this location?\"><img src=\"img/delete.png\" border=\"0\">
</a></div></td>\n";
echo "</tr>\n";
$counter++;
}
if ($priorType != null) {
echo "</table>";
}