清单 13. 修改后的 users.php 文件,它将显示 follow 和 unfollow 链接
<?php
$users = show_users();
$following = following($_SESSION['userid']);
if (count($users)){
?>
<table border='1' cellspacing='0' cellpadding='5' width='500'>
<?php
foreach ($users as $key => $value){
echo "<tr valign='top'>\n";
echo "<td>".$key ."</td>\n";
echo "<td>".$value;
if (in_array($key,$following)){
echo " <small>
<a href='action.php?id=$key&do=unfollow'>unfollow</a>
</small>";
}else{
echo " <small>
<a href='action.php?id=$key&do=follow'>follow</a>
</small>";
}
echo "</td>\n";
echo "</tr>\n";
}
?>
|
接下来的步骤很简单:创建 follow 和 unfollow 链接使用的 action.php 文件。该文件接受两个 GET 参数:一个用于用户 ID,另一个用于 follow 或 unfollow。如清单 14 所示,这个文件和 add.php 文件一样简短。
<?php
session_start();
include_once("header.php");
include_once("functions.php");
$id = $_GET['id'];
$do = $_GET['do'];
switch ($do){
case "follow":
follow_user($_SESSION['userid'],$id);
$msg = "You have followed a user!";
break;
case "unfollow":
unfollow_user($_SESSION['userid'],$id);
$msg = "You have unfollowed a user!";
break;
}
$_SESSION['message'] = $msg;
header("Location:index.php");
?>
|
可以看到,这里取决于之前选择的链接,采取两种不同的动作 — follow_user() 或 unfollow_user()。然后,设置一条消息,并将用户重定向回 index.php 页面。用户回到 index.php 页面后,不仅可以看到自己的消息,还可以看到他们追随的用户最近添加的消息。或者,如果之前选择 unfollow 链接,那么那个用户的消息将从列表中消失。稍后需要在 index.php 中添加这点代码。现在,将 follow_user() 和 unfollow_user() 函数添加到 functions.php 中。
对于这两个函数要小心一点。不能只是因为用户单击了那个链接,就盲目地追随或放弃追随一个用户。首先,需要检查 following 表中是否存在这样的关系。如果存在,那么可以忽略请求(单击 follow 链接时),或者接受请求(单击 unfollow 链接时)。为简单起见,编写两种情况下都可以使用的一个 check_count() 函数,如下面的清单所示。
function check_count($first, $second){
$sql = "select count(*) from following
where user_id='$second' and follower_id='$first'";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
return $row[0];
}
function follow_user($me,$them){
$count = check_count($me,$them);
if ($count == 0){
$sql = "insert into following (user_id, follower_id)
values ($them,$me)";
$result = mysql_query($sql);
}
}
function unfollow_user($me,$them){
$count = check_count($me,$them);
if ($count != 0){
$sql = "delete from following
where user_id='$them' and follower_id='$me'
limit 1";
$result = mysql_query($sql);
}
}
|
接下来的步骤很容易:在主页上显示当前用户正在追随的其他用户的列表。虽然已经有了一个 show_users() 函数,但那个函数是显示所有 用户。可以通过增加一个非必需的参数,轻松地改变这个函数的用途。这个参数是一个用户 ID,可以用该参数将用户列表限制为该用户 ID 所追随的那些用户。
清单 16 中重新编写的代码所做的事情是检查传入的 $user_id 参数。如果该用户 ID 大于 0,则使用一个查询获取此 ID 追随的所有用户的 ID。使用 implode() 函数将获得的数组转换为一个以逗号分隔的列表。然后将这个字符串 — 大致为 and id in (1,2,3...n) — 插入到已有的 SQL 查询中,从而将用户列表限制为该用户正在追随的那些用户。
清单 16. 重新编写的代码,用于限制通过查询获得的用户列表
function show_users($user_id=0){
if ($user_id > 0){
$follow = array();
$fsql = "select user_id from following
where follower_id='$user_id'";
$fresult = mysql_query($fsql);
while($f = mysql_fetch_object($fresult)){
array_push($follow, $f->user_id);
}
if (count($follow)){
$id_string = implode(',', $follow);
$extra = " and id in ($id_string)";
}else{
return array();
}
}
$users = array();
$sql = "select id, username from users
where status='active'
$extra order by username";
$result = mysql_query($sql);
while ($data = mysql_fetch_object($result)){
$users[$data->id] = $data->username;
}
return $users;
}
|
接下来,将清单 17 中的代码添加到主页中,用于显示所有那些被追随的用户。
<h2>Users you're following</h2>
<?php
$users = show_users($_SESSION['userid']);
if (count($users)){
?>
<ul>
<?php
foreach ($users as $key => $value){
echo "<li>".$value."</li>\n";
}
?>
</ul>
<?php
}else{
?>
<p><b>You're not following anyone yet!</b></p>
<?php
}
?>
|