Hi Guys

I am trying to display friends of a logged in user for my website, the user(person1 is friends with person2 and I would like this to display when either person1 or person2 log in( this should be under the hyperlink) Friends. and also to be able to see pending requests.

So far i have got this code but it gives me error messages. and does not work, any ideas please!!!!

<?php

session_start();

$user = $_SESSION["logged_user"];

// connection to the database

$query = mysql_query("SELECT * FROM users WHERE person2 = '$user' or person1 = $user");
$result=mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($query))

{

if ($user = $row["person1"] && $row["status"] == "1")

{
echo "$row[person2]";
}

else if ($user == $row["person2"] && $row["status"] == "1")
{
echo "$row[person1]";
}

else if ($user == $row["person1"] && $row["status"] == "0")
{
echo "$row[person2] - Awaiting to accept";
}

else if ($user == $row["person2"] && $row["status"] == "0")
{
echo "$row[person2] - Accept or reject";
}

}

mysql_close($conn);
?>

Any better ways that I could do this please i will really appreciate!!!!

Member Avatar for diafol

Your variables and array members are not consistently written:

echo "$row[person2] - Awaiting to accept";

should be

echo "$row['person2'] - Awaiting to accept";

There are other examples of inconsistency in your code.

Add a friends table to the database, with something along the lines of the following:
id: int - Unique ID, auto increment.
user1: int - ID of the user who sent the friend request
user2: int - ID of the user receiving the request
status: bool - Whether or not the request has been accepted

Query this table for records where user1 or user2 are equal to the logged in user ID.

I also have a problem how can I show user timeline or activisties like one of faceook and twitter?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.