foreachloop going 8 times expected

They have: 461 posts

Joined: Jul 2003

edited to stop horizontal scroll

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)

Quote: 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

<?php
      
foreach($desired as $friend){ # for each friend
   
$debug.='making desired'; // debug line
    /* break up the info */
   
$fuid=$friend['fuid']; // friend's user id
   
$frnd=$friend['fun']; // friend's username
   
$cof=$friend['cof'];  // user's comment on friend
   
$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
Quote: 1
1
1
1
N
N
a
a

the source of that

<tr><td><input type="checkbox" name="delf[]" value="1"></td><td>
<a href="profile.php?un=1" onClick="alert('Your Comment on 1: 1');" target="1">1
</td></tr>
    <tr><td><input type="checkbox" name="delf[]" value="1"></td><td>
<a href="profile.php?un=1" onClick="alert('Your Comment on 1: 1');" target="1">1
</td></tr>
    <tr><td><input type="checkbox" name="delf[]" value="1"></td><td>
<a href="profile.php?un=1" onClick="alert('Your Comment on 1: 1');" target="1">1
</td></tr>
    <tr><td><input type="checkbox" name="delf[]" value="1"></td><td>
<a href="profile.php?un=1" onClick="alert('Your Comment on 1: 1');" target="1">1
</td></tr>

    <tr><td><input type="checkbox" name="delf[]" value="N"></td><td>
<a href="profile.php?un=N" onClick="alert('Your Comment on N: N');" target="N">N
</td></tr>
    <tr><td><input type="checkbox" name="delf[]" value="N"></td><td>
<a href="profile.php?un=N" onClick="alert('Your Comment on N: N');" target="N">N
</td></tr>
    <tr><td><input type="checkbox" name="delf[]" value="a"></td><td>
<a href="profile.php?un=a" onClick="alert('Your Comment on a: a');" target="a">a
</td></tr>
    <tr><td><input type="checkbox" name="delf[]" value="a"></td><td>
<a href="profile.php?un=a" onClick="alert('Your Comment on a: a');" target="a">a
</td></tr>
'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.

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

From the 2nd quote, I can tell that your foreach() looping through a single record within a recordset. How can I tell? mysql_fetch_array() returns an array with both numeric and associative keys. That's why everything is repeating itself twice.

Copy & Paste the code that populates $desired. $desired can't be an array of associative arrays. It's simply the first record of the recordset.

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 461 posts

Joined: Jul 2003

<?php
$friendfind
=mysql_query(\"SELECT * FROM friends WHERE uid='$uin'\", $db); # get friends
if(mysql_num_rows(
$friendfind)==0){
 
$ferr='<h1>No Desires/Lusts Found</h1>'; # error on getting friends
}else{ # get the desires
 
$ppl=mysql_num_rows($friendfind); # how many friends?
 
$debug.=\"ppl : $ppl\";
  for(
$i=0;$i<$ppl;$i++){ # for each friend
 
$debug.='<br />fetching desire';
 
$desired=mysql_fetch_array($friendfind); # get the info
  }
  /* previously posted code */
}
?>
that's is, with where the other code is

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

<?php
$friendfind
= mysql_query(\"SELECT * FROM friends WHERE uid='$uin'\", $db); # get friends
if(mysql_num_rows(
$friendfind)==0) {
   
$ferr='<h1>No Desires/Lusts Found</h1>'; # error on getting friends
}
else { # get the desires
   
$ppl=mysql_num_rows($friendfind); # how many friends?
   
$debug.=\"ppl : $ppl\";
    for (
$i=0;$i<$ppl;$i++) { # for each friend
       
$debug .= '<br />fetching desire';
       
$desired[] = mysql_fetch_array($friendfind); # get the info
    }
    foreach(
$desired as $friend) { # for each friend
       
$debug .= 'making desired'; // debug line
        /* break up the info */
       
$fuid=$friend['fuid']; // friend's user id
       
$frnd=$friend['fun']; // friend's username
       
$cof=$friend['cof'];  // user's comment on friend
       
$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
    }
}
?>
Your sucking the entire recordset into memory. Not advised unless you need this data for later.
<?php
$friendfind
= mysql_query(\"SELECT * FROM friends WHERE uid='$uin'\", $db); # get friends
if(mysql_num_rows(
$friendfind)==0) {
   
$ferr='<h1>No Desires/Lusts Found</h1>'; # error on getting friends
}
else { # get the desires
   
$ppl=mysql_num_rows($friendfind); # how many friends?
   
$debug.=\"ppl : $ppl\";
    while (
$friend=mysql_fetch_array($friendfind)) {
       
$debug .= 'making desired'; // debug line
        /* break up the info */
       
$fuid=$friend['fuid']; // friend's user id
       
$frnd=$friend['fun']; // friend's username
       
$cof=$friend['cof'];  // user's comment on friend
       
$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
    }
}
?>

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 461 posts

Joined: Jul 2003

i use * because i use each part of the feild. it works perfectly now. thanks.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.