chieota / restaurant-research

0 stars 0 forks source link

Reservation condition #3

Open chieota opened 4 years ago

chieota commented 4 years ago

I have a question about the page of making a reservation. Now we can make a reservation even if the number of people is less than 0. Please tell me where I can put the condition..

スクリーンショット 2019-11-05 18 25 11
khertg commented 4 years ago

Inside this if statement https://github.com/chieota/restaurant-research/blob/fd4e4768ed2d24154f9dbb26898a75ac17f8f0a2/admin/reservation_action.php#L12

You put a condition that checks if the number of person is less than one. Then if it is less than one, you redirect the page back in add_reservation.php with the users last form inputs. Then you also need to display the last input back.

See example below.

*CONDITION

    if($_GET['action'] == 'add'){

        $date = $_POST['date'];
        $number_of_people = $_POST['number_of_people'];
        $restaurant_id = $_POST['restaurant_id'];

        //recommendation
        if($restaurant_id > 0){
             echo "<script>window.location.replace('reservations.php?date=$date&number_of_people=$number_of_people&restaurant_id=$restaurant_id');</script>";
        }

        $result = $reservation->save($restaurant_id,$date,$number_of_people,$_SESSION['user_id']);
        if($result){
            echo "<script>window.location.replace('reservations.php');</script>";
        }else{
            echo "Error!!";
        }
    }

*DISPLAYING LAST INPUTS

Before the line below add the recommended code https://github.com/chieota/restaurant-research/blob/fd4e4768ed2d24154f9dbb26898a75ac17f8f0a2/admin/add_reservation.php#L8

See example.

  <?php
            session_start();

            require_once "header.php";
            require_once "../classes/Restaurant.php";
            $restaurant = new Restaurant;
            $id = $_GET['restaurant_id'];

            //recommended
            $date = '';
            $number_of_people = '';

            if(isset($_GET['date'])){
                  $date = $_GET['date'];
            }
            if(isset($_GET['number_of_people'])){
                  $number_of_people= $_GET['number_of_people'];
            }

            $get_restaurant = $restaurant->selectOne($id);
        ?>

Then in you html code starting from the below modify you code. https://github.com/chieota/restaurant-research/blob/fd4e4768ed2d24154f9dbb26898a75ac17f8f0a2/admin/add_reservation.php#L22

See example.

 <form action="reservation_action.php?action=add" method="post">
                      <input  type="hidden" name="restaurant_id" value="<?php echo $_GET['restaurant_id']?>">    
                      <div class="form-group">
                          <label>Restaurant Name</label>
                          <input type="text" class="form-control" placeholder="Restaurant Name" name="restaurant_name" value="<?php echo $get_restaurant['restaurant_name']; ?>" disabled>
                      </div>
                      <div class="form-group">
                          <label>Date</label>
                          <input type="date" class="form-control" name="date"  value="<?php echo $date; ?>">
                      </div>
                      <div class="form-group">
                          <label>Number of person</label>
                          <input type="number" class="form-control" name="number_of_people"  value="<?php echo $number_of_people; ?>">
                      </div>
                          <input type="submit" value="Add" name="action" class="btn btn-danger">
                          </form>
chieota commented 4 years ago

Hi thank you so much. I put condition for number of people. But it is not working. Am I wrong?

スクリーンショット 2019-11-05 19 33 32
khertg commented 4 years ago

My bad the condition should be like this If($number_of_people < 1)

chieota commented 4 years ago
スクリーンショット 2019-11-05 19 56 58 スクリーンショット 2019-11-05 19 57 23

Regardless the number of people, the adding action is working..

khertg commented 4 years ago

Try this out. If((int)$number_of_people < 1)

chieota commented 4 years ago

Still the same. The number is saved even if it's less than 0.

スクリーンショット 2019-11-05 20 14 59
chieota commented 4 years ago

solved!Thanks!!!! I replaced this code "$result = $reservation->save($restaurant_id,$date,$number_of_people,$_SESSION['user_id']);" inside "if($result){ echo ""; }"

スクリーンショット 2019-11-05 20 22 03