unistake Posted May 19, 2014 Share Posted May 19, 2014 Hi all, I have a looping error in my PHP. I am trying to add use a while statement to gather the users first and last name in to a string, add them to an array called $friendname, then check that the $friendname[] is only output/echoed once. At the moment the code looks good to me but with multiple rows with the same name associated, the name is echoed the same amount of times as rows, instead of once as it is supposed to be. Any ideas anyone? while($row2 = mysqli_fetch_assoc($result2)) { $friendcode = $row2['Code']; $names = "SELECT FirstName, LastName from users WHERE Code = '$friendcode' AND Code != '$crewcode'"; $resultnames = mysqli_query($cxn,$names) or die ("Can't get friends names."); while($rownames = mysqli_fetch_assoc($resultnames)) { $friendname = array(); $newfriendname = ($rownames['FirstName'].' '.$rownames['LastName']); if (!in_array($newfriendname, $friendname)) { array_push($friendname, "$newfriendname"); echo $newfriendname; } } } Quote Link to comment Share on other sites More sharing options...
Solution ginerjm Posted May 19, 2014 Solution Share Posted May 19, 2014 Program much? Cause you have some pretty bad logic errors here. You initialize your friends array every iteration of your while loop. Therefore you will never get a hit in your check for dupes. You really should combine your second query with the first one (which you didn't show us) and avoid this coding completely. Quote Link to comment Share on other sites More sharing options...
Cornelius Posted May 19, 2014 Share Posted May 19, 2014 I am trying to add use a while statement to gather the users first and last name in to a string, add them to an array called $friendname, then check that the $friendname[] is only output/echoed once. I still have no idea what you're trying to do. Where from do you gather that first and last name? From database? By which criteria? It seems like you're checking if it's already inside? 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.