$users = Show_users (Add another user? posts
To add other users ' posts to a user's timesheet (timeline), simply reuse some of the code that was written earlier. For example, you now know how to get a list of users that the current user is following. Also know how to get all the posts sent by a user. So you just have to modify the latter function a little so that it can accept a list of users instead of a single user.
Now just move the first function up a bit in the index.php file so you can use it right away, and then use the list of user IDs obtained from the function to get a certain number of posts from their timesheet-there's no need for all posts, just 5 or so. Remember to arrange the posts for those users in reverse chronological order (nearest top).
First, show_posts() add a limit parameter to the function, and its value defaults to 0. If the limit is greater than 0, a limit value is added to the SQL statement used to retrieve the posts. The other thing to do is to $userid put the argument in an array and parse the array into a comma-delimited field, and finally pass the field to the SQL statement. This requires a little extra work, but it can be handsomely rewarded because, as you see, all posts are displayed in reverse order.
listing 18. Update show_posts() to accept an array of users
function show_posts ($userid, $limit =0) { $posts = Array ();
$user _string = Implode (",", $userid); $extra = "and ID in ($user _string)";
if ($limit > 0) { $extra = "Limit $limit"; }else{ $extra = ""; }
ORDER BY stamp desc $extra "; Echo $sql; $result = mysql_query ($sql);
while ($data = Mysql_fetch_object ($result)) { $posts [] = Array ( "Body" => $data->body ); } return $posts;
}
|
Now go back to the index.php file and pass more than one user ID to show_posts() it, as shown in the following list. This is actually very simple, because these users have been collected. Now just use the array_keys() fetch key value to add the session variable to the array. In this way, the passed array contains at least one value (the ID of the logged-on Current user), and the current user ID and the ID of each user that the user follows.
listing 19. Passing a user array to a show_posts() function
Conclusion
In this article, you learned how to build a simple PHP based microblogging service similar to the Twitter and Facebook status update tools. If all goes well, you can get the results you have now and add it to your application and tailor it to your needs. session["userid"]); if (count ($users)) { $myusers = Array_keys ($users); }else{ $myusers = Array (); } $myusers [] =Add another user? posts
To add other users ' posts to a user's timesheet (timeline), simply reuse some of the code that was written earlier. For example, you now know how to get a list of users that the current user is following. Also know how to get all the posts sent by a user. So you just have to modify the latter function a little so that it can accept a list of users instead of a single user.
Now just move the first function up a bit in the index.php file so you can use it right away, and then use the list of user IDs obtained from the function to get a certain number of posts from their timesheet-there's no need for all posts, just 5 or so. Remember to arrange the posts for those users in reverse chronological order (nearest top).
First, show_posts() add a limit parameter to the function, and its value defaults to 0. If the limit is greater than 0, a limit value is added to the SQL statement used to retrieve the posts. The other thing to do is to $userid put the argument in an array and parse the array into a comma-delimited field, and finally pass the field to the SQL statement. This requires a little extra work, but it can be handsomely rewarded because, as you see, all posts are displayed in reverse order.
listing 18. Update show_posts() to accept an array of users
function show_posts ($userid, $limit =0) { $posts = Array ();
$user _string = Implode (",", $userid); $extra = "and ID in ($user _string)";
if ($limit > 0) { $extra = "Limit $limit"; }else{ $extra = ""; }
ORDER BY stamp desc $extra "; Echo $sql; $result = mysql_query ($sql);
while ($data = Mysql_fetch_object ($result)) { $posts [] = Array ( "Body" => $data->body ); } return $posts;
}
|
Now go back to the index.php file and pass more than one user ID to show_posts() it, as shown in the following list. This is actually very simple, because these users have been collected. Now just use the array_keys() fetch key value to add the session variable to the array. In this way, the passed array contains at least one value (the ID of the logged-on Current user), and the current user ID and the ID of each user that the user follows.
listing 19. Passing a user array to a show_posts() function
Conclusion
In this article, you learned how to build a simple PHP based microblogging service similar to the Twitter and Facebook status update tools. If all goes well, you can get the results you have now and add it to your application and tailor it to your needs. session["UserID"];
$posts = Show_posts ($myusers, 5); |