Create food ordering app - Foody app

Last updated : Oct 01, 2021

Create food ordering app - Foody app



In this post you will get to know about the idea of creating a food ordering and menu management system in PHP.

We will have some directories

  1. common : for creating api

  2. controllers : for all the controller files

  3. css : for css file

  4. database : for database files

  5. images : for image files

  6. js : for some scripting

 

Now the idea is we have a file named 'commons.php' which will be the api part. On every page for every request to server or request for fetching records, insert records, update records the flow will be view_file -> .js_file -> api -> result. From the view file the request will go to js file and then js file will include api calls for each request and then api after processing will return data in vice versa manner.

Let's get started

  1. First we will create api. So create dir named common and in the dir create file named commons.php and write the script below:

    <?php
    /*
    This file accepts all the post request from the js ajax calls
    and decides which condition to execute according to ActionToCall
    value.
    */
    # requiring files
    require_once(__DIR__.'/../controllers/CustomerController.php');
    require_once(__DIR__.'/../controllers/RestaurantController.php');
    require_once(__DIR__.'/../controllers/OrderController.php');
    require_once(__DIR__.'/../controllers/MenuController.php');
    # checking the request is POST and then checking the ActionToCall for
    # correct snippet precossing
    if($_SERVER['REQUEST_METHOD'] == "POST"){
        switch($_POST['ActionToCall']){
            case "customer_register" :
                $customer = new CustomerController();
                echo $customer->customer_register($_POST);
            break;
            
            case "restaurant_register" :                
                $restaurant = new RestaurantController();
                echo $restaurant->restaurant_register($_POST);
            break;
    
            case "login" :                
                if($_POST['user_type'] == "customer"){
                    $customer = new CustomerController();
                    echo $customer->customer_login($_POST);
                }
                else if($_POST['user_type'] == "restaurant"){                    
                    $restaurant = new RestaurantController();
                    echo $restaurant->restaurant_login($_POST);
                }
            break;
    
            case "customer_profile_update" :                
                $customer = new CustomerController();
                echo $customer->update($_POST);
            break;
    
            case "add_menu_item" :                
                $menu = new MenuController();
                echo $menu->add_menu_item($_POST);
            break;
    
            case "place_order" :             
                $new_order = new OrderController();
                echo $new_order->place_order($_POST);
            break;
    
            case "update_menu_item" :             
                $update_order = new MenuController();
                echo $update_order->update_menu_item($_POST);
            break;
        }
    }
    // if the request is get then the page is not accessible
    else if($_SERVER['REQUEST_METHOD'] === "GET"){
        echo "You are accessing the wrong page.";
        exit();
    }
    else{
        echo "You are accessing the wrong page.";
        exit();
    }
    ?>

  2. Create a dir named as 'controllers' for creating our controller files. In this dir create file named 'CustomerController.php' and write script below:

    <?php
        /* This is customer controller to handle all the customer related queries. */
    
        require_once(__DIR__.'/../database/dbs_controls.php');
    
        class CustomerController {
    
            // table name in mysql database
            private $table_name = "customer";
    
    
            // customer login method
            public function customer_login($data){
                $email = $data['email'];
                $password = sha1($data['password']);
    
                $dbs = new DatabaseControls();
                $qry = "select * from ".$this->table_name." where c_email = '".$email."' and c_password = '".$password."'";
                $result = $dbs->run_select_qry($qry);
                
                if($result['status']){
                    $session_val_arr = array(
                        "c_id"=>$result['extra_data']['c_id'],
                        "c_name"=>$result['extra_data']['c_name'],
                        "c_food_preference"=>$result['extra_data']['c_food_preference'],
                        "c_country_code"=>$result['extra_data']['c_country_code'],
                        "c_cont_no"=>$result['extra_data']['c_cont_no'],
                        "c_email"=>$result['extra_data']['c_email'],
                        "user_type"=>"customer"
                    );
                    session_start();
                    foreach($session_val_arr as $key=>$value){
                        $_SESSION[$key] = $value;
                    }
                }
                return json_encode($result);
            }
    
            // customer register/sign up method
            public function customer_register($request_data){
                
                $dbs = new DatabaseControls();
                
                $newid = $dbs->getLastId($this->table_name,'c_id') + 1;
    
                //getting data from $request_data
                $c_name = $request_data['c_name'];
                $c_email = $request_data['c_email'];
                $c_cont_no = $request_data['c_cont_no'];
                $c_pass = sha1($request_data['c_password']);
                $c_food_pref = $request_data['c_food_pref'];
                $c_cty_code = "+91";
                $qry = "insert into ".$this->table_name." values ('".$newid."','".$c_name."','".$c_email."','".$c_cty_code."','".$c_cont_no."','".$c_pass."','".$c_food_pref."')";
                
                $result = $dbs->run_insert_qry($qry);
                return json_encode($result);
            }
    
            // customer update details method
            public function update($request_data){
                $c_id = $request_data['c_id'];
    
                //getting data from $request_data
                $c_name = $request_data['c_name'];
                $c_cont_no = $request_data['c_cont_no'];
                $c_food_pref = $request_data['c_food_pref'];
    
                $dbs = new DatabaseControls();
                $qry = "update ".$this->table_name." set c_name = '".$c_name."',c_cont_no = '".$c_cont_no."',c_food_preference = '".$c_food_pref."' where c_id = ".$c_id;
                $result = $dbs->run_update_qry($qry);        
                
                
                return json_encode($result);
            }
        }
    ?>

  3. Create a dir named as 'controllers' for creating our controller files. In this dir create file named 'CustomerController.php' and write script below:

    <?php
        /*
        This is menu controller to handle all the menu related queries.
        */
        require_once(__DIR__.'/../database/dbs_controls.php');
    
        class MenuController {
    
            // table name in mysql database
            private $table_name = "menu_item";
    
            // this method will return menu
            public function get_menu($r_id){
                $dbs = new DatabaseControls();
                $qry = "select * from ".$this->table_name." where r_id = ".$r_id." order by item_id";
                $result = $dbs->run_multi_data_select_qry($qry);
                return $result;
            }
    
            // add menu items
            public function add_menu_item($request_data){
                $return_arr = [];
                $item_num = 0;
                $r_id = $request_data['r_id'];
                // getting the no of array items from request data to run
                // the loop to run queries
                $max_size = count($request_data['menu_item_name']);
    
                // the loop
                for($i=0;$i < $max_size;$i++){
                    $name = $_POST['menu_item_name'][$i];
                    $price = $_POST['menu_item_price'][$i];
                    $measurment = $_POST['menu_item_measurment'][$i];
                    $type = $_POST['menu_item_type'][$i];
                    $dbs = new DatabaseControls();
                    // getting last ids for inserting new record
                    $newid = $dbs->getLastId($this->table_name,'item_id') + 1;
                    // issuing queries
                    $qry = "insert into menu_item values(".$newid.",".$r_id.",'".$name."','".$type."','".$measurment."','".$price."')";
                    $result = $dbs->run_insert_qry($qry);
                    //echo $qry;
                    if($result['status'] == true){$item_num++;}
                    $return_arr[] = $result;
                }
    
                $result = array(
                    "status"=>true,
                    "msg"=>"Succssfully added ".count($return_arr)." item(s)",
                    "extra_data"=>$return_arr
                );
                return json_encode($result);
            }
    
            // update menu items
            public function update_menu_item($req_data){
                $item_id = $req_data['item_id'];
                $item_name = $req_data['item_name'];
                $item_type = $req_data['item_type'];
                $item_price = $req_data['item_price'];
                $item_measurment = $req_data['item_measurment'];
                $r_id = $req_data['r_id'];
                $qry = "update ".$this->table_name." set item_name='".$item_name."',item_type='".$item_type."',item_price='".$item_price."',item_measurment='".$item_measurment."' where item_id=".$item_id." and r_id=".$r_id;
                $dbs = new DatabaseControls();
                $result = $dbs->run_update_qry($qry);
                
                return json_encode($result);
            }
        }
    ?>

  4. Create a dir named as 'controllers' for creating our controller files. In this dir create file named 'OrderController.php' and write script below:

    <?php
        /*
        This is order controller to handle all the orders related queries.
        */
        require_once(__DIR__.'/../database/dbs_controls.php');
    
        class OrderController {
            // table name in mysql database
            private $table_name = "orders";
    
            // return all orders for restaurant
            public function get_orders_for_restaurant($r_id){
                $dbs = new DatabaseControls();
                $qry = "select * from ".$this->table_name.",customer where orders.r_id = ".$r_id." and orders.c_id = customer.c_id order by o_id desc";
                $result = $dbs->run_multi_data_select_qry($qry);
                return $result;
            }
    
            // return all orders for customers
            public function get_orders_for_customer($c_id){
                $dbs = new DatabaseControls();
                $qry = "select * from ".$this->table_name.",restaurant where orders.c_id = ".$c_id." and orders.r_id = restaurant.r_id order by o_id desc";
                $result = $dbs->run_multi_data_select_qry($qry);
                return $result;
            }
    
            // place order
            public function place_order($req_data){
                $dbs = new DatabaseControls();
                
                $new_id = $dbs->getLastId($this->table_name,'o_id') + 1;
                $c_id = $req_data['c_id'];
                $r_id = $req_data['r_id'];
                $order_desc = substr($req_data['order_desc'],1,(strlen($req_data['order_desc'])-2));
                date_default_timezone_set('Asia/Kolkata');
                $order_date = date('Y-m-d H:i:s');
                $order_amt = $req_data['order_amt'];
    
                $qry = "insert into ".$this->table_name." values(".$new_id.",".$c_id.",".$r_id.",'".$order_desc."','".$order_date."',".$order_amt.")";
                $result = $dbs->run_insert_qry($qry);
    
                return json_encode($result);
            }
        }
    
    ?>

  5. Create a dir named as 'controllers' for creating our controller files. In this dir create file named 'RestaurantController.php' and write script below:

    <?php
        /*
        This is restaurant controller to handle all the restaurant related queries.
        */
    
        require_once(__DIR__.'/../database/dbs_controls.php');
    
        class RestaurantController {
            // table name in mysql database
            private $table_name = "restaurant";
    
            // login
            public function restaurant_login($data){
                $email = $data['email'];
                $password = sha1($data['password']);
    
                $dbs = new DatabaseControls();
                $qry = "select * from ".$this->table_name." where r_email = '".$email."' and r_password = '".$password."'";
                $result = $dbs->run_select_qry($qry);
                
                if($result['status']){
    
                    $session_val_arr = array(
                        "r_id"=>$result['extra_data']['r_id'],
                        "r_name"=>$result['extra_data']['r_name'],
                        "r_email"=>$result['extra_data']['r_email'],
                        "r_address"=>$result['extra_data']['r_address'],
                        "r_food_preference"=>$result['extra_data']['r_food_preference'],
                        "r_country_code"=>$result['extra_data']['r_country_code'],
                        "r_cont_no"=>$result['extra_data']['r_cont_no'],
                        "user_type"=>"restaurant"
                    );
                    session_start();
                    foreach($session_val_arr as $key=>$value){
                        $_SESSION[$key] = $value;
                    }
                }
                return json_encode($result);
            }
    
            // sign up/register
            public function restaurant_register($request_data){
                $dbs = new DatabaseControls();
                
                $newid = $dbs->getLastId($this->table_name,'r_id') + 1;
    
                //getting data from $request_data
                $r_name = $request_data['r_name'];
                $r_email = $request_data['r_email'];
                $r_cont_no = $request_data['r_cont_no'];
                $r_pass = sha1($request_data['r_password']);
                $r_address = $request_data['r_address'];
                $r_food_pref = $request_data['r_food_pref'];
                $r_cty_code = "+91";
                $qry = "insert into ".$this->table_name." values ('".$newid."','".$r_name."','".$r_address."','".$r_email."','".$r_cty_code."','".$r_cont_no."','".$r_pass."','".$r_food_pref."')";
                
                $result = $dbs->run_insert_qry($qry);
                
                return json_encode($result);
            }
    
            // getting all restaurants
            public function get_all_restaurant(){
                $dbs = new DatabaseControls();
                $qry = "select * from ".$this->table_name;
                $result = $dbs->run_multi_data_select_qry($qry);
                return json_encode($result);
            }
        }
    ?>

  6. It's time to add css file. So create a dir named 'css' in main dir. In the css dir create file named 'app.css' and write script below:

    .navbar{
                background-color: rgb(4, 212, 66);
            }
            .nav-link{
                color: white;
                background-color: yellowgreen;
            }
            
            .navbar-toggler{    
                color: white;
                background-color: yellowgreen;
            }
            
            .nav-link:hover{
                color:brown;
                background-color: yellow;
            }
            
            .navbar-brand{
                color: white;
            }
            .dropdown-menu{
                color: white;
                background-color: yellowgreen;
            }
            .dropdown-item{
                color: white;
                background-color: yellowgreen;
            }
            .dropdown-item:hover{
                color:brown;
                background-color: yellow;
            }
            .btn{
                background-color: rgb(4, 212, 66);
                color:white;
            }
            .btn:hover{
                background-color: rgb(174, 212, 4);
                color:white;
            }
            .card{
                margin:15px 5px;box-shadow: 0 0 10px rgba(0,0,0,0.12);
            }

  7. Then in mail dir create dir named 'database' and create a file named 'dbs_controls.php' in that dir and write below:

    
            <?php
    
    /*
    This is database controller file to handle all the queries.
    */
    class DatabaseControls {
        // variables for database
        private $host;
        private $user;
        private $password;
        private $database_name;
        private $connection;
        private $rset;
    
        public function __construct(){
            $this->host = "localhost";
            $this->user = "root";
            $this->password = "";
            $this->database_name = "foodshala_base";
            $this->connection = "";
            $this->rset = "";
        }
    
        // connect to database
        public function connect_to_database(){
            $this->connection = mysqli_connect(
                $this->host,
                $this->user,
                $this->password,
                $this->database_name
            );
        }
    
        
        // return the last id according to table and colun given
        public function getLastId($table_name, $col_name) : int {
            $lastid = 0;
            $this->connect_to_database();
            $this->rset = mysqli_query($this->connection,"select ".$col_name." from ".$table_name." order by ".$col_name);
            while($res = mysqli_fetch_assoc($this->rset)){
                $lastid = $res[$col_name];
            }
            $this->close_database_connection();
            return $lastid;
        }
    
        // dynamic insert query method
        public function run_insert_qry($qry) : Array{
            $ret_arr = [];
            $this->connect_to_database();
            $this->rset = mysqli_query($this->connection, $qry);
            
            if($this->rset){
                $ret_arr = array(
                    "status"=>true,
                    "msg"=>"success"
                );
            }
            else{
                $ret_arr = array(
                    "status"=>false,
                    "msg"=>"error"
                );
            }
            $this->close_database_connection();
            return $ret_arr;
        }
    
        // dynamic update query method
        public function run_update_qry($qry) : Array{
            $ret_arr = [];
            $this->connect_to_database();
            $this->rset = mysqli_query($this->connection, $qry);
            
            if($this->rset){
                $ret_arr = array(
                    "status"=>true,
                    "msg"=>"success"
                );
            }
            else{
                $ret_arr = array(
                    "status"=>false,
                    "msg"=>"error"
                );
            }
            $this->close_database_connection();
            return $ret_arr;
        }
    
        // dynamic select query for sinle row data return
        public function run_select_qry($qry) : Array{
            $ret_arr = [];
            $this->connect_to_database();
            $this->rset = mysqli_query($this->connection, $qry);
            $ret_data_arr = mysqli_fetch_assoc($this->rset);
            
            if(!empty($ret_data_arr)){
                $ret_arr = array(
                    "status"=>true,
                    "msg"=>"success",
                    "extra_data"=>$ret_data_arr
                );
            }
            else{
                $ret_arr = array(
                    "status"=>false,
                    "msg"=>"Data not found"
                );
            }
            $this->close_database_connection();
            return $ret_arr;
        }
    
        // dynamic select query for multiple data return
        public function run_multi_data_select_qry($qry) : Array{
            $ret_arr = [];
            $this->connect_to_database();
            $this->rset = mysqli_query($this->connection, $qry);
            while($return_data = mysqli_fetch_assoc($this->rset)){
                $ret_data_arr[] = $return_data;
            }
            
            if(!empty($ret_data_arr)){
                $ret_arr = array(
                    "status"=>true,
                    "msg"=>"success",
                    "extra_data"=>$ret_data_arr
                );
            }
            else{
                $ret_arr = array(
                    "status"=>false,
                    "msg"=>"no data found"
                );
            }
            $this->close_database_connection();
            return $ret_arr;
        }
    
        // closing connection to database to prevent connection pool
        public function close_database_connection(){
            if($this->connection){
                mysqli_close($this->connection);
            }
            else{
                return false;
            }
        }
    
        public function __destruct(){}
    }
    ?>

  8. In the main dir create dir named 'js' and create file named 'app.js' and write script below:

    var url_to_request = window.location.origin+"/foody/common/commons.php";
    
    if(document.getElementById('customer_register_frm') == null){}
    else{
        document.getElementById('customer_register_frm').addEventListener('submit',function(e){
            e.preventDefault();
            
            $.ajax({
                url:url_to_request,
                type:'POST',
                data        : new FormData(this), // our data object
                dataType    : 'json', // what type of data do we expect back from the server
                encode      : true,
                contentType: false,
                cache: false,
                processData:false,
                success:function(r){
                    //console.log(r);
                    if(r.status == true){
                        $("#customer_register_frm #success_msg_alert").html("Successfully registered");
                        $("#customer_register_frm #success_msg_alert").fadeIn();
                        setTimeout(function(){
                            $("#customer_register_frm #success_msg_alert").fadeOut();
                        },1300);
                        window.location.href=window.location.origin + "/foody/login.php";
                    }
                    else{
                        $("#customer_register_frm #error_msg_alert").html("Error. Try again later");
                        $("#customer_register_frm #error_msg_alert").fadeIn();
                        setTimeout(function(){
                            $("#customer_register_frm #error_msg_alert").fadeOut();
                        },1300);
                    }
                },
                error:function(err){ 
                    //console.error(err);               
                    $("#customer_register_frm #warning_msg_alert").html("Server internal error.");
                    $("#customer_register_frm #warning_msg_alert").fadeIn();
                    setTimeout(function(){
                        $("#customer_register_frm #warning_msg_alert").fadeOut();
                    },1300);
                }
            });
        });
    }
    
    if(document.getElementById('restaurant_register_frm') == null){}
    else{
        document.getElementById('restaurant_register_frm').addEventListener('submit',function(e){
            e.preventDefault();
            
            $.ajax({
                url:url_to_request,
                type:'POST',
                data        : new FormData(this), // our data object
                dataType    : 'json', // what type of data do we expect back from the server
                encode      : true,
                contentType: false,
                cache: false,
                processData:false,
                success:function(r){
                    //console.log(r);
                    if(r.status == true){
                        $("#restaurant_register_frm #success_msg_alert").html("successfully registered");
                        $("#restaurant_register_frm #success_msg_alert").fadeIn();
                        setTimeout(function(){
                            $("#restaurant_register_frm #success_msg_alert").fadeOut();
                        },1300);
                        
                        window.location.href=window.location.origin + "/foody/login.php";
                    }
                    else{
                        $("#restaurant_register_frm #error_msg_alert").html("Error. Try again later");
                        $("#restaurant_register_frm #error_msg_alert").fadeIn();
                        setTimeout(function(){
                            $("#restaurant_register_frm #error_msg_alert").fadeOut();
                        },1300);
                    }
                },
                error:function(err){ 
                    //console.error(err);               
                    $("#restaurant_register_frm #warning_msg_alert").html("Server internal error.");
                    $("#restaurant_register_frm #warning_msg_alert").fadeIn();
                    setTimeout(function(){
                        $("#restaurant_register_frm #warning_msg_alert").fadeOut();
                    },1300);
                }
            });
        });
    }
    
    if(document.getElementById('login_frm') == null){}
    else{
        document.getElementById('login_frm').addEventListener('submit',function(e){
            e.preventDefault();
            
            $.ajax({
                url : url_to_request,
                type:'POST',
                data        : new FormData(this), // our data object
                dataType    : 'json', // what type of data do we expect back from the server
                encode      : true,
                contentType: false,
                cache: false,
                processData:false,
                success:function(r){
                    //console.log(r);
                    if(r.status == true){
                        $("#login_frm #success_msg_alert").html(r.msg);
                        $("#login_frm #success_msg_alert").fadeIn();
                        setTimeout(function(){
                            $("#login_frm #success_msg_alert").fadeOut();
                        },1300);
                        window.location.href = "/foody";
                    }
                    else{
                        $("#login_frm #error_msg_alert").html(r.msg);
                        $("#login_frm #error_msg_alert").fadeIn();
                        setTimeout(function(){
                            $("#login_frm #error_msg_alert").fadeOut();
                        },1300);
                    }
                },
                error:function(err){ 
                    console.error(err);               
                    $("#login_frm #warning_msg_alert").html("Server internal error.");
                    $("#login_frm #warning_msg_alert").fadeIn();
                    setTimeout(function(){
                        $("#login_frm #warning_msg_alert").fadeOut();
                    },1300);
                }
            });
        });
    }
    
    if(document.getElementById('customer_profile_update_frm') == null){}
    else{
        document.getElementById('customer_profile_update_frm').addEventListener('submit',function(e){
            e.preventDefault();
            
            $.ajax({
                url:url_to_request,
                type:'POST',
                data        : new FormData(this), // our data object
                dataType    : 'json', // what type of data do we expect back from the server
                encode      : true,
                contentType: false,
                cache: false,
                processData:false,
                success:function(r){
                    //console.log(r);
                    if(r.status == true){
                        $("#customer_profile_update_frm #success_msg_alert").html("Successfully updated");
                        $("#customer_profile_update_frm #success_msg_alert").fadeIn();
                        setTimeout(function(){
                            $("#customer_profile_update_frm #success_msg_alert").fadeOut();
                            
                            window.location.reload();
                        },1300);
                    }
                    else{
                        $("#customer_profile_update_frm #error_msg_alert").html("Not updated. Try again later");
                        $("#customer_profile_update_frm #error_msg_alert").fadeIn();
                        setTimeout(function(){
                            $("#customer_profile_update_frm #error_msg_alert").fadeOut();
                        },1300);
                    }
                },
                error:function(err){ 
                    //console.error(err);               
                    $("#customer_profile_update_frm #warning_msg_alert").html("Server internal error.");
                    $("#customer_profile_update_frm #warning_msg_alert").fadeIn();
                    setTimeout(function(){
                        $("#customer_profile_update_frm #warning_msg_alert").fadeOut();
                    },1300);
                }
            });
        });
    }
    
    if(document.getElementById('add_menu_item_frm') == null){}
    else{
        document.getElementById('add_menu_item_frm').addEventListener('submit',function(e){
            e.preventDefault();
            
            $.ajax({
                url:url_to_request,
                type:'POST',
                data        : new FormData(this), // our data object
                dataType    : 'json', // what type of data do we expect back from the server
                encode      : true,
                contentType: false,
                cache: false,
                processData:false,
                success:function(r){
                    //console.log(r);
                    if(r.status == true){
                        $("#add_menu_item_frm #success_msg_alert").html(r.msg);
                        $("#add_menu_item_frm #success_msg_alert").fadeIn();
                        setTimeout(function(){
                            $("#add_menu_item_frm #success_msg_alert").fadeOut();
                            
                            window.location.reload();
                        },1300);
                    }
                    else{
                        $("#add_menu_item_frm #error_msg_alert").html(r.msg);
                        $("#add_menu_item_frm #error_msg_alert").fadeIn();
                        setTimeout(function(){
                            $("#add_menu_item_frm #error_msg_alert").fadeOut();
                        },1300);
                    }
                },
                error:function(err){ 
                    //console.error(err);               
                    $("#add_menu_item_frm #warning_msg_alert").html("Server internal error.");
                    $("#add_menu_item_frm #warning_msg_alert").fadeIn();
                    setTimeout(function(){
                        $("#add_menu_item_frm #warning_msg_alert").fadeOut();
                    },1300);
                }
            });
        });
    }
    
    
    
    
    function add_form(){
        if($("#add_menu_item_frm").show()){}
        else{$("#add_menu_item_frm").show();}
        var formElements = 
            "<div class='col-lg-4'>"+
                "<div class='card' style='margin:5px;'>"+
                    "<div class='card-body'>"+
                        "<div class='form-group'>"+
                            "<label>Item name</label>"+
                            "<input type='text' name='menu_item_name[]' class='form-control' required/><br/>"+
                        "</div>"+
    
                        "<div class='form-group'>"+
                            "<label>Item price</label>"+
                            "<input type='number' name='menu_item_price[]' class='form-control' required/><br/>"+
                        "</div>"+
                        
                        "<div class='form-group'>"+
                            "<label>Item price per</label>"+
                            "<select name='menu_item_measurment[]' class='form-control' required>"+
                                "<option value=''>select</option>"+
                                "<option value='kg'>kg</option>"+
                                "<option value='plate'>plate</option>"+
                                "<option value='unit'>unit</option>"+
                            "</select>"+
                        "</div>"+
    
                        "<div class='form-group'>"+
                            "<label>Item type</label>"+
                            "<select name='menu_item_type[]' class='form-control' required>"+
                                "<option value=''>select</option>"+
                                "<option value='veg'>Veg</option>"+
                                "<option value='veg'>non-veg</option>"+
                            "</select>"+
                        "</div><br/>"+
                    "</div>"+
                "</div>"+
            "</div>";
        $("#form_el_container").append(formElements);
    }
    
    
    if((document.getElementsByClassName('update_menu_frm')).length == 0){}
    else{
        $(".update_menu_frm").each(function(){
            var frm_id = this.id;
            $("#"+frm_id).submit(function(e){
                e.preventDefault();
                var loader_id = "loader_"+frm_id.substr((frm_id.lastIndexOf("_")+1),frm_id.length);
                
    
                $("#"+loader_id).fadeIn();
    
                $.ajax({
                    url:url_to_request,
                    type:'POST',
                    data        : new FormData(this), // our data object
                    dataType    : 'json', // what type of data do we expect back from the server
                    encode      : true,
                    contentType: false,
                    cache: false,
                    processData:false,
                    success:function(r){
                        //console.log(r);
                        if(r.status == true){                        
                            setTimeout(function(){
                                //$("#update_menu_item_frm #success_msg_alert").fadeOut();
                                $("#"+loader_id).fadeOut();
                                window.location.reload();
                            },1300);
                        }
                        else{
                        }
                    },
                    error:function(err){ 
                        alert("Menu item not updated. Try again later.");      
                    }
                });
            });
        });
    }

  9. It's time to create UI files so create first file named 'index.php' and write script below:

    <?php
        
    ?>
    <?php
        require_once(__DIR__.'/database/dbs_controls.php');
    ?>
    
    <?php
        session_start();
        if(!empty($_SESSION)){
            //print_r($_SESSION);
            if($_SESSION['user_type'] == "restaurant"){
                $r_email = $_SESSION['r_email'];
                $r_id = $_SESSION['r_id'];
                $dbs = new DatabaseControls();
                $qry = "select * from restaurant where r_email = '".$r_email."' and r_id = '".$r_id."'";
                $result = $dbs->run_select_qry($qry);
                $result = $result['extra_data'];
            }
            else if($_SESSION['user_type'] == "customer"){
                $c_email = $_SESSION['c_email'];
                $c_id = $_SESSION['c_id'];
                $dbs = new DatabaseControls();
                $qry = "select * from customer where c_email = '".$c_email."' and c_id = '".$c_id."'";
                $result = $dbs->run_select_qry($qry);
                $result = $result['extra_data'];
            }
        }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">    
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
        <link rel="stylesheet" href="/foody/css/app.css">
        <title>Foody - Home</title>
    </head>
    <body>
        <?php require_once('navbar.php'); ?>
    
    
        <?php if(empty($_SESSION)){?>
        <div class="jumbotron" style="text-align:center;">
            <h1 class="display-4">Welcome to FOODY!</h1>
            <h4>Delicious food. Great experience.</h4>
            <p class="lead"></p>
            <hr class="my-4">
            <p>Please Login/Register to continue...</p>
            <p class="lead">
                <a class="btn btn-primary btn-lg" href="/foody/login.php" role="button">Login</a>
                <a class="btn btn-primary btn-lg" href="/foody/customer_register.php" role="button">Customer Register</a>
                <a class="btn btn-primary btn-lg" href="/foody/restaurant_register.php" role="button">Restaurant Register</a>
            </p>
        </div>
        <?php 
        } else {
            if($_SESSION['user_type'] === "restaurant"){ ?>
              <div class="jumbotron" style="text-align:center;">
                    <h1 class="display-4">Welcome to FOODY <?php echo $result['r_name']; ?>!</h1>
                    <h4>Delicious food. Great experience.</h4>
                    <p class="lead"></p>
                    <hr class="my-4">
                    <p>You can manage menu dashboard and view orders</p>
                    <p class="lead">
                        <a class="btn btn-primary btn-lg" href="/foody/profile.php" role="button">Profile</a>
                    </p>
                </div>  
            <?php } else { ?>
                <div class="jumbotron" style="text-align:center;">
                    <h1 class="display-4">Welcome to FOODY <?php echo $result['c_name']; ?>!</h1>
                    <h4>Delicious food. Great experience.</h4>
                    <p class="lead"></p>
                    <hr class="my-4">
                    <p>You can view restaurants and order tasty food and view past orders</p>
                    <p class="lead">
                        <a class="btn btn-primary btn-lg" href="/foody/profile.php" role="button">Profile</a>
                        <a class="btn btn-primary btn-lg" href="/foody/browse_restaurants.php" role="button">Browse restaurants</a>
                    </p>
                </div>
            <?php } ?>
        <?php } ?>    
        <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
        <script src="/foody/js/app.js"></script>
    </body>
    </html>

  10. Now create file named as 'browse_restaurants.php' and write script below:

    <?php
        require_once(__DIR__.'/controllers/RestaurantController.php');
        session_start();
    
        // getting all restaurant from menu controller
        $res = new RestaurantController();
        $restaurant_list = $res->get_all_restaurant();
        $restaurant_list = json_decode($restaurant_list,true);
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">    
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
        <link rel="stylesheet" href="/foody/css/app.css">
        <title>Foody - Browse restaurants</title>
    </head>
    <body>
    
        <?php require_once(__DIR__.'/navbar.php'); ?>
    
        <?php if(($restaurant_list['status'])){
            $restaurant_list = $restaurant_list['extra_data'];
        ?>
            <div class="container">
                <?php foreach($restaurant_list as $rest){?>
                    <div class="row">
                        <div class="col-md-8">
                            <div class="card">
                                <div class="card-header"><?php echo $rest['r_name']; ?></div>
                                <div class="card-body">
                                    <div class="row">
                                        <div class="col-md">
                                            <?php if($rest['r_food_preference'] === "veg"){?>
                                                <h5>Food prefernce </h5>
                                                <img src="/foody/foodshala_images/veg_imag.jpg" width="25"/>
                                                <h6>Veg</h6>
                                            <?php } else { ?>
                                                <h5>Food preference </h5>
                                                <img src="/foody/foodshala_images/non_veg_imag.jpg"  width="25"/>
                                                <h6>Non Veg</h6>
                                            <?php } ?>
                                        </div>
                                    </div>
                                    
                                    <br/>
                                    <div class="row">
                                        <div class="col-md-8" style="margin:5px;">
                                            <h5>Address</h5>
                                            <h6><?php echo $rest['r_address']; ?></h6>
                                        </div>
                                        <div class="col-md" style="margin:5px;">
                                            <h5>Contact no</h5>
                                            <h6><?php echo $rest['r_country_code'].''.$rest['r_cont_no']; ?></h6>
                                        </div>
                                    </div>
                                    <div class="row" style="margin-top:10px;">                                    
                                        <br/>
                                        <div class="col-lg" style="margin:5px;">
                                            <button type="button" class="btn">
                                                Parking available <span class="badge badge-light"><i class="fa fa-car" aria-hidden="true"></i></span>
                                                <span class="sr-only">unread messages</span>
                                            </button>
                                        </div>
                                        <div class="col-lg" style="margin:5px;">
                                            <button type="button" class="btn">
                                                Wifi available <span class="badge badge-light"><i class="fa fa-wifi" aria-hidden="true"></i></span>
                                                <span class="sr-only">unread messages</span>
                                            </button>
                                        </div><div class="col-lg" style="margin:5px;">
                                            <button type="button" class="btn">
                                                Easy payment <span class="badge badge-light"><i class="fa fa-money" aria-hidden="true"></i></span>
                                                <span class="sr-only">unread messages</span>
                                            </button>
                                        </div>
                                    </div>
                                    
                                </div>
                                <div class="card-footer" style="margin-top:10px;">
                                        <?php if(empty($_SESSION)){?>
                                            <a href="/foody/restaurant.php?view_r_id=<?php echo $rest['r_id']; ?>" class="btn btn-success btn-md">View</a>
                                        <?php } else{ if($_SESSION['user_type'] === "restaurant"){ ?>                                        
                                            <a href="/foody/restaurant.php?view_r_id=<?php echo $rest['r_id']; ?>" class="btn btn-success btn-md">View</a>
                                        <?php } else{?>
                                            <a href="/foody/restaurant.php?view_r_id=<?php echo $rest['r_id']; ?>" class="btn btn-success btn-md">Order food</a>
                                        <?php } } ?>
                                    </div>
                            </div>
                        </div>
                    </div>
                <?php } ?>
            </div>
        
        
        
        <?php
        } else { ?>
            <div>No restaurants found</div>
        <?php } ?>
        
    
    
        <!-- js scripting -->
        <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
        <script src="/foody/js/app.js"></script>
    </body>
    </html>

  11. Now create file named as 'customer_register.php' and write script below:

    <?php
        
    ?>
    <?php 
        session_start();
        if(empty($_SESSION)){}
        else{
            header("location:/foodshala/profile.php");
        }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
        <link rel="stylesheet" href="/foodshala/css/app.css">
        <title>FoodShala - Customer Register</title>
    </head>
    <body>
        
    <?php require_once('navbar.php'); ?>
    <?php require_once('navbar.php'); ?>
    
        <div class="container" style="margin-top:100px;">
    
            
            <form action="" method="post" id="customer_register_frm">
                <input type="hidden" name="ActionToCall" value="customer_register"/>
    
                <div class="jumbotron jumbotron-fluid">
                    <div class="container">
                        <h1 class="display-4">Customer Register</h1>
                    </div>
                </div>
    
                <div class="form-group">
                    <div id="success_msg_alert" class="alert alert-success" style="display:none;"></div>
                    <div id="error_msg_alert" class="alert alert-danger" style="display:none;"></div>
                    <div id="warning_msg_alert" class="alert alert-warning" style="display:none;"></div>
                </div>
    
                <div class="form-group">
                    <label>Name</label>
                    <input type="text" name="c_name" class="form-control" required/>
                </div>
    
                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="c_email" class="form-control" required/>
                </div>
    
                <div class="form-group">
                    <label>Password</label>
                    <input type="password" name="c_password" class="form-control" required/>
                </div>
    
                <div class="form-group">
                    <label>Mobile Number</label>
                    <input type="text" name="c_cont_no" class="form-control" required/>
                </div>
    
                <div class="form-group">
                    <label>Food Preference</label>
                    <select name="c_food_pref" class="form-control" required>
                        <option value="">Select</option>
                        <option value="veg">Veg</option>
                        <option value="non-veg">Non-veg</option>
                    </select>
                </div>
    
                <div class="form-group">
                    <input type="submit" value="Login" class="btn btn-primary btn-md" />
                </div>
            </form>
        </div>
        
      
        
        
        <!-- js scripting -->
        <script src="https://code.jquery.com/jquery-2.2.4.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
        <script src="/foodshala/js/app.js"></script>
    </body>
    </html>

  12. Now create file named as 'login.php' and write script below:

    
            <?php
        
    ?>
    <?php 
        session_start();
        if(empty($_SESSION)){}
        else{
            header("location:/foody/profile.php");
        }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
        <link rel="stylesheet" href="/foody/css/app.css">
        <title>Foody - Login</title>
    </head>
    <body>
        <?php require_once('navbar.php'); ?>
        </nav>
    
        <div class="container">
            <form action="" method="post" id="login_frm">
                <input type="hidden" name="ActionToCall" value="login"/>
    
                <div class="jumbotron jumbotron-fluid">
                    <div class="container">
                        <h1 class="display-4">Login</h1>
                    </div>
                </div>
    
                <div class="form-group">
                    <div id="success_msg_alert" class="alert alert-success" style="display:none;"></div>
                    <div id="error_msg_alert" class="alert alert-danger" style="display:none;"></div>
                    <div id="warning_msg_alert" class="alert alert-warning" style="display:none;"></div>
                </div>
                <div class="form-group">
                    <label>user type</label>
                    <select name="user_type" class="form-control" required>
                        <option value="">Select</option>
                        <option value="customer">Customer</option>
                        <option value="restaurant">Restaurant</option>
                    </select>
                </div>
    
                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="email" class="form-control" required/>
                </div>
    
                <div class="form-group">
                    <label>Password</label>
                    <input type="password" name="password" class="form-control" required/>
                </div>
                <div class="form-group">
                    <input type="submit" class="btn" value="Login" required/>
                </div>
            </form>
        </div>
        
      
        
        
        <!-- js scripting -->
        <script src="https://code.jquery.com/jquery-2.2.4.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
        <script src="/foody/js/app.js"></script>
    </body>
    </html>

  13. Now create file named as 'logout.php' and write script below:

    <?php
        
    ?>
    <?php
    if(session_start()){
        session_destroy();
    }
    header("location:/foody");
    ?>

  14. Now create file named as 'navbar.php' and write script below:

    
            <?php
        
    ?>
    
    <!-- navbar -->
    <nav class="navbar navbar-expand-lg">
            <div class="container">
                <a class="navbar-brand" href="/foody">FOODY</a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                Menu
                </button>
    
                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    
                    <ul class="nav nav-tabs"> 
                        <li class="nav-item">
                            <a class="nav-link" href="/foody/browse_restaurants.php">Browse Restaurants</a>
                        </li>
                    </ul>
    
                    
                    <ul class="nav nav-tabs">
                        <?php if(empty($_SESSION)){ ?>
                        <li class="nav-item">
                            <a class="nav-link" href="/foody/login.php">Login</a>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                Register
                            </a>
                            <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                                <a class="dropdown-item" href="/foody/customer_register.php">Customer Register</a>
                                <a class="dropdown-item" href="/foody/restaurant_register.php">Restaurant Register</a>
                            </div>
                        </li>
                        <?php } ?>
    
                        <?php if(!empty($_SESSION)){ ?>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <img src="foodshala_images/def_prof_photo.jpg" style="width:20px;" alt="..." class="rounded-circle">
                            </a>
                            <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                                <a class="dropdown-item" href="/foody/profile.php">
                                <?php if($_SESSION['user_type'] == "customer"){echo $_SESSION['c_name'];}else{echo $_SESSION['r_name'];} ?></a>
                                <a class="dropdown-item" href="/foody/logout.php">Logout</a>
                            </div>
                        </li>
                        <?php } ?>
                    </ul>
                
                </div>
            </div>
        </nav>

  15. Now create file named as 'profile.php' and write script below:

    <?php
    ?>
    <?php
        require_once(__DIR__.'/database/dbs_controls.php');
        require_once(__DIR__.'/controllers/MenuController.php');
        require_once(__DIR__.'/controllers/OrderController.php');
    ?>
    
    <?php
        session_start();
        if(!empty($_SESSION)){
            //print_r($_SESSION);
            if($_SESSION['user_type'] == "restaurant"){
                $r_email = $_SESSION['r_email'];
                $r_id = $_SESSION['r_id'];
                $dbs = new DatabaseControls();
                $qry = "select * from restaurant where r_email = '".$r_email."' and r_id = '".$r_id."'";
                $result = $dbs->run_select_qry($qry);
                $result = $result['extra_data'];
            }
            else if($_SESSION['user_type'] == "customer"){
                $c_email = $_SESSION['c_email'];
                $c_id = $_SESSION['c_id'];
                $dbs = new DatabaseControls();
                $qry = "select * from customer where c_email = '".$c_email."' and c_id = '".$c_id."'";
                $result = $dbs->run_select_qry($qry);
                $result = $result['extra_data'];
            }
        }
        else{
            header("location:/foody/login.php");
        }
        
    ?>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
        <link rel="stylesheet" href="/foody/css/app.css">
        <title><?php if($_SESSION['user_type'] == "customer"){echo $_SESSION['c_name'];}else{echo $_SESSION['r_name'];} ?></title>
    </head>
    <body>
        <?php require_once(__DIR__.'/navbar.php'); ?>
        
    
        
        <?php if($_SESSION['user_type'] == "restaurant"){?>
            <div class="container" style="margin-top:50px;">
                <ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
                    
                    <li class="nav-item">
                        <a class="nav-link active" id="pills-profile-tab" data-toggle="pill" href="#pills-menu" role="tab" aria-controls="pills-profile" aria-selected="true">Update Menu</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" id="pills-profile-tab" data-toggle="pill" href="#pills-order-history" role="tab" aria-controls="pills-profile" aria-selected="true">Orders</a>
                    </li>
                    
                </ul>
                <div class="tab-content" id="pills-tabContent">
                    <div class="tab-pane fade show active" id="pills-menu" role="tabpanel" aria-labelledby="pills-profile-tab">
                        <?php 
                        $menu = new MenuController();
                        $result_menu = $menu->get_menu($result['r_id']);
                        if($result_menu['status']){
                            $menu_items = $result_menu['extra_data'];?>
                            <table border="1" cellspacing="10" cellpadding="10" style="width:100%;">
                                <thead>
                                    <tr>
                                    <th width="20%" style="text-align:center;">Item name</th>
                                    <th width="20%" style="text-align:center;">Item type</th>
                                    <th width="20%" style="text-align:center;">Item price</th>
                                    <th width="20%" style="text-align:center;">Item measurment</th>
                                    <th width="20%" style="text-align:center;">Controls</th>
                                    </tr>
                                </thead>
                            <?php
                            foreach($menu_items as $indi_menu){?>
                                <form action="" method="post" class="update_menu_frm" id="update_menu_item_frm_<?php echo $indi_menu['item_id']; ?>">
                                    <input type="hidden" name="ActionToCall" value="update_menu_item" />
                                    <input type="hidden" name="r_id" value="<?php echo $_SESSION['r_id']; ?>" />
                                    <input type="hidden" name="item_id" value="<?php echo $indi_menu['item_id']; ?>" />
                                    <tr>
                                    
                                        <td>
                                            <input type="text" name="item_name" required class="form-control" value="<?php echo $indi_menu['item_name']; ?>" />
                                        </td>
                                        <td>
                                            <select class="form-control" name="item_type" required>
                                                <option value="veg" <?php echo ($indi_menu['item_type'] == "veg")?'selected':''; ?>>veg</option>
                                                <option value="non-veg" <?php echo ($indi_menu['item_type'] == "non-veg")?'selected':''; ?>>non-veg</option>
                                            </select>
                                        </td>
                                        <td>
                                            <input type="text" name="item_price" required class="form-control" value="<?php echo $indi_menu['item_price']; ?>" />
                                        </td>
                                        <td>
                                            <select class="form-control" name="item_measurment" required>
                                                <option value="" <?php echo isset($indi_menu['item_measurment'])?'':'selected'; ?>>select</option>
                                                <option value="kg" <?php echo ($indi_menu['item_measurment'] == "kg")?'selected':''; ?>>kg</option>
                                                <option value="unit" <?php echo ($indi_menu['item_measurment'] == "unit")?'selected':''; ?>>unit</option>
                                                <option value="plate" <?php echo ($indi_menu['item_measurment'] == "plate")?'selected':''; ?>>plate</option>
                                            </select>
                                        </td>
                                        <td align="center">
                                            <img style="display:none;" src="/foody/foodshala_images/loader.gif" id="loader_<?php echo $indi_menu['item_id'] ?>" width="30"/>
                                            <button type="submit" class="btn btn-primary btn-sm"><span title="update"><i class="fa fa-pencil" aria-hidden="true"></i></span></button>
                                        </td>
                                    
                                    </tr>
                                </form>
                            <?php } ?>
                            </table>
                            <?php 
                        }
                        else{
                            echo "no menu is there.";
                        }
                        ?>
    
                        <!-- dynamic form for adding menu items -->
                        <div class="row" style="margin-top:50px;">
                            <div class="col-md-12">
                                <button onclick="add_form()" class="btn btn-secondary btn-sm">Add items</button>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <form action="" method="post" id="add_menu_item_frm" style="display:none;">
                                    <div class="form-group">
                                        <div id="success_msg_alert" class="alert alert-success" style="display:none;"></div>
                                        <div id="error_msg_alert" class="alert alert-danger" style="display:none;"></div>
                                        <div id="warning_msg_alert" class="alert alert-warning" style="display:none;"></div>
                                    </div>
                                    <input type="hidden" name="ActionToCall" value="add_menu_item" />
                                    <input type="hidden" name="r_id" value="<?php echo $result['r_id']; ?>" />
                                    <div id="form_el_container" class="row"></div>
                                    <input type="submit" class="btn btn-success btn-sm"/>
                                </form>
                            </div>
                        </div>
                    </div>
    
                    <div class="tab-pane fade" id="pills-order-history" role="tabpanel" aria-labelledby="pills-profile-tab">
                        <?php
                            $orders = new OrderController();
                            $order_details = $orders->get_orders_for_restaurant($_SESSION['r_id']);
                            if($order_details['status']){?>
                                
                                <?php
                                foreach($order_details['extra_data'] as $indi_order){?>
                                    <div class="card" style="margin-top:20px;box-shadow:0 0 10px rgba(0,0,0,0.12);">
                                        <div class="card-header">Order summary</div>
                                        <div class="card-body">
                                            <div><b>Order id</b><br/><?php echo $indi_order['o_id']; ?></div><br/>
                                            <div><b>Customer Name</b><br/><?php echo $indi_order['c_name']; ?></div><br/>
                                            <div><b>Customer Email</b><br/><?php echo $indi_order['c_email']; ?></div><br/>
                                            <div><b>Customer Phone no</b><br/><?php echo $indi_order['c_country_code'].''.$indi_order['c_cont_no']; ?></div><br/>
                                            
                                            <div><b>Order description</b><br/>
                                                <?php 
                                                    //echo $indi_order['o_desc'];
                                                    $desc = json_decode($indi_order['o_desc'],true);
                                                    foreach($desc as $d){
                                                        echo "<b>Item title</b> : ".$d['item_title'].
                                                        ",\t <b>Item price</b> : ".$d['item_price'].
                                                        ",\t <b>Item type</b> : ".$d['item_type'].
                                                        ",\t <b>Item qty</b> : ".$d['item_qty'].
                                                        "<br/>";
                                                    }
                                                        
                                                ?>
                                            </div><br/>
                                            <div><b>Order Amount</b><br/><?php echo $indi_order['o_amt']; ?></div><br/>
                                            <div><b>Order Date</b><br/><?php echo date('Y-m-d h:i a',strtotime($indi_order['o_date'])); ?></div>
                                        </div>
                                    </div>   
                                <?php } ?>
                                    
                            <?php
                            }
                            else{
                                echo "no orders";
                            }
                        ?>
                    </div>
                </div>
            </div>
    
        <?php } else { ?>
            <div class="container" style="margin-top:50px;">
                <ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
                    
                    <li class="nav-item">
                    <a class="nav-link active" id="pills-profile-tab" data-toggle="pill" href="#pills-menu" role="tab" aria-controls="pills-profile" aria-selected="true">Update Profile</a>
                    </li>
                    <li class="nav-item">
                    <a class="nav-link" id="pills-profile-tab" data-toggle="pill" href="#pills-order" role="tab" aria-controls="pills-profile" aria-selected="true">Order History</a>
                    </li>
                    
                </ul>
                <div class="tab-content" id="pills-tabContent">
                    <div class="tab-pane fade show active" id="pills-menu" role="tabpanel" aria-labelledby="pills-profile-tab">
                        <form action="" method="post" id="customer_profile_update_frm">
                            <input type="hidden" name="ActionToCall" value="customer_profile_update"/>
                            <input type="hidden" name="c_id" value="<?php echo isset($result['c_id'])?$result['c_id']:0; ?>"/>
    
                            <div class="jumbotron jumbotron-fluid">
                                <div class="container">
                                    <h1 class="display-4">Update Profile</h1>
                                </div>
                            </div>
    
                            <div class="form-group">
                                <div id="success_msg_alert" class="alert alert-success" style="display:none;"></div>
                                <div id="error_msg_alert" class="alert alert-danger" style="display:none;"></div>
                                <div id="warning_msg_alert" class="alert alert-warning" style="display:none;"></div>
                            </div>
    
                            <div class="form-group">
                                <label>Name</label>
                                <input type="text" name="c_name" value="<?php echo isset($result['c_name'])?$result['c_name']:''; ?>" class="form-control" required/>
                            </div>
    
                            <div class="form-group">
                                <label>Email</label>
                                <input type="email" name="c_email" value="<?php echo isset($result['c_email'])?$result['c_email']:''; ?>" class="form-control" readonly required/>
                            </div>
    
                            <div class="form-group">
                                <label>Mobile Number</label>
                                <input type="text" name="c_cont_no" value="<?php echo isset($result['c_cont_no'])?$result['c_cont_no']:''; ?>" class="form-control" required/>
                            </div>
    
                            <div class="form-group">
                                <label>Food Preference</label>
                                <select name="c_food_pref" class="form-control" required>
                                    <option value="" <?php echo isset($result['c_food_preference'])?'':'selected'; ?>>Select</option>
                                    <option value="veg" <?php echo ($result['c_food_preference']!=="" && $result['c_food_preference']=="veg")?'selected':''; ?>>Veg</option>
                                    <option value="non-veg" <?php echo ($result['c_food_preference']!=="" && $result['c_food_preference']=="non-veg")?'selected':''; ?>>Non-veg</option>
                                </select>
                            </div>
    
                            <div class="form-group">
                                <input type="submit" value="Update" class="btn btn-primary btn-md" />
                            </div>
                        </form>
                    </div>
                    <div class="tab-pane fade" id="pills-order" role="tabpanel" aria-labelledby="pills-profile-tab">
                        <?php
                            $orders = new OrderController();
                            $order_details = $orders->get_orders_for_customer($_SESSION['c_id']);
                            if($order_details['status']){?>
                                
                                <?php
                                foreach($order_details['extra_data'] as $indi_order){?>
    
                                    <div class="card" style="margin-top:20px;box-shadow:0 0 10px rgba(0,0,0,0.12);">
                                        <div class="card-header">Order summary</div>
                                        <div class="card-body">
                                            <div><b>Order id</b><br/><?php echo $indi_order['o_id']; ?></div><br/>
                                            <div><b>Restaurant Name</b><br/><?php echo $indi_order['r_name']; ?></div><br/>
                                            <div><b>Restaurant Email</b><br/><?php echo $indi_order['r_email']; ?></div><br/>
                                            <div><b>Restaurant Phone no</b><br/><?php echo $indi_order['r_country_code'].''.$indi_order['r_cont_no']; ?></div><br/>
                                            <div><b>Restaurant Address</b><br/><?php echo $indi_order['r_address']; ?></div><br/>
                                            <div><b>Order description</b><br/>
                                                <?php 
                                                    //echo $indi_order['o_desc'];
                                                    $desc = json_decode($indi_order['o_desc'],true);
                                                    foreach($desc as $d){
                                                        echo "<b>Item title</b> : ".$d['item_title'].
                                                        ",\t <b>Item price</b> : ".$d['item_price'].
                                                        ",\t <b>Item type</b> : ".$d['item_type'].
                                                        ",\t <b>Item qty</b> : ".$d['item_qty'].
                                                        "<br/>";
                                                    }
                                                        
                                                ?>
                                            </div><br/>
                                            <div><b>Order Amount</b><br/><?php echo $indi_order['o_amt']; ?></div><br/>
                                            <div><b>Order Date</b><br/><?php echo date('Y-m-d h:i a',strtotime($indi_order['o_date'])); ?></div>
                                        </div>
                                    </div>                                   
                                <?php } ?>
                            <?php
                            }
                            else{
                                echo "no orders";
                            }
                        ?>
                    </div>
                </div>
    
                
            </div>
        <?php } ?>
    
    
    
    
        
    
    
    
        <!-- js scripting -->
        <script src="https://code.jquery.com/jquery-2.2.4.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
        <script src="/foody/js/app.js"></script>
    </body>
    </html>

  16. Now create file named as 'restaurant_register.php' and write script below:

    <?php
    ?>
    <?php 
        session_start();
        if(empty($_SESSION)){}
        else{
            header("location:/foody/profile.php");
        }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
        <link rel="stylesheet" href="/foody/css/app.css">
        <title>Foody - Restaurant Register</title>
    </head>
    <body>
    
    <?php require_once('navbar.php'); ?>
    
        <div class="container">
            <form action="" method="post" id="restaurant_register_frm">
                <input type="hidden" name="ActionToCall" value="restaurant_register"/>
    
                <div class="jumbotron jumbotron-fluid">
                    <div class="container">
                        <h1 class="display-4">Restaurant Register</h1>
                    </div>
                </div>
    
                <div class="form-group">
                    <div id="success_msg_alert" class="alert alert-success" style="display:none;"></div>
                    <div id="error_msg_alert" class="alert alert-danger" style="display:none;"></div>
                    <div id="warning_msg_alert" class="alert alert-warning" style="display:none;"></div>
                </div>
    
                
    
                <div class="form-group">
                    <label>Name</label>
                    <input type="text" name="r_name" class="form-control" required/>
                </div>
    
                <div class="form-group">
                    <label>Address</label>
                    <input type="text" name="r_address" class="form-control" required/>
                </div>
    
                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="r_email" class="form-control" required/>
                </div>
    
                <div class="form-group">
                    <label>Password</label>
                    <input type="password" name="r_password" class="form-control" required/>
                </div>
    
                <div class="form-group">
                    <label>Mobile Number</label>
                    <input type="text" name="r_cont_no" class="form-control" required/>
                </div>
    
                <div class="form-group">
                    <label>Food Preference</label>
                    <select name="r_food_pref" class="form-control" required>
                        <option value="">Select</option>
                        <option value="veg">Veg</option>
                        <option value="non-veg">Non-veg</option>
                    </select>
                </div>
    
                <div class="form-group">
                    <input type="submit" value="Register" class="btn btn-primary btn-md" />
                </div>
            </form>
        </div>
        
      
        
        
        <!-- js scripting -->
        <script src="https://code.jquery.com/jquery-2.2.4.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
        <script src="/foody/js/app.js"></script>
    </body>
    </html>

  17. Now create file named as 'restaurant.php' and write script below:

    <?php
    ?>
    <?php
        require_once(__DIR__.'/database/dbs_controls.php');
    
        session_start();
        if(!empty($_SESSION)){
            //print_r($_SESSION);
            if($_SESSION['user_type'] == "restaurant"){
                $r_email = $_SESSION['r_email'];
                $r_id = $_SESSION['r_id'];
                $dbs = new DatabaseControls();
                $qry = "select * from restaurant where r_email = '".$r_email."' and r_id = '".$r_id."'";
                $result = $dbs->run_select_qry($qry);
                $result = $result['extra_data'];
            }
            else if($_SESSION['user_type'] == "customer"){
                $c_email = $_SESSION['c_email'];
                $c_id = $_SESSION['c_id'];
                $dbs = new DatabaseControls();
                $qry = "select * from customer where c_email = '".$c_email."' and c_id = '".$c_id."'";
                $result = $dbs->run_select_qry($qry);
                $result = $result['extra_data'];
            }
        }
        else{}
        
        $view_r_id = (int)$_REQUEST['view_r_id'];
        if(isset($view_r_id) && $view_r_id !== '' && $view_r_id!==0){        
            // getting restaurant details
            $qry = "select * from restaurant where r_id = ".$view_r_id;
            $dbs = new DatabaseControls();
            $view_r_details = $dbs->run_select_qry($qry);
            if($view_r_details['status']){
            $view_r_details = $view_r_details['extra_data'];}
            else{header("location:/foody");}
    
            // getting that restaurant's menu details
            $qry = "select * from menu_item where r_id = ".$view_r_id;
            $view_r_menu_details = $dbs->run_multi_data_select_qry($qry);
            $view_r_menu_details = $view_r_menu_details['extra_data'];
    
    
        }
        else{
            header("location:/foody");
        }
    ?>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
        <link rel="stylesheet" href="/foody/css/app.css">
        <title><?php echo $view_r_details['r_name']; ?></title>
    </head>
    <body>
        <?php require_once('navbar.php'); ?>
    
    
        <div class="container" style="margin-bottom:200px;">
            <?php foreach($view_r_menu_details as $menu_item){ ?>
                <div class="row">
                    <div class="col-md-6">
                        <div class="card" style="margin:10px 5px;padding:15px 10px;box-shadow:0 0 10px rgba(0,0,0,0.12);">
                            <div clss="card-body">                        
                                <div id="<?php echo $menu_item['item_id']; ?>_main">
                                    <div class="row">
                                        <div class="col-md-8">
                                            <div style="font-size:20px;" id="<?php echo $menu_item['item_id']; ?>_title"><?php echo $menu_item['item_name']; ?></div>
                                            <div id="<?php echo $menu_item['item_id']; ?>_price"><?php echo $menu_item['item_price']; ?></div>
                                            <div id="<?php echo $menu_item['item_id']; ?>_measurment">per <?php echo $menu_item['item_measurment']; ?></div>
                                            <div id="<?php echo $menu_item['item_id']; ?>_type"><?php echo $menu_item['item_type']; ?></div>
                                        </div>
                                        <?php if((empty($_SESSION)) || ((!empty($_SESSION) && $_SESSION['user_type'] === "customer"))){ ?>
                                        <div class="col-md">
                                            <br/>
                                            <h6>Qty</h6>
                                            <button type="button" class="btn btn-primary btn-md" id="<?php echo $menu_item['item_id']; ?>_dec_btn" onclick="dec_qty(this)">-</button>
                                            <input type="text" style="width:50px;height:35px;margin-top:1px;text-align:center;" readonly value="0" id="<?php echo $menu_item['item_id']; ?>_qty_box" />
                                            <button type="button" class="btn btn-primary btn-md" id="<?php echo $menu_item['item_id']; ?>_inc_btn" onclick="inc_qty(this)">+</button>
                                        </div>
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
    
            <?php } ?>
    
            <textarea rows="5" cols="50" style="display:none;width:100%;font-size:20px;" type="text" id="items_to_order_box"></textarea>
        </div>
        <div style="width:100%;color:white;font-size:22px;padding:5px;background-color:rgba(56,100,200,1.0);text-align:center;position:fixed;bottom:0;">       
            <?php if(empty($_SESSION)){ ?>
                <div>You need to be logged in to place order</div>
                <a href="/foody/login.php" class="btn btn-secondary btn-md">Login</a>
            <?php } else if(!empty($_SESSION) && $_SESSION['user_type'] === "restaurant"){ ?>
                <div>You need to be logged in as customer to place order</div>
            <?php } else { ?>
                <div>Total amount</div>
                <div>Rs.</div><div id="total_amt_div">0</div>
                <button type="button" class="btn btn-success btn-lg" id="place_order_btn" onclick="place_order()">Place order</button>
            <?php } ?>
        </div>
    
    
    <div class="modal fade" id="reply_modal" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel"></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div style="display:none;" class="alert alert-success" id="success_msg_alert"></div>
                    <div style="display:none;" class="alert alert-danger" id="error_msg_alert"></div>
                    <div style="display:none;" class="alert alert-warning" id="warning_msg_alert"></div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
    
    
        <!-- js scripting -->
        <script src="https://code.jquery.com/jquery-2.2.4.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
        <script src="/foody/js/app.js"></script>
    
        <script>
            var outer_arr = [];
            var inner_arr = {};
            var is_item_found = 0;
            var index_at_found = 0;
            var total_amt = 0;
    
            function inc_qty(evt){
                //console.log(evt.id);
                var id = evt.id;
                var sub = id.indexOf("_");
                var sub_str = id.substr(0,sub);
                //console.log(sub+" , "+sub_str);
                var cur_qty = parseInt($("#"+sub_str+"_qty_box").val()) + 1;
                $("#"+sub_str+"_qty_box").val(cur_qty);
    
                var cur_title = $("#"+sub_str+"_title").html();
                var cur_price = $("#"+sub_str+"_price").html();
                var cur_measurment = $("#"+sub_str+"_measurment").html();
                var cur_type = $("#"+sub_str+"_type").html();
                //console.log(cur_title+" at "+cur_price+" "+cur_measurment+" which is "+cur_type);
    
                if(outer_arr.length > 0){
                    for(var i=0;i<outer_arr.length;i++){
                        if(outer_arr[i].item_title == cur_title){
                            is_item_found = 1;
                            index_at_found = i;
                            break;
                        }
                    }
                    if(is_item_found == 1){
                        console.log("index changing : " +index_at_found);
                        outer_arr[index_at_found].item_qty = cur_qty;
                    }
                    else{
                        inner_arr.item_title = cur_title;
                        inner_arr.item_price = cur_price;
                        inner_arr.item_measurment = cur_measurment;
                        inner_arr.item_type = cur_type;
                        inner_arr.item_qty = cur_qty;
    
                        outer_arr.push(inner_arr);
                        inner_arr = {};
                    }
                }
                else{
                    inner_arr.item_title = cur_title;
                    inner_arr.item_price = cur_price;
                    inner_arr.item_measurment = cur_measurment;
                    inner_arr.item_type = cur_type;
                    inner_arr.item_qty = cur_qty;
    
                    outer_arr.push(inner_arr);
                    inner_arr = {};
                }
    
                
                total_amt = 0;
                for(var i=0;i<outer_arr.length;i++){
                    total_amt += (outer_arr[i].item_price * outer_arr[i].item_qty);
                }
    
    
                cur_title = null;
                cur_price = null;
                cur_measurment = null;
                cur_type = null;
                cur_qty = null;
                
                is_item_found = 0;
                index_at_found = 0;
                //finally show details in window
                $("#items_to_order_box").val(JSON.stringify(outer_arr));
                $("#total_amt_div").html(total_amt);
                //console.log("size of outer_arr : "+outer_arr.length);
                //console.log(total_amt);
            }
    
            function dec_qty(evt){
                //console.log(evt.id);
                var id = evt.id;
                var sub = id.indexOf("_");
                var sub_str = id.substr(0,sub);
                //console.log(sub+" , "+sub_str);
                var cur_qty = parseInt($("#"+sub_str+"_qty_box").val()) - 1;
                if(cur_qty < 0){
                    alert("Qty cannot be less than zero");$("#"+sub_str+"_qty_box").val('0');
                }
                else{
                    $("#"+sub_str+"_qty_box").val(cur_qty);
                }
                
    
                var cur_title = $("#"+sub_str+"_title").html();
                var cur_price = $("#"+sub_str+"_price").html();
                var cur_measurment = $("#"+sub_str+"_measurment").html();
                var cur_type = $("#"+sub_str+"_type").html();
                //console.log(cur_title+" at "+cur_price+" "+cur_measurment+" which is "+cur_type);
    
                if(outer_arr.length > 0){
                    for(var i=0;i<outer_arr.length;i++){
                        if(outer_arr[i].item_title == cur_title){
                            is_item_found = 1;
                            index_at_found = i;
                            break;
                        }
                    }
                    if(is_item_found == 1){
                        
                        console.log("index changing : " +index_at_found);
                        if(cur_qty == 0){
                            outer_arr.splice(index_at_found,1);
                        }
                        else{
                            outer_arr[index_at_found].item_qty = cur_qty;
                        }
                    }
                }
    
                total_amt = 0;
                for(var i=0;i<outer_arr.length;i++){
                    total_amt += (outer_arr[i].item_price * outer_arr[i].item_qty);
                }
    
                cur_title = null;
                cur_price = null;
                cur_measurment = null;
                cur_type = null;
                cur_qty = null;
                is_item_found = 0;
                index_at_found = 0;
                
                //finally show details in window
                $("#items_to_order_box").val(JSON.stringify(outer_arr));
                $("#total_amt_div").html(total_amt);
            }
    
            function place_order(){
                var final_amt = parseFloat($("#total_amt_div").html());
                if(final_amt <= 0){alert('order amount cannot be zero');return false;}
                
                var final_order_desc = $("#items_to_order_box").val();
                final_order_desc = JSON.stringify(final_order_desc);
                var final_amt = parseFloat($("#total_amt_div").html());
                
                var fd = new FormData();
                fd.append('ActionToCall','place_order');
                fd.append('c_id',<?php echo isset($_SESSION['c_id'])?$_SESSION['c_id']:''; ?>);
                fd.append('r_id',<?php echo (int)$view_r_id ?>);
                fd.append('order_desc',final_order_desc);
                fd.append('order_amt',final_amt);
    
                $.ajax({
                    url:window.location.origin+'/foody/common/commons.php',
                    type:'POST',
                    data : fd,
                    dataType : 'json',
                    encode : true,
                    contentType: false,
                    cache: false,
                    processData:false,
                    success:function(r){
                        console.log(r);
                        if(r.status == true){
                            $("#success_msg_alert").html("order successfully placed");
                            $("#success_msg_alert").show();
                            $("#error_msg_alert").hide();
                            $("#warning_msg_alert").hide();
    
                            $("#reply_modal").modal({
                                keyboard:false,
                                backdrop:false
                            });
    
                            setTimeout(function(){
                                window.location.reload();
                            },1200);
                        }
                        else{                        
                            $("#error_msg_alert").html("order placing error. try again later.");
                            $("#error_msg_alert").show();
                            $("#success_msg_alert").hide();
                            $("#warning_msg_alert").hide();
    
                            $("#reply_modal").modal({
                                keyboard:false,
                                backdrop:false
                            });
                        }
                    },
                    error:function(err){ 
                        
                        $("#warning_msg_alert").html("server error. try again later.");
                            $("#warning_msg_alert").show();
                            $("#error_msg_alert").hide();
                            $("#success_msg_alert").hide();
    
                            $("#reply_modal").modal({
                                keyboard:false,
                                backdrop:false
                            });
                    }
                });
            }
        </script>
    
    
    </body>
    </html>

Done
For further reference, please check repository at https://github.com/progssp/foody



How video streaming works on t...

Read more

Setup react and express on sam...

Read more

How neurons in brain function

Read more

How to create custom router in...

Read more

Run C++ as CGI script in web b...

Read more

Real time chat application usi...

Read more

How to setup lemp stack in ubu...

Read more

How to secure phpmyadmin in Ub...

Read more

How to create real time notifi...

Read more

How to create simple api in no...

Read more

Task scheduling with cron job ...

Read more

How to use modules in Javascri...

Read more

Create a multi language(lingua...

Read more

Linear Regression in machine l...

Read more

Create simple web application ...

Read more

Why algorithms can drive the i...

Read more

Why Javascript frameworks are ...

Read more

Create authenticated API in La...

Read more

Create simple and static websi...

Read more

Setup Pusher app

Read more

Laravel Installation and setti...

Read more

Realtime chat application in L...

Read more

Working of neurons (the easy w...

Read more

Progressive web app

Read more

Sign in for comment. Sign in