randombits
randombits

Reputation: 48450

PHP two foreach loops, inner foreach not working

Simple view built with PHP. I am trying to populate <option> tags with an inner loop. The form loads, but only the option already selected for the player is viewable. The others are never shown. I confirm with a print_r that $query_awards is just fine and populated:

<?php
//print_r($query_players);
print_r($query_awards);
foreach ($query_players as $row):
    echo "<form method=POST action=player_update><table><tr> <input type=hidden name=player_id value=".$row->id.">";
    echo "<td><input type=text name=first_name value=".$row->first_name."></td>";
    echo "<td><input type=text name=last_name value=".$row->last_name."></td>"; 
    echo "<td><input type=text name=school_name value='".$row->school_name."'></td>";
    echo "<td><select name=\"award_id\"><option selected value=".$row->award_id.">".$row->award_name."</option>";
    foreach ($query_awards as $award_row): // <-- Broken?
      "<option value=".$award_row->id.">".$award_row->name."</option></select></td>";
    endforeach;
    echo "<td><input type=submit name=submit value=Update></td>";

    echo "</tr></table></form>";
endforeach;
?>

The bothersome part is that the </select> from the inner loop is showing up.

Upvotes: 0

Views: 129

Answers (1)

Nir Alfasi
Nir Alfasi

Reputation: 53525

You forgot the echo in the beginning of the line

"<option value=".$award_row->id.">".$award_row->name."</option></select></td>";

Upvotes: 3

Related Questions