Reputation: 137
So i have multiple drop downs on a page. But i need them to get their records from a single database query such that all drop downs would have identical lists. I have tried to achieve that but what i get is just the first drop down getting populated. This is what i have done below and need help.
<select name="pl2a" class="input-field-login3" id="pl2a" tabindex="1">
<option selected="Selected">--Select Player--</option>
<?php do { ?>
<option value="<?php echo $row_playersa['plyName']; ?>"><?php echo $row_playersa['plyName']; ?></option>
<?php } while ($row_playersa = mysqli_fetch_assoc($result_playersa)); ?>
</select>
i used this repeatedly for all the drop downs by just changing the name to differentiate. But only the first drop down gets populated.
Upvotes: 0
Views: 411
Reputation: 41810
mysqli_fetch_assoc
consumes the rows from the result set as you use it. So after the first dropdown is generated, you have reached the end of the result set and further calls to mysqli_fetch_assoc
will return null
. To resolve this, store your results in an array as you fetch them before outputting any of the dropdowns. The array will be reusable.
So do this once:
<?php
while ($row_playersa = mysqli_fetch_assoc($result_playersa)) {
$players[] = $row_playersa;
}
?>
And then you can do this as many times as you need to:
<select name="pl2a" class="input-field-login3" id="pl2a" tabindex="1">
<option selected="Selected">--Select Player--</option>
<?php foreach ($players as $player): ?>
<option value="<?php echo $player['plyName'] ?>"><?php echo $player['plyName'] ?></option>
<?php endforeach ?>
</select>
Upvotes: 1