| Cameron 2004-11-29, 3:59 pm |
| LB wrote:
> I'm using a book to learn PHP (bad one as it may be). I'm a little
> how they got this to work. They are using this query to
> populate 2 select boxes.
>
> $people_sql = "SELECT *
> FROM people
> ORDER BY people_fullname";
>
> $people_results = mysql_query($people_sql)
> or die (mysql_error());
>
> while ($row = mysql_fetch_assoc($people_results)) {
> $people[$row['people_id']] = $row['people_fullname'];
> }
>
> //to populate select box
> foreach ($people as $people_id => $people_fullname) {
> echo "<option value=\"" . $people_id . "\">" . $people_fullname .
> "</option>";
> }
>
> I'm not really sure about the while statement and the code that it runs.
> I had tried to do it using a while to populate each select box...didn't
> work and I'm not sure what the failure in my logic is (or about the
> while statement. Thanks for the help.
>
> Erick
You can't do it with a while statement in that form, foreach is used to
iterate through each value of an array, so that each time it loops
$people_id will contain the key of the next element from $people and
$people_fullname will contain the value of the array element.
Ref: http://uk.php.net/manual/en/control...res.foreach.php
~Cameron
|