nickelus Posted July 27, 2009 Share Posted July 27, 2009 i am becoming increasingly concerned that i may never understand the proper use of foreach(). every time i execute a for each loop what happens is i end up with the only the last result of the array what is happening? how can this be interpreted Quote Link to comment Share on other sites More sharing options...
corbin Posted July 27, 2009 Share Posted July 27, 2009 Errr..... What exactly do you not understand about foreach loops? Can we see an example of one of your erroneous code snippets? If I had to guess, I would guess you're setting a variable inside a foreach loop meaning it will be reset each iteration. Quote Link to comment Share on other sites More sharing options...
gevans Posted July 27, 2009 Share Posted July 27, 2009 Give us an example of your implementation of a foreach() loop EDIT: what corbin said Quote Link to comment Share on other sites More sharing options...
nickelus Posted July 27, 2009 Author Share Posted July 27, 2009 heres an example and you're right but i cant think of where else i would set the variable when i am try to use the results in a table as (echo $each) while($row=mysql_fetch_array($widgets)){ $each="<tr> <td>".$row[0]."</td> <td>".$row[1]."</td> <td>".$row[2]."</td> <td>".$row[3]."</td> <td>".$row[4]."</td> <td>".$row[5]."</td></tr>";} Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted July 27, 2009 Share Posted July 27, 2009 You need to echo each "row" or set of data within the loop... if you wait and echo or print it outside of the loop, the loop runs and overwrites its variables from the last time around, leaving you with the last item in the set like: $items = range(1,50); echo "Before the loop<br />"; foreach($items as $k => $v) { echo $v."<br />"; } echo "After the loop"; Or in your example echo "<table>"; while($row=mysql_fetch_array($widgets)){ $each="<tr> <td>".$row[0]."</td> <td>".$row[1]."</td> <td>".$row[2]."</td> <td>".$row[3]."</td> <td>".$row[4]."</td> <td>".$row[5]."</td></tr>"; echo $each; } echo "</table>"; Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted July 27, 2009 Share Posted July 27, 2009 heres an example and you're right but i cant think of where else i would set the variable when i am try to use the results in a table as (echo $each) while($row=mysql_fetch_array($widgets)){ $each="<tr> <td>".$row[0]."</td> <td>".$row[1]."</td> <td>".$row[2]."</td> <td>".$row[3]."</td> <td>".$row[4]."</td> <td>".$row[5]."</td></tr>";} That's because a while loop functions differently than a foreach loop. A while loop continues as long as the statement is true (or rather "while it's true it continues"). mysql_fetch_array() has an internal cursor that it moves forward each time you call it. As long as you've not exceeded the amount of rows returned it will return a value that can evaluate to true. That is why you can get all the rows using while, and why it will not work with foreach (because foreach takes a preexisting array and iterates over all the elements). If you wanted to output all the fields in a row you could do like this: while ($row = mysql_fetch_array($widgets)) { echo '<tr>'; foreach ($row as $field) { echo '<td>' . $field . '</td>'; } echo '</tr>'; } Quote Link to comment Share on other sites More sharing options...
nickelus Posted July 27, 2009 Author Share Posted July 27, 2009 thanks, still not my forte but i'm getting better results now. i was using do do/while and didn't like the results Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted July 28, 2009 Share Posted July 28, 2009 You should really take the time to understand looping... it is one of the most basic building blocks of any language. Not taking the time to understand them fully will come back to bite you Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted July 28, 2009 Share Posted July 28, 2009 it is one of the most basic building blocks of any language. Haskell doesn't have any loops. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 28, 2009 Share Posted July 28, 2009 http://www.php-learn-it.com/php_foreach_loop.html Start here. Read carefully and slowly through this page. Then go to next page, and read. All throughout all the pages pertaining to loops. Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted July 28, 2009 Share Posted July 28, 2009 it is one of the most basic building blocks of any language. Haskell doesn't have any loops. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.