Jump to content

Array looping error


unistake

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/288589-array-looping-error/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/288589-array-looping-error/#findComment-1479991
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/288589-array-looping-error/#findComment-1480016
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.