作者:卡瑙娇管 | 来源:互联网 | 2013-06-23 23:13
接下来可以将更多东西添加到functions.PHP文件中。这里需要一个show_users()函数,该函数可以返回系统中所有用户的一个列表。后面将使用这个函数填充一个用户列表。
追随其他用户
接下来可以将更多东西添加到 functions.PHP 文件中。这里需要一个 show_users()
函数,该函数可以返回系统中所有用户的一个列表。后面将使用这个函数填充一个用户列表。
清单 10. show_users() 函数
function show_users(){
$users = array();
$sql = "select id, username from users where status='active' order by username";
$result = MySQL_query($sql);
while ($data = mysql_fetch_object($result)){
$users[$data->id] = $data->username;
}
return $users;
}
|
有了 show_users() 函数之后,接下来可以创建一个 users.php
文件,该文件将运行这个函数,并显示系统中所有用户的一个列表,对于每个用户,在用户名的旁边都有一个 follow 链接。
清单 11. 运行 show_users() 函数的 users.php 文件
session_start();
include_once("header.php");
include_once("functions.php");
?>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
List of Users
$users = show_users();
if (count($users)){
?>
foreach ($users as $key => $value){
echo "\n";
echo "".$key ." | \n";
echo "".$value ." follow | \n";
echo " \n";
}
?>
}else{
?>
There are no users in the system!
}
?>
|
为了访问这个用户列表,在 index.php 文件中表单的上方添加一个到 users.php 的链接:
现在有了一个易于使用的用户名列表,每个用户名旁有一个 follow 链接。
图 2. 用户列表
在进入下一个阶段之前,还需要编写一个小函数,该函数将返回当前用户正在追随的用户。这样一来,用户就可以用这个列表来确定是否追随另一个用户。
回到 functions.php 文件,添加一个名为 following() 的函数,如清单 12 所示。将当前用户 ID
传递给该函数,就可以得到该用户正在追随的每个用户的 ID。
清单 12. following() 函数
function following($userid){
$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;
}
|
现在可以在 users.php 上运行这个函数,检查某个用户 ID 是否在该数组中。如果在,则使用 unfollow 链接。如果不在,则使用默认的
follow 链接。清单 13 显示修改后的代码。
清单 13. 修改后的 users.php 文件,它将显示 follow 和 unfollow 链接
$users = show_users();
$following = following(
追随其他用户
|
接下来可以将更多东西添加到 functions.php 文件中。这里需要一个 show_users()
函数,该函数可以返回系统中所有用户的一个列表。后面将使用这个函数填充一个用户列表。
清单
14. action.php 文件
session_start();
include_once("header.php");
include_once("functions.php");
$id =
追随其他用户
|
清单 15. 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 中的代码添加到主页中,用于显示所有那些被追随的用户。
清单 17. 修改 index.php 以显示被追随的用户
Users you're following
$users = show_users(
追随其他用户
|