Jump to content

Friend system


doddsey_65

Recommended Posts

For my friend system usernames are stored in a table (f_username) along with the username of the friend (f_friend_username).

 

Im trying to display the friends of a user on their profile but the users username can be in either the f_username column or the f_friend_username column.

 

So i have this query but it returns the same result twice. Is there a better way around this? I thought about a conditional ON statement but don't know if it's possible.

 

$sql = "SELECT f.*, u.u_avatar
                FROM ".Asf_Db::$prefix."friends f
                JOIN ".Asf_Db::$prefix."users u
                ON (
                    f.f_username = u.u_username
                    OR
                    f.f_friend_username = u.u_username
                )
                WHERE (
                    f_username = ?
                    OR 
                    f_friend_username = ?
                )
                AND f_confirmed = ?";
                
        $sth = Asf_Core::$db->prepare($sql);
        $sth->execute(array(
            $username,
            $username,
            1
            )
        ) or die(Asf_Core::$db->error($sth, $sql));

 

Link to comment
Share on other sites

Just stick the WHERE conditions into the JOIN's:

SELECT f.*, u.u_avatar
FROM friends f
JOIN users u ON (
f.f_username = u.u_username AND f.f_friend_username = $username
OR f.f_friend_username = u.u_username AND f.f_username = $username
)
AND f.f_confirmed = 1

You might want a DISTINCT too if your friendslisting thing doesn't already check for an existing pair when adding friends.

Link to comment
Share on other sites

Yes, but use parenthesis or your ORs and ANDs won't work.

:-\ But ANDs are higher than ORs, so the way I have it should be fine.

I sincerely hope you don't rely on the arbitrary order-of-operations that way -- you should be able to add an arbitrary new expression without breaking everything.

 

Add the 4 characters, you'll thank me later.  Order-of-operations shouldn't exist.

Link to comment
Share on other sites

Fenway has a point, it can be a headache to alter your existing query and spend hours looking for why the results aren't showing up as expected only to realize its because of that pesky new AND / OR clause messing up your logic. I speak from personal less-than-a-week-ago experience.

 

It also looks nicer. Nicer looking code = Super A+ bro guy;

Link to comment
Share on other sites

Fair enough, but I always write sets of conditions in the same way that makes it easy for me to tell where my ANDs and ORs are. Believe me, if I thought there would be ambiguity then I would be more explicit.

(cond OR cond) AND
cond AND
(cond OR cond)

cond AND cond AND cond
OR cond AND cond AND cond
OR cond AND cond AND cond

But to each their own.

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.