Reputation: 420
I have 5 rooms in a table for a guest house and a user must check the room or rooms they wish to book, and select the options they wish to have like the number of people, the breakfast option, dinner option and bed option.
Let's say we call the 5 rooms in order of appearance:
Element Room
Earth Room
Water Room
Air Room
Fire Room
I get option the results if I select the rooms in top to bottom order like Element to Fire rooms, but I get 0 if I select any room inbetween. The room's name always displays correctly when checking them from checkbox, but the option values display 0, except for the first room (Element)
HTML:
<form class="booking-form" name="book-room-form" action="" id="contactForm" method="post" novalidate>
<?php foreach ( $rooms as $room ) : ?>
<input type="checkbox" class="form-check-input" name="room-selected[]" value="<?php echo $room->post_title; ?>">
<select class="num-person select-update" id="<?php echo 'r-' . $room->ID; ?>" name="people-select[]" required>
<option value="0" selected>Select Number of Persons</option>
<option value="1">1 person R600</option>
<option value="2">2 persons R800</option>
</select>
<select class="num-dinner select-update" id="<?php echo 'd-' . $room->ID; ?>" name="dinner-select[]">
<option value="0" selected>Select Dinner Course</option>
<option value="120">Two Course Dinner R120</option>
<option value="200">Three Course Dinner R200</option>
</select>
<select class="num-bf select-update" id="<?php echo 'b-' . $room->ID; ?>" name="breakfast-select[]">
<option value="0" selected>Select Breakfast Type</option>
<option value="70">Basic Breakfast R70</option>
<option value="120">Full Breakfast R120</option>
</select>
<select class="bed-select" name="bed-select[]">
<option selected>Select Bed Size</option>
<option value="King Bed">King Bed</option>
<option value="2 Single Beds">2 Single Beds</option>
</select>
<?php endforeach; ?>
<button name="submit-request" type="submit" class="btn btn-primary">Submit</button>
</form>
CODE:
if(isset($_POST['submit-request'])) {
$room_selected = $_POST['room-selected'];
$numPeople = $_POST['people-select'];
$dinnerSelect = $_POST['dinner-select'];
$breakfastSelect = $_POST['breakfast-select'];
$bedSelect = $_POST['bed-select'];
$room = $room_selected;
$num = $numPeople;
$dinn = $dinnerSelect;
$bf = $breakfastSelect;
$bed = $bedSelect;
foreach ($room as $id => $key) {
//$key returns the room name
if($key) {
$result[$key] = array(
'num_person' => $num[$id],
'dinner' => $dinn[$id],
'breakfast' => $bf[$id],
'bed_type' => $bed[$id],
);
}
echo $key . '<br/>' . $num[$id] . '<br/>' . $dinn[$id] . '<br/>' . $bf[$id] . '<br/>' . $bed[$id] . '<br/>' ;
}
}
I want to be able to select any room and have the option values displayed
Upvotes: 0
Views: 1969
Reputation: 964
I would say its because your not using any keys in your html form arrays. The current solution is a bit fragile so you either need to be more specific in the field naming or include the key in the array.
<?php foreach ( $rooms as $room ) : ?>
<input type="checkbox" class="form-check-input" name="room-selected[]" value="<?php echo $room->post_title; ?>">
<select class="num-person select-update" id="<?php echo 'r-' . $room->ID; ?>" name="people-select[<?php echo $room->post_title; ?>]" required>
<option value="0" selected>Select Number of Persons</option>
<option value="1">1 person R600</option>
<option value="2">2 persons R800</option>
</select>
<select class="num-dinner select-update" id="<?php echo 'd-' . $room->ID; ?>" name="dinner-select[<?php echo $room->post_title; ?>]">
<option value="0" selected>Select Dinner Course</option>
<option value="120">Two Course Dinner R120</option>
<option value="200">Three Course Dinner R200</option>
</select>
<select class="num-bf select-update" id="<?php echo 'b-' . $room->ID; ?>" name="breakfast-select[<?php echo $room->post_title; ?>]">
<option value="0" selected>Select Breakfast Type</option>
<option value="70">Basic Breakfast R70</option>
<option value="120">Full Breakfast R120</option>
</select>
<select class="bed-select" name="bed-select[<?php echo $room->post_title; ?>]">
<option selected>Select Bed Size</option>
<option value="King Bed">King Bed</option>
<option value="2 Single Beds">2 Single Beds</option>
</select>
<?php endforeach; ?>
Then when running your loop instead of checking for $id use the $key.
if(isset($_POST['submit-request'])) {
$room_selected = $_POST['room-selected'];
$numPeople = $_POST['people-select'];
$dinnerSelect = $_POST['dinner-select'];
$breakfastSelect = $_POST['breakfast-select'];
$bedSelect = $_POST['bed-select'];
$room = $room_selected;
$num = $numPeople;
$dinn = $dinnerSelect;
$bf = $breakfastSelect;
$bed = $bedSelect;
foreach ($room as $id => $key) {
//$key returns the room name
if($key) {
$result[$key] = array(
'num_person' => $num[$key],
'dinner' => $dinn[$key],
'breakfast' => $bf[$key],
'bed_type' => $bed[$key],
);
}
echo $key . '<br/>' . $num[$key] . '<br/>' . $dinn[$key] . '<br/>' . $bf[$key] . '<br/>' . $bed[$key] . '<br/>' ;
}
}
Upvotes: 1