追随其他用户
接下来可以将更多东西添加到 functions.php 文件中。这里需要一个 <font face="NSimsun">show_users()</font> 函数,该函数可以返回系统中所有用户的一个列表。后面将使用这个函数填充一个用户列表。
清单 10.
<font face="NSimsun">show_users()</font> 函数| @@######@@ |
有了 <font face="NSimsun">show_users()</font> 函数之后,接下来可以创建一个 users.php 文件,该文件将运行这个函数,并显示系统中所有用户的一个列表,对于每个用户,在用户名的旁边都有一个 follow 链接。
清单 11. 运行
<font face="NSimsun">show_users()</font> 函数的 users.php 文件| @@######@@ |
为了访问这个用户列表,在 index.php 文件中表单的上方添加一个到 users.php 的链接:
| @@######@@ |
现在有了一个易于使用的用户名列表,每个用户名旁有一个 follow 链接。
立即学习“PHP免费学习笔记(深入)”;
图 2. 用户列表
在进入下一个阶段之前,还需要编写一个小函数,该函数将返回当前用户正在追随的用户。这样一来,用户就可以用这个列表来确定是否追随另一个用户。
回到 functions.php 文件,添加一个名为 <font face="NSimsun">following()</font> 的函数,如清单 12 所示。将当前用户 ID 传递给该函数,就可以得到该用户正在追随的每个用户的 ID。
清单 12.
<font face="NSimsun">following()</font> 函数| @@######@@ |
现在可以在 users.php 上运行这个函数,检查某个用户 ID 是否在该数组中。如果在,则使用 unfollow 链接。如果不在,则使用默认的 follow 链接。清单 13 显示修改后的代码。
清单 13. 修改后的 users.php 文件,它将显示 follow 和 unfollow 链接
@@######@@
有了 清单 11. 运行 <font face="NSimsun">show_users()</font> 函数的 users.php 文件
为了访问这个用户列表,在 index.php 文件中表单的上方添加一个到 users.php 的链接:
现在有了一个易于使用的用户名列表,每个用户名旁有一个 follow 链接。 立即学习“PHP免费学习笔记(深入)”; 图 2. 用户列表 在进入下一个阶段之前,还需要编写一个小函数,该函数将返回当前用户正在追随的用户。这样一来,用户就可以用这个列表来确定是否追随另一个用户。 回到 functions.php 文件,添加一个名为 清单 12. <font face="NSimsun">following()</font> 函数
现在可以在 users.php 上运行这个函数,检查某个用户 ID 是否在该数组中。如果在,则使用 unfollow 链接。如果不在,则使用默认的 follow 链接。清单 13 显示修改后的代码。 清单 13. 修改后的 users.php 文件,它将显示 follow 和 unfollow 链接
接下来的步骤很简单:创建 follow 和 unfollow 链接使用的 action.php 文件。该文件接受两个 清单 14. action.php 文件
<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> <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><p><a href="users.php">see list of users</a></p><br> <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>show_users() <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> <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><p><a href="users.php">see list of users</a></p><br> <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>___FCKpd___4 show_users() <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> <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><p><a href="users.php">see list of users</a></p><br> <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>show_users() <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> <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><p><a href="users.php">see list of users</a></p><br> <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>___FCKpd___4 ___FCKpd___5 <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> <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>show_users() <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> <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><p><a href="users.php">see list of users</a></p><br> <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>show_users() <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> <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><p><a href="users.php">see list of users</a></p><br> <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>___FCKpd___4 show_users() <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> <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><p><a href="users.php">see list of users</a></p><br> <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>show_users() <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> <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><p><a href="users.php">see list of users</a></p><br> <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>___FCKpd___4 ___FCKpd___5 <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> <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>___FCKpd___8 ___FCKpd___5 ___FCKpd___6 ___FCKpd___7 ___FCKpd___8 <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> <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><p><a href="users.php">see list of users</a></p><br> <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>show_users() <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> |











