0

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 
}
3
Contributors
11
Replies
39
Views
3 Years
Discussion Span
Last Post by pritaeas
0

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

0

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

0
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.

0

@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>";
}
0

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

Edited by pritaeas

0

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

Edited by vizz

0

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>";
         }
    }
0

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.

0

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`)
)
0

Use a JOIN on member_id.

select * from members m, profile p where m.member_id != $user_id and p.id = m.member_id
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.