How to follow and unfollow users? With following code, list of users appears. containing link to action.php

If not following anyone goto action.php to follow that user from list or if following then show link for unfollow

How to check from table following

condition check:

if ($following)
    {
        <a href='action.php?id=$uid&do=unfollow'>unfollow</a>";
    }
else{
        echo "<a href='action.php?id=$uid&do=follow'>follow</a>";
    }

***

CREATE TABLE IF NOT EXISTS `members` (
`member_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`firstname` varchar(100) DEFAULT NULL,
`lastname` varchar(100) DEFAULT NULL,
`login` varchar(100) NOT NULL DEFAULT '',
`passwd` varchar(32) NOT NULL DEFAULT '',
`friends` text NOT NULL,
PRIMARY KEY (`member_id`)
)

CREATE TABLE `following` (
`user_id` INT NOT NULL ,
`follower_id` INT NOT NULL ,
PRIMARY KEY ( `user_id` , `follower_id` )
)


$user_id=$_SESSION['SESS_MEMBER_ID']; //user logged in

$sql = "select *  from members where member_id != $user_id order by firstname"; 
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result))
{
    $uid=$row["member_id"];
    $fullname= $row["firstname"]."&nbsp;".$row["lastname"];

    echo "$fullname <small><a href='action.php?id=$uid&do=follow'>follow</a></small> <br />"; // follow or unfollow 
}

Recommended Answers

All 11 Replies

You'll need to query your "following" table to see if there's a record that matches the current user id and follower id.

jquery can be used later, but how to check ids? and update ids?

it needs mysql and php...

plz help to write query and necessary code

select *, 
    (select count(*) from following where follower_id = $user_id) as following
from members 
where member_id != $user_id 
order by firstname

Something like this might work, although I'm sure it can be improved upon. A left join might be quicker, but I need to know what your data looks like.

@pritaeas OK, thanks
How to check following condition with your query?

if ($following)
{
echo "<a href='action.php?id=$uid&do=unfollow'>unfollow</a>";
}
else{
echo "<a href='action.php?id=$uid&do=follow'>follow</a>";
}

Execute the query. The result column named 'following' will contain 0 (false) if nobody follows (assuming I read your table correctly).

I tried but something went wrong.

How to check logged in user is following or not to user in the list?

(please give query)

Thanks

Also how to get everything inside loop?

while ($row = mysql_fetch_assoc($result))
    {
      //values????  


        if ($following) //How to check logged in user is following or not?
        {
           echo "<a href='action.php?id=$uid&do=unfollow'>unfollow</a>";
         }
        else
        {
           echo "<a href='action.php?id=$uid&do=follow'>follow</a>";
         }
    }

Help please....

Please do NOT bump threads.

What have you tried? Just executing a query in your loop shouldn't be hard. You still haven't shown us how your data looks.

I have written newly following code with function.

function following($userid){
$user_id=$_SESSION['SESS_MEMBER_ID'];
$users = array();

$sql = "select distinct user_id from following  where follower_id = '$userid'";
$result = mysql_query($sql);

while($data = mysql_fetch_object($result)){
    array_push($users, $data->user_id);
}
return $users;
}

$sql = "select * from members where member_id != $user_id order by firstname";// where status='active' 
$result = mysql_query($sql);
while ($data = mysql_fetch_assoc($result))
{
     $mid=$data['member_id'];
     $firstname=$data['firstname'];
     $lastname=$data['lastname'];  

    $following = following($user_id);

    if (in_array($mid,$following)) 
    {
        echo "<a href=profile.php?id=$mid>$firstname $lastname</a>  &nbsp;<a href='action.php?id=$mid&do=unfollow'>unfollow</a>";echo"<br />";
    }
    else{
      echo "<a href=profile.php?id=$mid>$firstname $lastname &nbsp;</a> <a href='action.php?id=$mid&do=follow'>follow</a>";echo"<br />";
    }
}

It's working, but now, how to add profile table data? (other tables are posted in above posts).

With this code I can follow and unfollow user with displaying their names. How to get their profile data from profile table?

You can suggest function same like function following.

Table structure for table profile

CREATE TABLE IF NOT EXISTS `profile` (
`id` bigint(11) NOT NULL AUTO_INCREMENT,
`user_pid` bigint(11) NOT NULL,
`about` varchar(160) NOT NULL,
`bdate` date NOT NULL,
`photo` varchar(250) NOT NULL,
`location` varchar(250) NOT NULL,
`website` varchar(350) NOT NULL,
`facebook` varchar(350) NOT NULL,
`twitter` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
)

Use a JOIN on member_id.

select * from members m, profile p where m.member_id != $user_id and p.id = m.member_id
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.