我有一个复选框和一个选择下拉菜单,用于选择一个晚餐课程,该课程将循环显示4个房间。当我选择多个房间时,会得到以下输出:
大象室
三道菜
两道菜
狮子室
三道菜
两道菜
如果我为大象房选择了两道菜,我会这样:
大象室
两道菜
如果我为狮子房选择三道菜,我想要这样做:
狮子室
三道菜
我已经尝试通过与众不同的方式以多种方式修改foreach循环。我还不是循环专家,而且还没有循环专家。
HTML
<?php while ( have_posts() ) : the_post(); ?>
<form name="book-room-form" action="" id="contactForm" method="post">
<div>
<label class="form-check-label selected-label" for="room-selected">Select</label>
<input type="checkbox" class="form-check-input" id="room-selected" name="room-selected[]" value="<?php echo $room_name; ?>">
</div>
<select name="dinner-select[]" required>
<option value="0" selected>Select Dinner Course</option>
<option value="120">Two Course Dinner</option>
<option value="200">Three Course Dinner</option>
</select>
<button name="submit-request" type="submit" class="btn btn-primary">Submit</button>
</form>
<?php endwhile; // end of the loop. ?>
PHP
if(isset($_POST['submit-request']))
{
$room_selected = $_POST['room-selected'];
$numPeople = $_POST['people-select'];
$dinnerSelect = $_POST['dinner-select'];
$roomoption = isset($room_selected) ? $room_selected : false;
if ($roomoption) {
foreach ($room_selected as $room){
if($room){
echo $room . '<br/>';
$dinneroption = isset($dinnerSelect) ? $dinnerSelect : false;
if ($dinneroption) {
foreach ($dinnerSelect as $dinn){
if($dinn >= 200){
echo 'three course <br/>';
}
elseif($dinn == 120) {
echo 'two course <br/>';
}
}
}
}
}
}
}
有什么办法可以解决这个问题吗?
答案 0 :(得分:0)
似乎很奇怪,您将循环表单,并且在每个表单中仅包含单个文本输入和单个选择菜单-似乎更有意义的是,您将拥有单个表单然后在输入元素周围循环,如下所示:
<!-- can leave the ID for the form as it is outside the loop -->
<form name="book-room-form" id="contactForm" method="post">
<?php while ( have_posts() ) : the_post(); ?>
<!--- to segregate the looped form elements -->
<fieldset>
<div>
<label class="form-check-label selected-label" for="room-selected">Select</label>
<!-- remove id attribute from input -->
<input type="checkbox" class="form-check-input" name="room-selected[]" value="<?php $room_name; ?>">
</div>
<select name="dinner-select[]" required>
<option value="0" selected>Select Dinner Course
<option value="120">Two Course Dinner
<option value="200">Three Course Dinner
</select>
</fieldset>
<?php endwhile;?>
<!-- a single "submit" button rather than however many were generated by the loop -->
<button name="submit-request" type="submit" class="btn btn-primary">Submit</button>
</form>
其中的PHP部分无法由我轻松测试,因此可能存在问题,但是您可以对其进行一些修改
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['submit-request'], $_POST['room-selected'], $_POST['people-select'], $_POST['dinner-select'] ) ){
$room_selected = filter_input( INPUT_POST, 'room-selected', FILTER_SANITIZE_STRING );
$numPeople = filter_input( INPUT_POST, 'people-select', FILTER_SANITIZE_NUMBER_INT );
$dinnerSelect = filter_input( INPUT_POST, 'dinner-select', FILTER_SANITIZE_NUMBER_INT );
if( $room_selected & $numPeople & $dinnerSelect ){
foreach( $room_selected as $room ){
if( $room ){
echo $room . '<br/>';
foreach( $dinnerSelect as $value ){
switch( intval( $value ) ){
case 120:echo 'two course <br/>';break;
case 200:echo 'three course <br/>';break;
}
}
}
}
}
}