Reputation: 589
I'm creating a team allocation table that allows me to use a dropdown to allocate team members to a team. I have two tables:
members
- This stores the persons information map_team
- This is the team informationI'm preparing a statement for both tables to execute the foreach:
members
statement becomes $Team_allocation_row
map_team
statement becomes $Team_List_Row
Here is my code so far:
<tbody>
<tr>
<th scope="col">Name</th>
<th scope="col">Team</th>
</tr>
<?php $item_type=null; ?>
<?php if($Team_allocation->rowCount() > 0) {
foreach ($Team_allocation->fetchAll() as $Team_allocation_row) { ?>
<tr>
<td><?php echo ucwords($Team_allocation_row['firstname'])." ".ucwords($Team_allocation_row['surname']); ?></td>
<td>
<input type="hidden" name="id[]" value="<?php echo $Team_allocation_row['memberID']; ?>">
<select name="outcome[]">
<?php foreach ($Team_List->fetchAll() as $Team_List_Row) { ?>
<option <?php if($Team_allocation_row['map_team'] == $Team_List_Row['id']) { echo "selected";} ?> value="0"><?php echo $Team_List_Row['name']; ?></option>
<?php } ?>
</select>
</td>
</tr>
<?php } } else { ?>
<tr>
<td>No Users Found</td>
</tr>
<?php } ?>
</tbody>
The above code only outputs the dropdown list on the the first $Team_allocation_row
Any advice would be great and apologies if I'm not using the correct terminology.
Upvotes: 0
Views: 75
Reputation: 94642
You will have consumed all the result set for $Team_List
the first time round the outer foreach loop, therefore on the second iteration of the outer loop the $Team_List
result set will be empty.
So instead get the $Team_List
results and place them in an array that you can reuse multiple times.
<?php
$teamlist = $Team_List->fetchAll();
?>
<tbody>
<tr>
<th scope="col">Name</th>
<th scope="col">Team</th>
</tr>
<?php $item_type=null; ?>
<?php if($Team_allocation->rowCount() > 0) {
foreach ($Team_allocation->fetchAll() as $Team_allocation_row) { ?>
<tr>
<td><?php echo ucwords($Team_allocation_row['firstname'])." ".ucwords($Team_allocation_row['surname']); ?></td>
<td>
<input type="hidden" name="id[]" value="<?php echo $Team_allocation_row['memberID']; ?>">
<select name="outcome[]">
<?php foreach ($teamlist as $Team_List_Row) { ?>
// ---changed here to ^^^^^^^^^
<option <?php if($Team_allocation_row['map_team'] == $Team_List_Row['id']) { echo "selected";} ?> value="0"><?php echo $Team_List_Row['name']; ?></option>
<?php } ?>
</select>
</td>
</tr>
<?php } } else { ?>
<tr>
<td>No Users Found</td>
</tr>
<?php } ?>
</tbody>
Upvotes: 2