-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_seats.php
More file actions
65 lines (56 loc) · 2.22 KB
/
Copy pathsetup_seats.php
File metadata and controls
65 lines (56 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
require_once 'db.php';
$conn = getDbConnection();
// 1. Add capacity to bus table if not exists
$check = $conn->query("SHOW COLUMNS FROM bus LIKE 'capacity'");
if ($check->num_rows === 0) {
if ($conn->query("ALTER TABLE bus ADD COLUMN capacity INT DEFAULT 36")) {
echo "Added 'capacity' column to 'bus' table.\n";
} else {
echo "Error adding column: " . $conn->error . "\n";
}
} else {
echo "'capacity' column already exists.\n";
}
// 2. Create bookings table
$sql = "CREATE TABLE IF NOT EXISTS bookings (
booking_id INT AUTO_INCREMENT PRIMARY KEY,
bus_id INT NOT NULL,
user_id INT NOT NULL,
seat_number VARCHAR(10) NOT NULL,
booking_date DATE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (bus_id) REFERENCES bus(bus_id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
UNIQUE KEY unique_booking (bus_id, seat_number, booking_date)
)";
if ($conn->query($sql)) {
echo "Table 'bookings' created or already exists.\n";
} else {
echo "Error creating table: " . $conn->error . "\n";
}
// 3. Populate Random Capacities & Dummy Bookings for Today
$buses = $conn->query("SELECT bus_id FROM bus");
while ($bus = $buses->fetch_assoc()) {
$id = $bus['bus_id'];
// Random Capacity (30, 36, or 40)
$cap = [30, 36, 40][array_rand([30, 36, 40])];
$conn->query("UPDATE bus SET capacity = $cap WHERE bus_id = $id");
// Random Pre-booked Seats for Today
$seats_booked = rand(5, 15); // Book 5-15 seats randomly
$today = date('Y-m-d');
for ($i = 0; $i < $seats_booked; $i++) {
// Generate random seat (e.g., A1, B3)
$cols = ['A', 'B', 'C', 'D']; // 2+2 layout typically has 4 columns
$rows = ceil($cap / 4);
$col = $cols[array_rand($cols)];
$row = rand(1, $rows);
$seat = $col . $row;
// Try to insert dummy booking (ignore duplicates due to UNIQUE constraint)
// User ID 1 is assumed to exist (usually admin) or use a valid ID
$conn->query("INSERT IGNORE INTO bookings (bus_id, user_id, seat_number, booking_date)
VALUES ($id, 1, '$seat', '$today')");
}
}
echo "Database setup and population complete.\n";
?>