-
Notifications
You must be signed in to change notification settings - Fork 2
/
datafill.php
65 lines (52 loc) · 2.68 KB
/
datafill.php
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
// Connect to the DB
$pdo = null;
$dsn = "mysql:host=localhost;dbname=emanamba;";
$options = [
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
];
try {
$pdo = new PDO($dsn, "root", "", $options);
}catch( PDOException $e) {
error_log($e->getMessage());
exit('Something went wrong. Try again');
}finally{
echo "Connected!<br>";
}
// Fill the DB with sample data
$towns_array = ["Bomet","Bondo","Bujumbura","Bumala","Bungoma","Busia","Chalinze","Dar Es Salaam","Eldoret","Garissa","Goma","Harare",
"Homa Bay","Iganga","Isiolo","Jinja","Juba","Kabale","Kagitumba","Kaimosi","Kakamega","Kampala","Kanyaru","Kapsabet","Katuna","Kericho",
"Kigali","Kilifi","Kisii","Kisoro","Kisumu","Kitale","Kitui","Lamu","Lilongwe","Lodwar","Lokichoggio","Luanda","Lubumbashi","Lusaka",
"Machakos","Malaba","Malindi","Maseno","Matayos","Maua","Mbale","Mbarara","Meru","Migori","Mombasa","Mombo","Moshi","Moyale","Mumias",
"Nairobi","Naivasha","Nakuru","Namanga","Narok","Ntugamo","Oyugis","Same","Segera","Siaya","Sirare","Tanga","Taveta","Ugunja","Voi","Webuye"];
$capacity1 = rand(16,45);
$capacity2 = rand(16,45);
// Loop through all towns, create at least one route for each pair
for( $i = 0; $i < 10; ++$i ) {
$departTown = $towns_array[$i];
for( $j = $i+1; $j < 11; ++$j ) {
$destinationTown = $towns_array[$j];
$departureDate = "2018-06-13";
$returnDate1 = "2018-06-" . (string)rand(18,29);
$returnDate2 = "2018-06-" . (string)rand(18,29);
$plate1 = getPlate();
$plate2 = getPlate();
// Populate the db
$query = 'INSERT INTO `buses`(`regPlate`, `capacity`, `departureTown`, `departureDate`, `destinationTown`, `returnDate`) VALUES (?,?,?,?,?,?)';
$stmt = $pdo->prepare($query);
$response = $stmt->execute([$plate1, $capacity1, $departTown, $departureDate, $destinationTown, $returnDate1]);
$response2 = $stmt->execute([$plate2, $capacity2, $departTown, $departureDate, $destinationTown, $returnDate2]);
if( !$response ) {
echo "Run into a wall";
}
}
}
echo "All done!";
function getPlate() {
$letters = ['A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'];
$len = sizeof( $letters ) - 1;
return $plate = "KB" . $letters[rand(0, $len)] . " " . (string)rand(0,9) . (string)rand(0,9) . (string)rand(0,9) . $letters[rand(0, $len)];
}
?>