connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}
// SQL query to fetch ‘flat_num’, ‘flat_id’, ‘building_id’, building ‘name’, and ‘address’
$sql = ”
SELECT f.flat_num, f.id as flat_id, f.building_id, fr.status, b.name as building_name, b.address
FROM flat f
LEFT JOIN flat_registration fr ON f.id = fr.flat_id
LEFT JOIN building b ON f.building_id = b.id
ORDER BY f.building_id, f.flat_num”; // Order by building_id and then by flat_num
$result = $conn->query($sql);
// Initialize an array to hold plot data grouped by building_id
$plots = [];
if ($result->num_rows > 0) {
// Fetch each plot’s data
while ($row = $result->fetch_assoc()) {
$building_id = $row[‘building_id’];
if (!isset($plots[$building_id])) {
$plots[$building_id] = [
‘building_name’ => $row[‘building_name’],
‘address’ => $row[‘address’],
‘plots’ => [] // Initialize an array for plots
];
}
$plots[$building_id][‘plots’][$row[‘flat_num’]] = [
‘flat_id’ => $row[‘flat_id’], // Store flat_id
‘booked’ => isset($row[‘status’]) && $row[‘status’] == 1 // Status 1 means booked
];
}
} else {
echo “No results found.”;
}
$conn->close();
?>
$buildingData) {
echo “
“;
echo “
“;
echo “
// Sort plots by flat_num to maintain sequence
ksort($buildingData[‘plots’]);
// foreach ($buildingData[‘plots’] as $plotNo => $plotData) {
// $status = $plotData[‘booked’] ? ‘booked’ : ‘available’;
// $text = $plotData[‘booked’] ? ‘Already Booked’ : ‘Plot No. ‘ . $plotNo;
// // $flatIdText = ‘Plot ID: ‘ . $plotData[‘flat_id’]; // Text for flat_id
// echo “
“;
// }
foreach ($buildingData[‘plots’] as $plotNo => $plotData) {
$status = $plotData[‘booked’] ? ‘booked’ : ‘available’;
// Modify this line to include the plot number even when booked
$text = $plotData[‘booked’] ? ‘Plot No. ‘ . $plotNo . ‘ – Already Booked’ : ‘Plot No. ‘ . $plotNo;
$flatIdText = ‘Plot ID: ‘ . $plotData[‘flat_id’]; // Text for flat_id
echo “
“;
}
echo “
“; // Close the grid-container and building-group
}
?>