Faxsy Faxsy - 2 months ago 6
PHP Question

Two Selects with same data

I want to select a versus of teams, I have made two selects and show the teams from database: what I have did is that :

<p>
<label>Teams</label>
<select id="team1" name="team1">
<option disabled selected value>Team 1</option>
<?php

while ($team = $db->fetch_array($teams))
{
echo renderTeamsSelect($team);
}
$db->free_result($teams);

?>
</select>
VS
<select id="team2" name="team2">
<option disabled selected value>Team 2</option>
<?php

while ($team = $db->fetch_array($teams))
{
echo renderTeamsSelect($team);
}
$db->free_result($teams);

?>
</select>
</p>


Note that $team is :

$teams = $db->query('SELECT * FROM teams');


and my function renderTeamSelect is :

function renderTeamsSelect($team)
{
return '<option value="' . $team['id'] . '">' . $team['name'] . '</option>';
}


Once I see the result in html , team 2 is not showing anything in options.

I included a screenshot

Error

I can't understand why Select one is showing results and select 2 no

Answer

A far better approach:-

<?php

$teams = $db->query('SELECT * FROM teams');
$final_data = '';
 while ($team = $db->fetch_array($teams))
{
    $final_data .=  renderTeamsSelect($team);
}
function renderTeamsSelect($team)
{
return '<option value="' . $team['id'] . '">' . $team['name'] . '</option>';
}

?>

<p>  
    <label>Teams</label>
    <select id="team1" name="team1">
      <option disabled selected value>Team 1</option>
      <?php  echo $final_data ;?>
    </select>
    VS
    <select id="team2" name="team2">
      <option disabled selected value>Team 2</option>
        <?php  echo $final_data ;?>
    </select>
</p>
Comments