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

Source: Internet
Author: User
Tags php session php example
Create a simple php file named functions. php, which contains the main functions. Create a small number of functions in this file to support the actions in the context of the microblog application. Create input form

After the backend table is set, you can consider processing the PHP for data insertion and update. Now you need some simple functions, which will:


Allow users to log on and add posts.


Forward the posts to those who follow the user.


Allow users to follow other users.


I usually use the Model-View-Controller (MVC) application framework (for example
CodeIgniter), because it provides a set of tools for creating these types of applications. For example, I create two models first (one for the user and the other for the post ).
The users, posts, and following tables interact with each other, and then proceed from the two models.


Since you may already be using different frameworks, I decided not to use the above method here. Instead, I chose a simpler, framework-independent approach. For this article, we take a shortcut to directly add records
Users table to create a series of test users for use by the application. I created three users and set their usernames to jane, tommy, and bill.


Then, create a simple php named functions. PHP
File, which contains the main functions. Create a small number of functions in this file to support the actions in the context of the microblog application.


As shown in listing 5, the first function is a simple function that adds content to the posts table.


 Listing 5. functions used to add content to the posts table







Function add_post ($ userid, $ body ){

$ SQL = "insert into posts (user_id, body, stamp)

Values ($ userid, '". MySQL_real_escape_string ($ body)."', now ())";


$ Result = mysql_query ($ SQL );
}



To test this simple function, you also need to add two other PHP files. One is the index. php file, which currently contains a basic small form-
Add more content to the page. Another PHP file is add. php. the above form will be published to this file. Listing 6 is an excerpt from the index. php file. Note that
The PHP session hard encodes a user ID value into 1, which is the user jane in my database. Now there is no problem in doing so, but it needs to be changed later.


Listing 6. index. php file excerpt








Session_start ();
Include_once ('header. php ');

Include_once ('functions. php ');



After the backend table is set, you can consider processing the PHP for data insertion and update. Now you need some simple functions, which will:


Allow users to log on and add posts.


Forward the posts to those who follow the user.


Allow users to follow other users.


I usually use the Model-View-Controller (MVC) application framework (for example
CodeIgniter), because it provides a set of tools for creating these types of applications. For example, I create two models first (one for the user and the other for the post ).
The users, posts, and following tables interact with each other, and then proceed from the two models.


Since you may already be using different frameworks, I decided not to use the above method here. Instead, I chose a simpler, framework-independent approach. For this article, we take a shortcut to directly add records
Users table to create a series of test users for use by the application. I created three users and set their usernames to jane, tommy, and bill.


Then, create a simple php named functions. PHP
File, which contains the main functions. Create a small number of functions in this file to support the actions in the context of the microblog application.

Add a series of updates

Now you can open the functions. php file and add ?? Another function. This time, name the function show_posts (). It displays all posts for a specific user ID, as shown in the following list.

Listing 8. show_posts () function


Function show_posts ($ userid ){
$ Posts = array ();

$ SQL = "select body, stamp from posts
Where user_id = '$ userid' order by stamp desc ";
$ Result = mysql_query ($ SQL );

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

}


If a user ID is passed for this function, it returns the post sent by the user in the descending order of the date in a multi-dimensional array. To use this function, you only need to call it on index. php and retrieve all the posts of that user. Since each record only needs to process a small amount of data, this query can be well expanded.

Listing 9 is the code added to the index. php page. the code is placed after the previously added form. By using the show_posts () function and session variable, you can obtain all the posts sent by the login user. If there is no post, an error message is displayed. If there are posts, they will be displayed one by one in a table-or, if you want to be more chic, you can use your own Cascading Style Sheet (CSS ).

Listing 9. display the post on the index. php page

$ Posts = show_posts (


After the backend table is set, you can consider processing the PHP for data insertion and update. Now you need some simple functions, which will:

Allow users to log on and add posts.

Forward the posts to those who follow the user.

Allow users to follow other users.

I usually work in the context of the Model-View-Controller (MVC) application framework (such as CodeIgniter, because it provides a set of tools for creating these types of applications. For example, I create two models (one for the user and the other for the post), which allow me to interact with users, posts, and following tables, then proceed from the two models.

Since you may already be using different frameworks, I decided not to use the above method here. Instead, I chose a simpler, framework-independent approach. For this article, we take a shortcut to add records directly to the users table to create a series of test users for use by the application. I created three users and set their usernames to jane, tommy, and bill.

Create a simple php file named functions. PHP that contains the main functions. Create a small number of functions in this file to support the actions in the context of the microblog application.

 

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.