Jump to content

foreach array


unistake

Recommended Posts

Hi guys,

 

Having trouble trying to show values from my $friend array. Only the first friend code is being displayed in my output. 

There are several fields in my mysql associated with the $sql.

 

Any ideas? 

 

Thanks

$sql = "SELECT * FROM friends WHERE Code = '$code' OR FriendCode = '$code' AND Status ='A'";
$result = mysqli_query($cxn,$sql)
	or die ("Can't get friends.");

$friend = array();
while($row=mysqli_fetch_array($result)) {
	if($code == $row['FriendCode']) { $friend[] = $row['Code']; }
	elseif($code == $row['Code']) { $friend[] = $row['FriendCode'];
}	
	
	foreach($friend as $key => $value) {
             echo $value.'<br />';
}
Link to comment
https://forums.phpfreaks.com/topic/289193-foreach-array/
Share on other sites

I would imagine that the query should return results which have "Code" equal to $code and "Status" equal to 'A'...or "FriendCode" equal to $code and "Status" equal to 'A'. If that's the case, you'll want to add parenthesis around Code and FriendCode:

 

$sql = "SELECT * FROM friends WHERE (Code = '$code' OR FriendCode = '$code') AND Status ='A'";
Link to comment
https://forums.phpfreaks.com/topic/289193-foreach-array/#findComment-1482829
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.