Jump to content

Need Help With Query Logic


Mohammedmehdi

Recommended Posts

Hello :D

 

2 tables

- users (userid, username)

- friends (friendshipid, user1, user2, accepted)

 

I have a friends search box (works like google's/facebook's auto suggest). 

This search box is meant to be used to pick a friend to send a private message to.

So far, all my code can do is autosuggest people on the social network (basically display all people on the users table based on the characters typed).

 



<?php
 mysql_connect("localhost","username","pass");
 mysql_select_db("dbname");


 $term=$_GET["term"];


 $query=mysql_query("SELECT userid, username FROM users where username like '%".$term."%'");
 $json=array();


    while($friend=mysql_fetch_array($query)){
         $json[]=array(
                    'value'=> $friend["username"]
                        );
    }


 echo json_encode($json);


?>


 

 

I wanted it to filter out those who are not friends.

 

BTW,  just to clarify, the logged in user who wants to send a private message, his userid could be on either user1/user2 of the friends table.

 

Any idea behind the logic that could sort this issue?

Will really appreciate any help :)

Link to comment
Share on other sites

try

$sql = "SELECT u.userid, u.username
    FROM users u
    JOIN friends f ON u.userid = f.user1
    WHERE f.user2 = $loggedinuser AND u.username LIKE '%$term%'
    UNION
    SELECT u.userid, u.username
    FROM users u
    JOIN friends f ON u.userid = f.user2
    WHERE f.user1 = $loggedinuser AND u.username LIKE '%$term%'
    ORDER BY username";
Link to comment
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.