For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > November 2004 > How does this work?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How does this work?
LB

2004-11-27, 3:55 pm

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
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



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com