Jump to content

Desplaying something if a value is found in an array?


Alex1646

Recommended Posts

Hello.

I have a system worked out where I have 2 columes for friends in my database (login_info).

One of these is requests and the other is friends. Each request/friend is seperated with a underscore (_). I use the explode() function to seperate the friends/requests into a array. My only problem is I want to display the a different text  if the user has sent a requests or is already friends with the user. I tried to do this using the array_search() function and converting it into a boolean (with the boolean cast). It doesn't seem to be working.

<?php 
$query = mysql_query('SELECT friends, requests FROM login_info
WHERE user = \'
' .$userget .'\'');
$friend = mysql_fetch_assoc($query);
$req = explode('_', $friend['requests']);
$friends = explode('_', $friend['friends']);
if((bool)array_search($userget, $friends) && (bool)checkarray($userget, $req))
{
echo '<a href="?p=add&user='.$userget .'"> Add As Friend </a>';	
}
if((bool)array_search($userget, $req))
{
echo 'Friend Request Pending';	
}
if((bool)array_search($userget, $friends))
{
echo $userget .'Is Your Friend' .$user_log .'!';	
}


?>

Any help?  :confused:

If you remove those casts I'm pretty sure it would work. array_search will return false if it doesn't find a match.

 

On second thought, it'd probably be safer to do it like this, just to make sure it found a match:

if(array_search($userget, $friends) !== false && checkarray($userget, $req) !== false)
{
echo '<a href="?p=add&user='.$userget .'"> Add As Friend </a>';	
}
if(array_search($userget, $req) !== false)
{
echo 'Friend Request Pending';	
}
if(array_search($userget, $friends) !== false)
{
echo $userget .'Is Your Friend' .$user_log .'!';	
}

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.