forked from divyamamgai/Naveta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchBuses.php
56 lines (54 loc) · 1.71 KB
/
searchBuses.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
<?php
function CheckForBuses($Source, $Destination, $AllStops, $DirectionFlag)
{
$CheckingNumber = 0;
$ReversedAllStops = $AllStops;
if ($DirectionFlag) {
$ReversedAllStops = array_reverse($AllStops);
}
foreach ($ReversedAllStops as $Stop) {
if (strcmp($Stop, $Source) == 0) {
$CheckingNumber++;
continue;
}
if ((strcmp($Stop, $Destination) == 0) && $CheckingNumber) {
$CheckingNumber++;
break;
}
}
if ($CheckingNumber == 2) {
return true;
}
return false;
}
function listAvailableBus()
{
$source = $_GET['SRC'];
$destination = $_GET['DSC'];
$dbuser = 'root';
$dbname = 'naveta';
$dbpass = '123456789';
$dbhost = 'localhost';
mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname) or die(mysql_error());
$query = "SELECT * FROM `bus`";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}
$Buses = array();
foreach ($data as $bus) {
$BusStops = explode("|", $bus['Stops']);
if (array_search($bus['CurrentStop'], $BusStops) == count($BusStops) - 1) {
} else if (array_search($bus['CurrentStop'], $BusStops) > array_search($source, $BusStops)) {
continue;
} else {
if (CheckForBuses($source, $destination, $BusStops, $bus['Direction'])) {
array_push($Buses,$bus);
}
}
}
echo json_encode($Buses);
}
listAvailableBus();
?>