Coding
Coding
VOLATILE PRICING IN
FOOD DELIVERY SYSTEM
(Implementation and Coding)
DATABASE
CODING
Login
<?php
session_start();
include './db_connect.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="author" content="Kodinger">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Yummie Wheels</title>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9J
voRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="./css/my-login.css">
<link rel="stylesheet" href="./css/style.css">
</head>
<body class="my-login-page log">
<div class="nav">
<div class="menu-wrap">
<a href="./index.php">
<div class="logo">
Yummie Wheels
</div>
</a>
<div class="menu h-xs">
<a href="./index.php">
<div class="menu-item">
Home
</div>
</a>
<a href="./res.php">
<div class="menu-item">
Restaurants
</div>
</a>
<a href="./order.php">
<div class="menu-item">
Orders
</div>
</a>
<a href="./login.php">
<div class="menu-item active">
<?php
if(!isset($_SESSION['uid'])){
echo "Login";
}
else{
echo "Profile";
}
?>
</div>
</a>
</div>
<div class="right-menu">
<div class="cart-btn">
<i class='bx bx-cart-alt'></i>
</div>
</div>
</div>
</div>
<section class="h-100">
<div class="container h-100">
<div class="row justify-content-md-center h-100">
<div class="card-wrapper">
<div class="card-fat">
<div class="card-body">
<?php
if(!isset($_SESSION['uid'])){
echo<<<login
<h4 class="card-title">Login</h4>
<form method="POST" class="login-validate.php" action="./login-
validate.php">
<div class="form-group">
<label for="email">E-Mail Address</label>
<input id="email" type="email" class="form-control" name="email"
value="" required autofocus>
<div class="invalid-feedback">
Email is invalid
</div>
</div>
<div class="form-group">
<label for="password">Password
</label>
<input id="password" type="password" class="form-control"
name="password" required data-eye>
<div class="invalid-feedback">
Password is required
</div>
</div>
<div class="form-group m-0">
<button type="submit" class="btn btn-primary btn-block">
Login
</button>
</div>
<div class="mt-4 text-center">
Don't have an account? <a href="./register.php">Create One</a>
</div>
</form>
</div>
login;
}
else{
$user_id=$_SESSION['uid'];
$sql="select * from Users where user_id=$user_id";
$result=mysqli_query($conn, $sql);
$row=mysqli_fetch_assoc($result);
$name=$row["name"];
$email=$row["email"];
$ph=$row["phone"];
$sqla="SELECT * FROM `Address` where user_id=$user_id";
$addrow=mysqli_query($conn, $sqla);
$ar=mysqli_fetch_assoc($addrow);
$state=$ar["state"];
$city=$ar["city"];
$street=$ar["street"];
$pin=$ar["pincode"];
echo<<<in
<h2 style="text-align:center;">Welcome, $name</h2>
<br>
<h5><strong>Email:</strong>{$email}.</br>
<strong>Phone Number:</strong>{$ph}.</br>
<strong>Address:</strong>{$street}.</br><strong>City:</strong>{$city}</
br><strong>State:</strong>{$state}</br><strong>Pincode:</strong>{$pin}
</h5>
<form action="login.php" method="post">
<input type="submit" name="logout" value="logout"
style="background-color: #3D7065;border-radius: 1.5rem;border:none; color:
white;padding: 16px 32px;text-decoration: none;margin: 4px 2px;
cursor: pointer;display: block;width: 100px;margin: 0 auto;">
</form>
in;
if(isset($_POST['logout'])){
session_destroy();
header('Location: ./login.php');
}
}
?>
</div>
</div>
</div>
</div>
</body>
</html>
login-validate
<?php
session_start();
include 'db_connect.php';
$email=$_POST['email'];
$pass=$_POST['password'];
$sqlc="SELECT * FROM `Users` where email='$email' and password='$pass'";
$res=mysqli_query($conn, $sqlc);
if (mysqli_num_rows($res) > 0) {
$sqlu="select user_id from Users where email='$email'";
$result=mysqli_query($conn, $sqlu);
$row=mysqli_fetch_assoc($result);
$u_id=$row["user_id"];
$_SESSION['uid']=$u_id;
header('Location: ./login.php');
}
else{
header('Location: ./error.php?no=2');
}
echo $_SESSION['active'];
?>
Assign-pricing-categories
<?php
// Include necessary files
require_once 'db_connect.php';
require_once 'pricing-categories.php';
// Initialize pricing manager
$dbConnection = $conn;
$pricingManager = new PricingCategoryManager($dbConnection);
$foods = array();
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
// Get restaurant name
$resId = $row['restaurant_id'];
$resNameSql = "SELECT name FROM Restaurants WHERE restaurant_id =
$resId";
$resNameResult = mysqli_query($conn, $resNameSql);
$resNameRow = mysqli_fetch_assoc($resNameResult);
$foods[] = array(
'name' => $row['item_name'],
'price' => $row['price'],
'restaurant' => $resNameRow['name']
);
}
}
return $foods;
}
// Function to get top 3 cheapest items
function getTopCheapestItems($conn, $limit = 3) {
$sql = "SELECT item_name, price, restaurant_id FROM Menu ORDER BY price ASC
LIMIT $limit";
$result = mysqli_query($conn, $sql);
$foods = array();
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
// Get restaurant name
$resId = $row['restaurant_id'];
$resNameSql = "SELECT name FROM Restaurants WHERE restaurant_id =
$resId";
$resNameResult = mysqli_query($conn, $resNameSql);
$resNameRow = mysqli_fetch_assoc($resNameResult);
$foods[] = array(
'name' => $row['item_name'],
'price' => $row['price'],
'restaurant' => $resNameRow['name'],
'highlight' => true
);
}
}
return $foods;
}
// Function to filter foods by restaurant and budget (using restaurant as category)
function filterFoodsByRestaurantAndBudget($conn, $restaurantId, $budget) {
$sql = "SELECT item_name, price, restaurant_id FROM Menu
WHERE restaurant_id = $restaurantId AND price <= $budget
ORDER BY price ASC";
$result = mysqli_query($conn, $sql);
$foods = array();
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
// Get restaurant name
$resId = $row['restaurant_id'];
$resNameSql = "SELECT name FROM Restaurants WHERE restaurant_id =
$resId";
$resNameResult = mysqli_query($conn, $resNameSql);
$resNameRow = mysqli_fetch_assoc($resNameResult);
$foods[] = array(
'name' => $row['item_name'],
'price' => $row['price'],
'restaurant' => $resNameRow['name']
);
}
}
return $foods;
}
// Get all restaurants as categories
function getRestaurants($conn) {
$sql = "SELECT restaurant_id, name FROM Restaurants ORDER BY name";
$result = mysqli_query($conn, $sql);
$restaurants = array();
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$restaurants[$row['restaurant_id']] = $row['name'];
}
}
return $restaurants;
}
// Handle form submissions
$budget = isset($_POST['budget']) ? (float)$_POST['budget'] : 0;
$restaurantId = isset($_POST['restaurant_id']) ? (int)$_POST['restaurant_id'] : 0;
$action = isset($_POST['action']) ? $_POST['action'] : '';
$filteredFoods = array();
$cheapestItems = array();
$categoryFoods = array();
$restaurants = array();
if ($action == 'filter_by_budget' && $budget > 0) {
$filteredFoods = filterFoodsByBudget($conn, $budget);
} elseif ($action == 'cheapest_items') {
$cheapestItems = getTopCheapestItems($conn);
} elseif ($action == 'filter_by_restaurant' && $restaurantId > 0 && $budget > 0) {
$categoryFoods = filterFoodsByRestaurantAndBudget($conn, $restaurantId, $budget);
}
// Get restaurants for dropdown
$restaurants = getRestaurants($conn);
// Find selected restaurant name
$selectedRestaurantName = '';
if ($restaurantId > 0 && isset($restaurants[$restaurantId])) {
$selectedRestaurantName = $restaurants[$restaurantId];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Budget Food Finder - Yummie Wheels</title>
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?
family=Poppins:wght@200;300;400;500;600;700;800;900&display=swap"
rel="stylesheet">
<link href='https://unpkg.com/[email protected]/css/boxicons.min.css' rel='stylesheet'>
<link rel="stylesheet" href="css/style.css">
<style>
.budget-container {
padding: 20px;
background-color: var(--secondary-color);
border-radius: 10px;
margin-bottom: 20px;
}
.budget-form {
display: flex;
flex-direction: column;
gap: 15px;
margin-bottom: 20px;
}
.input-group {
display: flex;
gap: 10px;
}
.budget-results {
background-color: rgba(255, 255, 255, 0.1);
padding: 15px;
border-radius: 8px;
}
.food-item-budget {
display: flex;
justify-content: space-between;
padding: 10px;
border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}
.food-item-budget:last-child {
border-bottom: none;
}
.highlighted {
background-color: var(--third-color);
border-radius: 5px;
}
.activities-container {
display: flex;
flex-wrap: wrap;
gap: 20px;
margin-bottom: 20px;
}
.activity-card {
background-color: var(--secondary-color);
border-radius: 10px;
padding: 15px;
flex: 1;
min-width: 250px;
cursor: pointer;
transition: transform 0.3s;
}
.activity-card:hover {
transform: translateY(-5px);
}
.activity-card h3 {
color: var(--third-color);
margin-bottom: 10px;
}
Shortest path
<?php
include 'db_connect.php'; // Database connection
/**
* Creates a more realistic alternative path with multiple waypoints
* @param float $start_lat Starting latitude (restaurant)
* @param float $start_lon Starting longitude (restaurant)
* @param float $end_lat Ending latitude (customer)
* @param float $end_lon Ending longitude (customer)
* @return array Path information with waypoints
*/
function haversine($lat1, $lon1, $lat2, $lon2) {
$earth_radius = 6371; // Radius of the earth in km
$dLat = deg2rad($lat2 - $lat1);
$dLon = deg2rad($lon2 - $lon1);
$a = sin($dLat / 2) * sin($dLat / 2) +
cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *
sin($dLon / 2) * sin($dLon / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
return $earth_radius * $c; // Distance in km
}
function dijkstra($graph, $start, $end) {
$distances = [];
$previous = [];
$queue = new SplPriorityQueue();
foreach ($graph as $node => $edges) {
$distances[$node] = INF;
$previous[$node] = null;
$queue->insert($node, INF);
}
$distances[$start] = 0;
$queue->insert($start, 0);
while (!$queue->isEmpty()) {
$current = $queue->extract();
if ($current === $end) {
break;
}
foreach ($graph[$current] as $neighbor => $cost) {
$alt = $distances[$current] + $cost;
if ($alt < $distances[$neighbor]) {
$distances[$neighbor] = $alt;
$previous[$neighbor] = $current;
$queue->insert($neighbor, $alt);
}
}
}
// Reconstruct shortest path
$path = [];
$node = $end;
while ($node !== null) {
array_unshift($path, $node);
$node = $previous[$node];
}
return [
'path' => $path,
'distance' => $distances[$end]
];
}
function alternativePath($start_lat, $start_lon, $end_lat, $end_lon) {
// Calculate the direct distance
$direct_distance = haversine($start_lat, $start_lon, $end_lat, $end_lon);
// Vector from start to end
$dx = $end_lat - $start_lat;
$dy = $end_lon - $start_lon;
// Generate 2-3 waypoints for a realistic alternative route
$waypoints = [];
// Calculate perpendicular offsets to create a curved path
// First waypoint - offset in one direction
$wp1_lat = $start_lat + ($dx * 0.3) + ($dy * 0.1);
$wp1_lon = $start_lon + ($dy * 0.3) - ($dx * 0.1);
$waypoints[] = ['lat' => $wp1_lat, 'lng' => $wp1_lon];
// Second waypoint - offset in the opposite direction
$wp2_lat = $start_lat + ($dx * 0.6) - ($dy * 0.1);
$wp2_lon = $start_lon + ($dy * 0.6) + ($dx * 0.1);
$waypoints[] = ['lat' => $wp2_lat, 'lng' => $wp2_lon];
// Calculate the total distance of this alternative path
$alt_distance = haversine($start_lat, $start_lon, $wp1_lat, $wp1_lon);
$alt_distance += haversine($wp1_lat, $wp1_lon, $wp2_lat, $wp2_lon);
$alt_distance += haversine($wp2_lat, $wp2_lon, $end_lat, $end_lon);
// Make the alternative slightly longer than the direct route (typically 10-20% longer)
$distance_ratio = $alt_distance / $direct_distance;
if ($distance_ratio < 1.1) {
// If our alternative isn't significantly longer, make it a bit longer
$alt_distance = $direct_distance * (1.1 + (mt_rand(5, 15) / 100));
}
return [
'path' => ['restaurant', 'waypoint1', 'waypoint2', 'user'],
'distance' => $alt_distance,
'waypoints' => [
'waypoint1' => ['lat' => $wp1_lat, 'lng' => $wp1_lon],
'waypoint2' => ['lat' => $wp2_lat, 'lng' => $wp2_lon]
]
];
}
// Debug information - log request parameters
error_log("shortest_path.php called with params: " . json_encode($_GET));
// Fetch restaurant and user locations
$restaurant_id = isset($_GET['restaurant_id']) ? intval($_GET['restaurant_id']) : 0;
$user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0;
// Handle direct coordinate input instead of IDs
if (isset($_GET['res_lat']) && isset($_GET['res_lng']) && isset($_GET['cust_lat']) &&
isset($_GET['cust_lng'])) {
$restaurant = [
'latitude' => floatval($_GET['res_lat']),
'longitude' => floatval($_GET['res_lng'])
];
$user = [
'latitude' => floatval($_GET['cust_lat']),
'longitude' => floatval($_GET['cust_lng'])
];
// Direct path graph
$graph = [
'restaurant' => ['user' => haversine($restaurant['latitude'], $restaurant['longitude'],
$user['latitude'], $user['longitude'])],
'user' => [] // End node
];
// Calculate shortest path
$shortest = dijkstra($graph, 'restaurant', 'user');