Jump to content

still afraid of foreach


nickelus

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.