I am trying to make a calendar that shows every day of the month and shows the event information for each day. Some days may contain multiple events. The trouble is I'm stuck trying to iterate the result set and group the multiple events into a single day row.
Here's a few sample rows:
Here is my code to retrieve array
$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); gives me the array below
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] =>
)
)
I want to be able to retrieve from the array (where the event type = $something
and the start date = $a_date
and then display the $swim_club
and the $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']}";
}
}
I have been trying to figure out how to pull the key data from the array for each row (there may be more than one event per start date in the array), but I can't figure out the syntax to retrieve the row from the array and then pull out the value from the key.
To be clear, it is a calendar I am trying to populate, and the results are to be inserted into a cell in the relevant date row. That's why I thought it best to use one query at the top and to then iterate through an array (as per the example above) at each cell matching the date of an event to the day of the relevant cell. The calendar that I am using is below (with comments to show the cell where I need to insert the data)
<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>