Jump to content

foreach array


Go to solution Solved by cyberRobot,

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 />';
}
Edited by unistake
Link to comment
https://forums.phpfreaks.com/topic/289193-foreach-array/
Share on other sites

  • Solution

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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