Alex1646 Posted April 4, 2011 Share Posted April 4, 2011 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? Link to comment https://forums.phpfreaks.com/topic/232614-desplaying-something-if-a-value-is-found-in-an-array/ Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 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 .'!'; } Link to comment https://forums.phpfreaks.com/topic/232614-desplaying-something-if-a-value-is-found-in-an-array/#findComment-1196445 Share on other sites More sharing options...
sasa Posted April 4, 2011 Share Posted April 4, 2011 use in_array() function Link to comment https://forums.phpfreaks.com/topic/232614-desplaying-something-if-a-value-is-found-in-an-array/#findComment-1196462 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.