$users = show_users(); $following = following(追随其他用户
接下来可以将更多东西添加到 functions.php 文件中。这里需要一个 <font face="NSimsun">show_users()</font> 函数,该函数可以返回系统中所有用户的一个列表。后面将使用这个函数填充一个用户列表。
清单 10. <font face="NSimsun">show_users()</font> 函数
<br/>function show_users(){<br/> $users = array();<br/> $sql = "select id, username from users where status="active" order by username";<br/> $result = mysql_query($sql);<br/><br/> while ($data = mysql_fetch_object($result)){<br/> $users[$data->id] = $data->username;<br/> }<br/> return $users;<br/>}<br/> 登录后复制 登录后复制
|
有了 <font face="NSimsun">show_users()</font> 函数之后,接下来可以创建一个 users.php 文件,该文件将运行这个函数,并显示系统中所有用户的一个列表,对于每个用户,在用户名的旁边都有一个 follow 链接。
清单 11. 运行 <font face="NSimsun">show_users()</font> 函数的 users.php 文件
<br/><?php <br/>session_start();<br/>include_once("header.php");<br/>include_once("functions.php");<br/><br/>?><br/><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"<br/> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br/><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><br/><head><br/> <meta http-equiv="content-type" content="text/html; charset=utf-8" /><br/> <title>Microblogging Application - Users</title><br/></head><br/><body><br/><br/><h1>List of Users</h1><br/><?php<br/>$users = show_users();<br/><br/>if (count($users)){<br/>?><br/><table border="1" cellspacing="0" cellpadding="5" width="500"><br/><?php<br/>foreach ($users as $key => $value){<br/> echo "<tr valign="top">\n";<br/> echo "<td>".$key ."</td>\n";<br/> echo "<td>".$value ." <small><a href="#">follow</a></small></td>\n";<br/> echo "</tr>\n";<br/>}<br/>?><br/></table><br/><?php<br/>}else{<br/>?><br/><p><b>There are no users in the system!</b></p><br/><?php<br/>}<br/>?><br/></body><br/></html><br/> 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制
|
为了访问这个用户列表,在 index.php 文件中表单的上方添加一个到 users.php 的链接:
<p><a href="users.php">see list of users</a></p><br/> 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制
|
现在有了一个易于使用的用户名列表,每个用户名旁有一个 follow 链接。
图 2. 用户列表
在进入下一个阶段之前,还需要编写一个小函数,该函数将返回当前用户正在追随的用户。这样一来,用户就可以用这个列表来确定是否追随另一个用户。
回到 functions.php 文件,添加一个名为 <font face="NSimsun">following()</font> 的函数,如清单 12 所示。将当前用户 ID 传递给该函数,就可以得到该用户正在追随的每个用户的 ID。
清单 12. <font face="NSimsun">following()</font> 函数
<br/>function following($userid){<br/> $users = array();<br/><br/> $sql = "select distinct user_id from following<br/> where follower_id = "$userid"";<br/> $result = mysql_query($sql);<br/><br/> while($data = mysql_fetch_object($result)){<br/> array_push($users, $data->user_id);<br/><br/> }<br/><br/> return $users;<br/><br/>}<br/> 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制
|
现在可以在 users.php 上运行这个函数,检查某个用户 ID 是否在该数组中。如果在,则使用 unfollow 链接。如果不在,则使用默认的 follow 链接。清单 13 显示修改后的代码。
清单 13. 修改后的 users.php 文件,它将显示 follow 和 unfollow 链接
接下来的步骤很简单:创建 follow 和 unfollow 链接使用的 action.php 文件。该文件接受两个 <font face="NSimsun">GET</font> 参数:一个用于用户 ID,另一个用于 follow 或 unfollow。如清单 14 所示,这个文件和 add.php 文件一样简短。
清单 14. action.php 文件
可以看到,这里取决于之前选择的链接,采取两种不同的动作 — <font face="NSimsun">follow_user()</font> 或 <font face="NSimsun">unfollow_user()</font> 。然后,设置一条消息,并将用户重定向回 index.php 页面。用户回到 index.php 页面后,不仅可以看到自己的消息,还可以看到他们追随的用户最近添加的消息。或者,如果之前选择 unfollow 链接,那么那个用户的消息将从列表中消失。稍后需要在 index.php 中添加这点代码。现在,将 <font face="NSimsun">follow_user()</font> 和 <font face="NSimsun">unfollow_user()</font> 函数添加到 functions.php 中。
对于这两个函数要小心一点。不能只是因为用户单击了那个链接,就盲目地追随或放弃追随一个用户。首先,需要检查 following 表中是否存在这样的关系。如果存在,那么可以忽略请求(单击 follow 链接时),或者接受请求(单击 unfollow 链接时)。为简单起见,编写两种情况下都可以使用的一个 <font face="NSimsun">check_count()</font> 函数,如下面的清单所示。
清单 15. <font face="NSimsun">check_count()</font> 函数
<br/>function check_count($first, $second){<br/> $sql = "select count(*) from following <br/> where user_id="$second" and follower_id="$first"";<br/> $result = mysql_query($sql);<br/><br/> $row = mysql_fetch_row($result);<br/> return $row[0];<br/><br/>}<br/><br/>function follow_user($me,$them){<br/> $count = check_count($me,$them);<br/><br/> if ($count == 0){<br/> $sql = "insert into following (user_id, follower_id) <br/> values ($them,$me)";<br/><br/> $result = mysql_query($sql);<br/> }<br/>}<br/><br/><br/>function unfollow_user($me,$them){<br/> $count = check_count($me,$them);<br/><br/> if ($count != 0){<br/> $sql = "delete from following <br/> where user_id="$them" and follower_id="$me"<br/> limit 1";<br/><br/> $result = mysql_query($sql);<br/> }<br/>}<br/> 登录后复制 登录后复制
|
接下来的步骤很容易:在主页上显示当前用户正在追随的其他用户的列表。虽然已经有了一个 <font face="NSimsun">show_users()</font> 函数,但那个函数是显示所有 用户。可以通过增加一个非必需的参数,轻松地改变这个函数的用途。这个参数是一个用户 ID,可以用该参数将用户列表限制为该用户 ID 所追随的那些用户。
清单 16 中重新编写的代码所做的事情是检查传入的 <font face="NSimsun">$user_id</font> 参数。如果该用户 ID 大于 0,则使用一个查询获取此 ID 追随的所有用户的 ID。使用 <font face="NSimsun">implode()</font> 函数将获得的数组转换为一个以逗号分隔的列表。然后将这个字符串 — 大致为 <font face="NSimsun">and id in (1,2,3...n)</font> — 插入到已有的 SQL 查询中,从而将用户列表限制为该用户正在追随的那些用户。
清单 16. 重新编写的代码,用于限制通过查询获得的用户列表
<br/>function show_users($user_id=0){<br/><br/> if ($user_id > 0){<br/> $follow = array();<br/> $fsql = "select user_id from following<br/> where follower_id="$user_id"";<br/> $fresult = mysql_query($fsql);<br/><br/> while($f = mysql_fetch_object($fresult)){<br/> array_push($follow, $f->user_id);<br/> }<br/><br/> if (count($follow)){<br/> $id_string = implode(",", $follow);<br/> $extra = " and id in ($id_string)";<br/><br/> }else{<br/> return array();<br/> }<br/><br/> }<br/><br/> $users = array();<br/> $sql = "select id, username from users <br/> where status="active" <br/> $extra order by username";<br/><br/><br/> $result = mysql_query($sql);<br/><br/> while ($data = mysql_fetch_object($result)){<br/> $users[$data->id] = $data->username;<br/> }<br/> return $users;<br/>}<br/> 登录后复制 登录后复制
|
接下来,将清单 17 中的代码添加到主页中,用于显示所有那些被追随的用户。
清单 17. 修改 index.php 以显示被追随的用户
SESSION["userid"]);
if (count($users)){ ?>
foreach ($users as $key => $value){ echo "\n"; echo "".$key ." | \n"; echo "".$value; if (in_array($key,$following)){ echo " unfollow "; }else{ echo " follow "; } echo " | \n"; echo " \n"; } ?>
接下来的步骤很简单:创建 follow 和 unfollow 链接使用的 action.php 文件。该文件接受两个 <font face="NSimsun">GET</font> 参数:一个用于用户 ID,另一个用于 follow 或 unfollow。如清单 14 所示,这个文件和 add.php 文件一样简短。
清单 14. action.php 文件
可以看到,这里取决于之前选择的链接,采取两种不同的动作 — <font face="NSimsun">follow_user()</font> 或 <font face="NSimsun">unfollow_user()</font> 。然后,设置一条消息,并将用户重定向回 index.php 页面。用户回到 index.php 页面后,不仅可以看到自己的消息,还可以看到他们追随的用户最近添加的消息。或者,如果之前选择 unfollow 链接,那么那个用户的消息将从列表中消失。稍后需要在 index.php 中添加这点代码。现在,将 <font face="NSimsun">follow_user()</font> 和 <font face="NSimsun">unfollow_user()</font> 函数添加到 functions.php 中。
对于这两个函数要小心一点。不能只是因为用户单击了那个链接,就盲目地追随或放弃追随一个用户。首先,需要检查 following 表中是否存在这样的关系。如果存在,那么可以忽略请求(单击 follow 链接时),或者接受请求(单击 unfollow 链接时)。为简单起见,编写两种情况下都可以使用的一个 <font face="NSimsun">check_count()</font> 函数,如下面的清单所示。
清单 15. <font face="NSimsun">check_count()</font> 函数
接下来的步骤很容易:在主页上显示当前用户正在追随的其他用户的列表。虽然已经有了一个 <font face="NSimsun">show_users()</font> 函数,但那个函数是显示所有 用户。可以通过增加一个非必需的参数,轻松地改变这个函数的用途。这个参数是一个用户 ID,可以用该参数将用户列表限制为该用户 ID 所追随的那些用户。
清单 16 中重新编写的代码所做的事情是检查传入的 <font face="NSimsun">$user_id</font> 参数。如果该用户 ID 大于 0,则使用一个查询获取此 ID 追随的所有用户的 ID。使用 <font face="NSimsun">implode()</font> 函数将获得的数组转换为一个以逗号分隔的列表。然后将这个字符串 — 大致为 <font face="NSimsun">and id in (1,2,3...n)</font> — 插入到已有的 SQL 查询中,从而将用户列表限制为该用户正在追随的那些用户。
清单 16. 重新编写的代码,用于限制通过查询获得的用户列表
接下来,将清单 17 中的代码添加到主页中,用于显示所有那些被追随的用户。
清单 17. 修改 index.php 以显示被追随的用户
GET["id"]; $do = 追随其他用户
接下来可以将更多东西添加到 functions.php 文件中。这里需要一个 <font face="NSimsun">show_users()</font> 函数,该函数可以返回系统中所有用户的一个列表。后面将使用这个函数填充一个用户列表。
清单 10. <font face="NSimsun">show_users()</font> 函数
<br/>function show_users(){<br/> $users = array();<br/> $sql = "select id, username from users where status="active" order by username";<br/> $result = mysql_query($sql);<br/><br/> while ($data = mysql_fetch_object($result)){<br/> $users[$data->id] = $data->username;<br/> }<br/> return $users;<br/>}<br/> 登录后复制 登录后复制
|
有了 <font face="NSimsun">show_users()</font> 函数之后,接下来可以创建一个 users.php 文件,该文件将运行这个函数,并显示系统中所有用户的一个列表,对于每个用户,在用户名的旁边都有一个 follow 链接。
清单 11. 运行 <font face="NSimsun">show_users()</font> 函数的 users.php 文件
<br/><?php <br/>session_start();<br/>include_once("header.php");<br/>include_once("functions.php");<br/><br/>?><br/><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"<br/> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br/><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><br/><head><br/> <meta http-equiv="content-type" content="text/html; charset=utf-8" /><br/> <title>Microblogging Application - Users</title><br/></head><br/><body><br/><br/><h1>List of Users</h1><br/><?php<br/>$users = show_users();<br/><br/>if (count($users)){<br/>?><br/><table border="1" cellspacing="0" cellpadding="5" width="500"><br/><?php<br/>foreach ($users as $key => $value){<br/> echo "<tr valign="top">\n";<br/> echo "<td>".$key ."</td>\n";<br/> echo "<td>".$value ." <small><a href="#">follow</a></small></td>\n";<br/> echo "</tr>\n";<br/>}<br/>?><br/></table><br/><?php<br/>}else{<br/>?><br/><p><b>There are no users in the system!</b></p><br/><?php<br/>}<br/>?><br/></body><br/></html><br/> 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制
|
为了访问这个用户列表,在 index.php 文件中表单的上方添加一个到 users.php 的链接:
<p><a href="users.php">see list of users</a></p><br/> 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制
|
现在有了一个易于使用的用户名列表,每个用户名旁有一个 follow 链接。
图 2. 用户列表
在进入下一个阶段之前,还需要编写一个小函数,该函数将返回当前用户正在追随的用户。这样一来,用户就可以用这个列表来确定是否追随另一个用户。
回到 functions.php 文件,添加一个名为 <font face="NSimsun">following()</font> 的函数,如清单 12 所示。将当前用户 ID 传递给该函数,就可以得到该用户正在追随的每个用户的 ID。
清单 12. <font face="NSimsun">following()</font> 函数
<br/>function following($userid){<br/> $users = array();<br/><br/> $sql = "select distinct user_id from following<br/> where follower_id = "$userid"";<br/> $result = mysql_query($sql);<br/><br/> while($data = mysql_fetch_object($result)){<br/> array_push($users, $data->user_id);<br/><br/> }<br/><br/> return $users;<br/><br/>}<br/> 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制 登录后复制
|
现在可以在 users.php 上运行这个函数,检查某个用户 ID 是否在该数组中。如果在,则使用 unfollow 链接。如果不在,则使用默认的 follow 链接。清单 13 显示修改后的代码。
清单 13. 修改后的 users.php 文件,它将显示 follow 和 unfollow 链接
<br><?php <br/>$users = show_users();<br>$following = following(<p><span class="atitle">追随其他用户</span></p>
<p>接下来可以将更多东西添加到 functions.php 文件中。这里需要一个 <code><font face="NSimsun">show_users()</font></code> 函数,该函数可以返回系统中所有用户的一个列表。后面将使用这个函数填充一个用户列表。</p>
<br><strong>清单 10. <code><font face="NSimsun">show_users()</font></code> 函数</strong><br>
登录后复制
<br>function show_users(){<br> $users = array();<br> $sql = "select id, username from users where status="active" order by username";<br> $result = mysql_query($sql);<br><br> while ($data = mysql_fetch_object($result)){<br> $users[$data->id] = $data->username;<br> }<br> return $users;<br>}<br> 登录后复制
|
|
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31
|