我正在尝试制作一个日历,该日历显示该月的每一天并显示每一天的事件信息。某些日子可能包含多个事件。麻烦在于,我一直在尝试迭代结果集并将多个事件分组为一天的行。
以下是一些示例行:
这是我检索数组的代码
$query="SELECT event_date_start, event_date_end, event_description, event_type, swim_club_assigned
FROM eventcalendar
WHERE event_date_start like '$search_date%'";
$result = mysqli_query($con, $query);
echo "<br>";
// Only loop if there is a result.
if ($result){
$r = []; // You aren't exactly clear on 'myData'. In this instance, I am setting the rows inside of it.
while ($row = $result->fetch_object()) {
$r[] = $row;
}
$result->close(); // Free result set
}
print_r($ r);给我下面的数组
Array (
[0] => stdClass Object (
[event_date_start] => 2018-10-01
[event_date_end] => 2018-10-01
[event_description] => Preparation Meet (LC) - Template: A
[event_type] => GCRSA Club Meet
[swim_club_assigned] => Gold Coast North SC Inc
)
[1] => stdClass Object (
[event_date_start] => 2018-10-11
[event_date_end] => 2018-10-11
[event_description] => Sprint Meet (LC) - Template: H
[event_type] => GCRSA Club Meet
[swim_club_assigned] => Bond SC Inc
)
[2] => stdClass Object (
[event_date_start] => 2018-10-31
[event_date_end] => 2018-10-31
[event_description] => Reg Winter Championships (SC) - Template: XY
[event_type] => GCRSA Championship
[swim_club_assigned] =>
)
)
我希望能够从数组中检索(事件类型= $something
,开始日期= $a_date
,然后显示$swim_club
和$event_description
while($row = fetch_assoc($r)) {
if (($row["event_type"] == 'GCRSA Championship') && ($row["event_date_start"] == $day)){
echo "{$row['swim_club_assigned']} {$row['event_description']}";
}
}
我一直在尝试找出如何从数组中为每一行提取关键数据(数组中每个开始日期可能有多个事件),但是我无法弄清楚检索该行的语法。行,然后从键中取出值。
要清楚,这是我要填充的日历,结果将插入相关日期行中的单元格中。这就是为什么我认为最好在顶部使用一个查询,然后在与事件日期和相关单元格的日期匹配的每个单元格中遍历数组(按照上面的示例)。我正在使用的日历如下(带有注释以显示需要在其中插入数据的单元格)
<table class="table table-bordered">
<thead>
<tr>
<th rowspan="2" colspan="2">Date</th>
<th colspan="3">GCRSA Region</th>
<th colspan="2">State, SAL, Intl</th>
<th colspan=2><center>Other events</center> </th>
</tr>
<tr style="border-bottom: 1pt Darkblue;">
<th>Club meet</th>
<th>Championship</th>
<th>Development</th>
<th>SQ</th>
<th>Sal</th>
<th>School Meets</th>
<th>Qld School Holidays</th>
</tr>
</thead>
<tbody>
<?php
// First day of the month.
$date_start = date('Y-m-01', strtotime($query_date));
$day = date('Y-m-01', strtotime($query_date));
for($i= 1; $i < date('t', strtotime($query_date)) + 1; $i++){
?>
<tr>
<td <?php if (date('w',strtotime($day)) == 0 || date('w',strtotime($day)) == 6 ){
echo "style='background-color:red;'";
}?>><?php echo date('M d, Y',strtotime($day));
?></td>
<td <?php if (date('w',strtotime($day)) == 0 || date('w',strtotime($day)) == 6 ){
echo "style='background-color:red;'";
}?>><?php echo date('l',strtotime($day)); ?></td>
<td> <?php //this bit is not working. need to iterate through array result $r to filter appropriate events here
while($row = mysqli_fetch_assoc($r)) {
if (($row["event_type"] == 'GCRSA Club Meet') && ($row["event_date_start"] == $day)){
echo "{$row['swim_club_assigned']} {$row['event_description']}";
}
}
?></td>
<td> <?php //this bit is not working. need to iterate through array result $r to filter appropriate events here
while($row = mysqli_fetch_assoc($r)) {
if (($row["event_type"] == 'GCRSA Championship') && ($row["event_date_start"] == $day)){
echo "{$row['swim_club_assigned']} {$row['event_description']}";
}
}
?></td>
<?php
for($j =1;$j<=5;$j++){
echo "<td></td>".PHP_EOL;
}
?>
</tr>
<?php
$day = date('Y-m-d', strtotime('+1 day', strtotime($day)));
}
?>
</tbody>
答案 0 :(得分:0)
首先是代码段/文件,然后是说明和详细信息...
<style>
.table-bordered {border: solid 1px grey;}
thead tr th {border: solid 1px blue;}
tbody tr td {border: solid 1px green;}
.Sday {background-color: red;}
</style>
<?php
if (!$conn = new mysqli("localhost", "root","","db")) {
echo "Database Connection Error"; // $conn->connect_error
} else {
// Generate derived table comprised of united hardcoded dates for the current month with minimal function calls
$ym = date('Y-m-');
$derived_table = '';
for ($d = 1, $end = date('t'); $d <= $end; ++$d) {
$date = $ym . str_pad($d, 2, 0, STR_PAD_LEFT); // format the date without calling strtotime() & date() each iteration
if ($d == 1) {
$derived_table .= "SELECT '$date' AS event_date_start"; // only need to name the first column
} else {
$derived_table .= " UNION SELECT '$date'";
}
}
$query = "SELECT
DATE_FORMAT(A.event_date_start, '%b %e, %Y') AS Date,
DAYNAME(A.event_date_start) AS Day,
GROUP_CONCAT(
IF(event_type = 'GCRSA Club Meet', CONCAT(swim_club_assigned, ' ', event_description), null)
SEPARATOR '<br>'
) AS `Club Meet`,
GROUP_CONCAT(
IF(event_type = 'GCRSA Championship', CONCAT(swim_club_assigned, ' ', event_description), null)
SEPARATOR '<br>'
) AS `Championship`
FROM ($derived_table) A
LEFT JOIN eventcalendar B ON A.event_date_start = B.event_date_start
GROUP BY A.event_date_start";
if (!$result = $conn->query($query)) {
echo "Syntax Error"; // $conn->error
} else {
?>
<table class="table table-bordered">
<thead>
<tr>
<th rowspan="2" colspan="2">Date</th>
<th colspan="3">GCRSA Region</th>
<th colspan="2">State, SAL, Intl</th>
<th colspan="2">Other events</th>
</tr>
<tr style="border-bottom: 1pt Darkblue;">
<th>Club meet</th>
<th>Championship</th>
<th>Development</th>
<th>SQ</th>
<th>Sal</th>
<th>School Meets</th>
<th>Qld School Holidays</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td class=\"{$row['Day'][0]}day\">{$row['Date']}</td>";
echo "<td class=\"{$row['Day'][0]}day\">{$row['Day']}</td>";
echo "<td>{$row['Club Meet']}</td>";
echo "<td>{$row['Championship']}</td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
}
?>
</tbody>
</table>
<?php
}
}
SQL查询是此脚本的主力。通过生成一个“派生表”,其中包含当月的所有日期(按顺序),然后向左联接您的实际数据库表数据并在event_date_start
上进行分组,可以将相似数据分组在一起,并进行充分准备,并提供严格的结果集。
使用{$row['Day'][0]}day
注意我的班级/ css技巧<-[0]
的意思是“仅显示第一个字符。这使得类属性如Mday
,Fday
,等等。为避免有条件检查,我将Sday
分配给星期六和星期日,以便样式简单。
如果您想知道本月呈现的查询是什么样的:
在开发此解决方案时,我没有费心添加未使用的列,但这是我用来测试的数据:
CREATE TABLE IF NOT EXISTS `eventcalendar` (
`event_description` varchar(255) NOT NULL,
`event_date_start` date NOT NULL,
`event_date_end` date NOT NULL,
`event_type` varchar(255) NOT NULL,
`swim_club_assigned` varchar(255) NOT NULL,
PRIMARY KEY (`event_description`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `eventcalendar` (`event_description`, `event_date_start`, `event_date_end`, `event_type`, `swim_club_assigned`) VALUES
('Another Championship one!', '2018-10-11', '2018-10-11', 'GCRSA Championship', 'My Club 2'),
('Another one!', '2018-10-11', '2018-10-11', 'GCRSA Club Meet', 'My Club'),
('Dunno Invitational - Template: Z', '2018-11-12', '2018-11-12', 'GCRSA Championship', ''),
('Preparation Meet (LC) - Template: A', '2018-10-01', '2018-10-01', 'GCRSA Club Meet', 'Gold Coast North SC Inc'),
('Reg Winter Championships (SC) - Template: XY', '2018-10-31', '2018-10-31', 'GCRSA Championship', ''),
('Sprint Meet (LC) - Template: H', '2018-10-11', '2018-10-11', 'GCRSA Club Meet', 'Bond SC Inc');
这就是它的作用: