0

I have a form with multiple rows and multiple "select" drop-down menus in each row. For simplicity I am showing 2x2 in this example but in reality it could go up to 5x5.

In simplified form, my HTML form looks like this:

<form action="aaa2.php" method="post" name="book">

<?php $roomNumber=101; ?> 
Normal Occupancy 
<select name="occupancy[101]"> 
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
Extra Beds
<select name="extrabed[101]"> 
<option value="1">1</option>
<option value="2">2</option>
</select>
<br><br>
<?php $roomNumber=101; ?>
Normal Occupancy 
<select name="occupancy[102]"> 
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
Extra Beds
<select name="extrabed[102]"> 
<option value="1">1</option>
<option value="2">2</option>
</select>
<br><br>

<input name="Submit" type="submit" value="submit">
</form>

On the next page, I can easily display the information from the first drop-down:

<?php 
$occupancy_array=$_POST['occupancy'];
foreach($occupancy_array as $roomNumber=>$occupancy) {
echo "Room ".$roomNumber.". Normal occupancy is ".$occupancy.".<br>";   
}
?>

My problem comes when I try to use the information from the second drop-down. I've got this far

<?php 
$occupancy_array=$_POST['occupancy'];
$extrabed_array=$_POST['extrabed'];
foreach($occupancy_array as $roomNumber=>$occupancy) {
echo "Room ".$roomNumber.". Normal occupancy is ".$occupancy." and the room will take ".$extrabed." extra beds.<br>";   
}
?>

but obviously I need another foreach line for the second array. I've tried it in series and I've tried it nested inside the first foreach, but neither works right. Is there a way that I can combine it all into one foreach? Or how should I do it?

4
  • You mean combine occupancy and extrabed together? Commented Oct 25, 2013 at 16:59
  • Yes. I need to be able to construct the next page with occupancy and extra bed information for each roomNumber. Commented Oct 25, 2013 at 17:01
  • So what's the problem? You don't want to have two foreachs? Commented Oct 25, 2013 at 17:02
  • I think I got your point. You can use $roomNumber to access extra beds. Something like $extrabed_array[$roomNumber] in your foreach. Commented Oct 25, 2013 at 17:04

1 Answer 1

2

Try this:

<?php 

$occupancy_array=$_POST['occupancy'];
$extrabed_array=$_POST['extrabed'];

foreach($occupancy_array as $roomNumber => $occupancy) {
    echo "Room $roomNumber. Normal occupancy is $occupancy and the room will take ".$extrabed_array[$roomNumber]." extra beds.<br>";   
}

You should take care to injections, because any user can send a request with

occupancy[<script>...</script>] = <script>...</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Recode. That worked perfectly. Now I'm going to charge on putting that logic into the real thing. I take your point about injections, but this was just a very simple example to address my problem. I cut out all the superfluous stuff.
@TrapezeArtist yes, it was just a notice, there are enough people forgetting that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.