PHP example tutorial (4): Build a PHP-based microblog service

Source: Internet
Author: User
Tags php example
To add the posts of other users to a user's timeline, you only need to re-use the code written earlier. For example, you now know how to obtain a list of users that the current user is following. You also know how to obtain all the posts sent by a user and add other users.

To add the posts of other users to a user's timeline, you only need to re-use the code written earlier. For example, you now know how to obtain a list of users that the current user is following. You also know how to obtain all posts sent by a user. Therefore, you only need to modify the last function to accept a user list, instead of a single user.

Now you only need. move the first function up in the PHP file to use it immediately, and then use the user ID list obtained through this function, get a certain number of posts from their schedules-there is no need for all the posts here, only about five. Remember, sort the user's posts in descending order of the date (the most recent one.

First, add a limit parameter to the show_posts () function and set its value to 0 by default. If the limit value is greater than 0, a limit value is added to the SQL statement used to retrieve the subscriber. In addition, you need to add the $ userid parameter to an array, resolve the array to a field separated by commas, and finally pass the field to the SQL statement. This requires a little extra work, but you can get a great reward, because as you can see, all posts will be displayed in reverse order.

Listing 18. update show_posts () to accept a group 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 = '';
}

$ SQL = "select user_id, body, stamp from posts
Where user_id in ($ user_string)
Order by stamp desc $ extra ";
Echo $ SQL;
$ Result = mysql_query ($ SQL );

While ($ data = mysql_fetch_object ($ result )){
$ Posts [] = array ('stamp' => $ data-> stamp,
'Userid' => $ data-> user_id,
'Body' => $ data-> body
);
}
Return $ posts;

}


Return to the index. php file and pass more than one user ID to show_posts (), as shown in the following list. This is actually very simple, because these users have been collected. Now you only need to use array_keys () to retrieve the key value and add the session variable to the array. In this way, the passed array contains at least one value (the ID of the currently logged-on user), and at most the ID of the current user and the ID of each user that the user follows.

Listing 19. Passing a user group to the show_posts () function


$ Users = show_users (add another user? Post


To add the posts of other users to a user's timeline, you only need to re-use the code written earlier. For example, you now know how to obtain a list of users that the current user is following. You also know how to obtain all posts sent by a user. Therefore, you only need to modify the last function to accept a user list, instead of a single user.

Now you only need. move the first function up in the php file to use it immediately, and then use the user ID list obtained through this function, get a certain number of posts from their schedules-there is no need for all the posts here, only about five. Remember, sort the user's posts in descending order of the date (the most recent one.

First, add a limit parameter to the show_posts () function and set its value to 0 by default. If the limit value is greater than 0, a limit value is added to the SQL statement used to retrieve the subscriber. In addition, you need to add the $ userid parameter to an array, resolve the array to a field separated by commas, and finally pass the field to the SQL statement. This requires a little extra work, but you can get a great reward, because as you can see, all posts will be displayed in reverse order.

Conclusion

In this article, you learned how to build a simple PHP-based microblog service similar to Twitter and Facebook status update tools. If everything goes well, you can get the result now, add it to your application, and customize it as needed.


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.