lundi 22 mai 2017

Disable and select certain checkboxes using database values

I'm working on a reservation system for a cinema. I have a table called Reservation with columns: movie_id, customer_id, seatnumbers and time_id.

On the page where you can select the seats that you wanted, I'd like to disable and check the checkboxes who are already chosen.

This is my seats.php file (the page where you choose the seats):

<form method="POST">
    <table>
        <tr>
            <?php 
            $rijStoelen = 20;  // seat rows
            $aantalRijenG = 30;  // number of rows
            for($x=1; $x<=$aantalRijenG; $x++){
                echo '<div class="totalRij">';  // total rows
                    echo '<div class="rijStoelen">'.$x.'</div>';
                    echo '<div class="groepjeStoelen">';  // group of seats
                        for($i = 1; $i <= $rijStoelen; $i++){
                            if(($i % 4) ==1){
                                echo '</div>';
                                echo '<div class="groepjeStoelen">';  // group of seats
                            }
                            echo '<div class="enkeleStoel">';  // single seat
                                if(isset($_POST['chbstoel']) && in_array($x.'-'.$i , $_POST['chbstoel']) && count($_POST['chbstoel']) == $_GET['pers']){
                                    echo '<input class="button1" type="checkbox" value="'.$x. '-'. $i. '" checked  disabled name="chbstoel[]" id="chbstoel"/>';
                                }elseif(isset($_POST['chbstoel']) && !in_array($x.'-'.$i , $_POST['chbstoel']) && count($_POST['chbstoel']) == $_GET['pers']){
                                    echo '<input class="button1" type="checkbox" value="'.$x. '-'. $i. '" disabled name="chbstoel[]" id="chbstoel"/>';
                                }elseif(isset($_POST['chbstoel']) && in_array($x.'-'.$i , $_POST['chbstoel']) && count($_POST['chbstoel']) != $_GET['pers']){
                                    echo '<input class="button1" type="checkbox" value="'.$x. '-'. $i. '" checked name="chbstoel[]" id="chbstoel"/>';
                                }else{
                                    echo '<label><input class="button1" type="checkbox" value="'.$x. '-'. $i. '" name="chbstoel[]" id="chbstoel"/></label>';
                                }
                                //echo '<input class="button1" type="checkbox" value="'.$x. '-'. $i. '" name="chbstoel[]" id="chbstoel"/>';
                            echo '</div>';
                        }
                    echo '</div>';
                echo "</div>";  
            }   
            ?>              
        </tr>
    </table>
</form>

I'm not actually inserting it here. I will insert it later, when I have all the data that is needed for the table.

There are seatnumbers in my database, so there is data. This is an other file were I insert everything into database.

if($movie_id != "" || $customer_id != "" || $seats != "" || $time_id != "")
        {
            $con = mysqli_connect("localhost", "root", "", "biosdb") or die ("Connection died!");

            mysqli_query($con, "INSERT into reservation VALUE (0, '$movie_id', '$customer_id', '$seats', '$time_id')") or die ("Query broken");
            echo "Reservation succeeded!";
        }
        else
        {
            echo "Something went wrong";
        }

So to be clear: The checkboxes that are already chosen needs to be selected and disabled. Every seat has his own value what can be the value in the database. In my database is insert that value. Something like: 6-12 (row 6, seat 12)




Aucun commentaire:

Enregistrer un commentaire