Page 1 of 1

foreachloop going 8 times expected

Posted: Fri Sep 05, 2003 8:30 pm
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.

Posted: Sat Sep 06, 2003 12:51 pm
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)?

Posted: Sat Sep 06, 2003 5:26 pm
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

Posted: Sat Sep 06, 2003 6:52 pm
by trollll
Glad I could help!

:)

Posted: Sat Sep 06, 2003 7:18 pm
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.