PHP Example Tutorial (4): Building a micro-blogging service based on PHP

Source: Internet
Author: User
Tags contains modify php file php example requires variable

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
				
$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
___fckpd___1







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
___fckpd___1







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);







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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.