So I am trying to display my database data into my html table but it doesn’t seem to actually display the data into the table. I want to be able to display each ID into the table for each time and day that is assigned to it. My roster format seems to work fine though and the only error I am getting is this:
Notice: Only variables should be passed by reference in C:xampphtdocs2024SAT_program-filesform.php on line 35
Here is some sample data that I have inside of my database:
id workingTimes workingDate secondID
12025 06, 08, 10, 12 2024-06-21 1010
12345 06, 08, 10, 12 2024-06-21 1010
It has the id, times (eg, 06 would be 06:00), dates (Y-m-d format), and a second ID that doesn’t need to be on the table whatsoever.
<?php
// Database connection parameters
$servername = "localhost";
$username = "my_username";
$password = "my_password";
$dbname = "staffroster";
try {
// Create a PDO connection
$pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// Set PDO to throw exceptions on error
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Determine start and end dates for the roster (next 4 weeks)
$startDate = date('Y-m-d');
$endDate = date('Y-m-d', strtotime('+28 days'));
// Define the specific times
$times = array(
'06:00', '08:00', '10:00', '12:00',
'14:00', '16:00', '18:00', '20:00',
'22:00', '00:00', '02:00', '04:00'
);
// Prepare SQL query to fetch staff working data within the specified date range and times
$stmt = $pdo->prepare("SELECT id, workingTimes, DATE_FORMAT(workingDate, '%Y-%m-%d') AS roster_date
FROM staffworking
WHERE workingDate BETWEEN :start_date AND :end_date
AND FIND_IN_SET(workingTimes, :times) > 0
ORDER BY roster_date, FIELD(workingTimes, :times)");
// Bind parameters
$stmt->bindParam(':start_date', $startDate, PDO::PARAM_STR);
$stmt->bindParam(':end_date', $endDate, PDO::PARAM_STR);
$stmt->bindParam(':times', implode(',', $times), PDO::PARAM_STR);
// Execute the query
$stmt->execute();
// Fetch all rows as associative array
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
// Print PDO exception message
echo "Connection failed: " . $e->getMessage();
die();
}
?>
<h2>Date Time Roster (<?php echo $startDate; ?> to <?php echo $endDate; ?>)</h2>
<table>
<thead>
<th class="time-header"></th> <!-- Empty corner -->
<?php
$currentDate = strtotime($startDate);
for ($i = 0; $i < 28; $i++) {
echo '<th>' . date('Y-m-d', $currentDate) . '</th>';
$currentDate = strtotime('+1 day', $currentDate);
}
?>
</tr>
</thead>
<tbody>
<?php foreach ($times as $time): ?>
<tr>
<td class="time-header"><?php echo $time; ?></td>
<?php
$currentDate = strtotime($startDate);
for ($i = 0; $i < 28; $i++) {
$currentDateString = date('Y-m-d', $currentDate);
echo '<td>';
foreach ($rows as $row) {
$workingTimes = explode(', ', $row['workingTimes']);
if (in_array($time, $workingTimes) && $row['roster_date'] === $currentDateString) {
echo htmlspecialchars($row['id']);
}
}
echo '</td>';
$currentDate = strtotime('+1 day', $currentDate);
}
?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
// Close the PDO connection
$pdo = null;
?>
I want it do display the id inside time of the date it has been selected to work and the times on that date.
This is what the roster looks like currently with some basic CSS styling