I'm having issues with understanding how to use the foreach function. I've been reading the PHP manual and looking around on here for some help, but I have been able to grasp the concepts.
What I'm wanting to do is create a navigation menu that is database driven. I am still pretty new to PHP, and I figure that the foreach would help me with this, however, I cannot figure out how to do this.
Thanks for any help!
Luke
issues with foreach (database driven menu)
Moderator: General Moderators
-
the9ulaire
- Forum Commoner
- Posts: 74
- Joined: Mon Jun 11, 2007 11:31 am
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
The foreach loop takes the array, the index, and the alias of each piece of data. It's not too complicated. It's a way of looping through each element in the order that they are in the array / object.
Running that code should give you an idea.
Code: Select all
$test = array('one' => 1, 'two' => 2, 0 => 'element');
foreach($test as $index => $value)
{
echo $index . ' => ' . $value . "<br />\n";
}-
the9ulaire
- Forum Commoner
- Posts: 74
- Joined: Mon Jun 11, 2007 11:31 am
Thanks for that! It was helpful. However, I still am not sure how I go about using that over something I am querying from a database. I want to echo a few different things from each row.
After doing:
So I have my array. I don't understand how I use the foreach statement and retrieve various values. I want it to echo:
for each row from my query. How do I set up my foreach statement (array_expression as $key => $value)?
Thanks!
Luke
After doing:
Code: Select all
$sql = "SELECT * FROM pages ORDER BY page_id";
$result = mysql_query($sql, $conn);
$row = mysql_fetch_array($result);Code: Select all
echo "<a href=\"index.php?p=" . $row['page_id'] . "\" title=\"" .
$row['page_desc'] . "\">" . $row['page_name'] . "</a>";
echo " | ";Thanks!
Luke
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
That wouldn't be a foreach statement, it'd be a while loop. However, the foreach loop can give you an idea of how the while loop would work for query results.
A foreach loop goes from one index to the next of an array, and the reason that it is possible to do this is because the array has a pointer than keeps track of the current position in the array. Whenever the foreach loop starts another iteration, the pointer goes to the next index of the array.
Results sets are set up similar in that they have a pointer that signifies the current point in the result set that you are at. Each time that you call a mysql_fetch_* function, it gives you the data from that particular row of the result set, and then moves the pointer to point at the next row in the result set. Every time that you call a fetch function, it will do this until there are no rows left.
So, using a while loop would just be like this:
This code will continue looping while the statement "$row = mysql_fetch_array()" (which evaluates down to just the value of $row after running the mysql_fetch_array() function) has a value, continue. Once it doesn't have a value (i.e. NULL), then the loop is over.
Run it and you should get an idea of what's happening.
A foreach loop goes from one index to the next of an array, and the reason that it is possible to do this is because the array has a pointer than keeps track of the current position in the array. Whenever the foreach loop starts another iteration, the pointer goes to the next index of the array.
Results sets are set up similar in that they have a pointer that signifies the current point in the result set that you are at. Each time that you call a mysql_fetch_* function, it gives you the data from that particular row of the result set, and then moves the pointer to point at the next row in the result set. Every time that you call a fetch function, it will do this until there are no rows left.
So, using a while loop would just be like this:
Code: Select all
while($row = mysql_fetch_array())
{
echo '<pre>' . print_r($row, true) . '</pre>';
}Run it and you should get an idea of what's happening.
-
the9ulaire
- Forum Commoner
- Posts: 74
- Joined: Mon Jun 11, 2007 11:31 am