foreachloop going 8 times expected

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

foreachloop going 8 times expected

Post by m3rajk »

from the mysql cli (the cof was made as it is because i was having issues with the adding page trying to update when nothing was there)
mysql> select * from friends;
+-----+------+-----+-------------------+
| uid | fuid | fun | cof |
+-----+------+-----+-------------------+
| 1 | 1 | Neo | add damn you add! |
+-----+------+-----+-------------------+
1 row in set (0.99 sec)

mysql>
the section of code where i format this for display

Code: Select all

foreach($desired as $friend){ # for each friend
	$debug.='making desired'; // debug line
	$fuid=$friend['fuid']; $frnd=$friend['fun']; $cof=$friend['cof']; # break up the info
	$desires.="		    <tr><td><input type="checkbox" name="delf[]" value="$fuid"></td><td><a href="profile.php?un=$frnd" onClick="alert('Your Comment on $frnd: $cof');" target="$frnd">$frnd</a></td></tr>\n"; # make the display
      }
    }
from the table it prints out when echoed
1
1
1
1
N
N
a
a
the source of that

Code: Select all

&lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="delf&#1111;]" value="1"&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="profile.php?un=1" onClick="alert('Your Comment on 1: 1');" target="1"&gt;1 &lt;/td&gt;&lt;/tr&gt;
		    &lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="delf&#1111;]" value="1"&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="profile.php?un=1" onClick="alert('Your Comment on 1: 1');" target="1"&gt;1 &lt;/td&gt;&lt;/tr&gt;
		    &lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="delf&#1111;]" value="1"&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="profile.php?un=1" onClick="alert('Your Comment on 1: 1');" target="1"&gt;1 &lt;/td&gt;&lt;/tr&gt;
		    &lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="delf&#1111;]" value="1"&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="profile.php?un=1" onClick="alert('Your Comment on 1: 1');" target="1"&gt;1 &lt;/td&gt;&lt;/tr&gt;

		    &lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="delf&#1111;]" value="N"&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="profile.php?un=N" onClick="alert('Your Comment on N: N');" target="N"&gt;N &lt;/td&gt;&lt;/tr&gt;
		    &lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="delf&#1111;]" value="N"&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="profile.php?un=N" onClick="alert('Your Comment on N: N');" target="N"&gt;N &lt;/td&gt;&lt;/tr&gt;
		    &lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="delf&#1111;]" value="a"&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="profile.php?un=a" onClick="alert('Your Comment on a: a');" target="a"&gt;a &lt;/td&gt;&lt;/tr&gt;
		    &lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="delf&#1111;]" value="a"&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="profile.php?un=a" onClick="alert('Your Comment on a: a');" target="a"&gt;a &lt;/td&gt;&lt;/tr&gt;
i don't understand what's wrong with the foreach loop that's causing that. $desired is an array of associative arrays. each associative array is the information on the friend of the user. i can't think of anything else that might help.
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post by trollll »

Hmmm... Looking at everything, it seems difficult to pin down a certain thing to look at (I've never seen it return duplicates of the first letter of each field before...). Can you post the results of print_r($desired)?
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

mark (the person that runs a diff forum i mentioned in another thread because i think i see a second crossover from it, and who happens to be reallly great with mysql) pointed out that everything is twice. he's not sure why, but it was acting as both an associative and indexed array even though i was breaking it up, it was breaking it up itself. he suggested a different way that works
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post by trollll »

Glad I could help!

:)
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

np. you've come in on other things and been right, so i know you're good. this just happens to have fallen in somoene else's area of expertise.
Post Reply