How to Use PHP and MySQL to create personalized website pages

Source: Internet
Author: User
How can I use PHP and MySQL to create personalized website pages? Paging plays an important role in any Web application, where there is enough big data to be divided into pages. It not only looks neat, but also increases the loading time of a webpage. Therefore, paging is an important improvement of the user interface and saves server resources. In this tutorial, I will tell you a simple

How can I use PHP and MySQL to create personalized website pages? Paging plays an important role in any Web application, where there is enough big data to be divided into pages. It not only looks neat, but also increases the loading time of a webpage. Therefore, paging is an important improvement of the user interface and saves server resources. In this tutorial, I will tell you a simple

How to Use PHP and MySQL to create personalized website pages

?

Paging plays an important role in any Web application, where there is enough big data to be divided into pages. It not only looks neat, but also increases the loading time of a webpage. Therefore, paging is an important improvement of the user interface and saves server resources. In this tutorial, I will show you a simple method to create the data in PHP and obtain the data from the MySQL page. So, let's get started.

?

?

Looking for jQuery pagination? This isHere.

?

The following three important variables are often used in the paging tutorial.

?
123 $ Page; // This variable contains the current page number $ limit; // The number of posts/articles to show on each page $ total_posts; // The total number of posts available in the database

Now, let's look at the following code and figure out what it is about. Don't worry. If you have any comments that cannot be used as code, it will always be good.

?
12345678910111213141516171819202122 /** Trying to get the page number if the $ _ GET ['P'] parameter is set. if not, set the $ page variable to 1. */if (isset ($ _ GET ['P']) {??? $ Page = intval ($ _ GET ['P']); ??? If (empty ($ page )){???????????? $ Page = 1 ;????????}}? /** $ Start variable as per the current page. we will be using this in our SQL queries. */$ start = ($ page-1) * $ limit ;????? /*???? * An important fix for the pagination .???? */???? If ($ start = $ total_posts | $ start> $ total_posts ){???????? $ Start = 0 ;???????? $ Page = 1 ;????}

First, check whether the page parameter is set by setting global variables.$ _ GET. This isGETRequest, because we will pass the page number in the url. If not$ Page = 1, Which means we are on the first page. Then,StartThe value of the variable is set because it will be used in our SQL statement andRestrictionsVariable extraction result. Finally, I have applied for a small paging fix. Therefore, if someone tries to reset the page on url, this is the first page on which our last page is larger than manually entering the page number.

Here is my code. We will use the pagination function displayed on our webpage link.

??
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 Function pagination ($ page, $ total_pages, $ limit ){???? /*???? * We are going to use $ stages for creating the gap between pages in the pagination links .???? */???? $ Stages = 1 ;????? /*???? * The previous and next links .???? */???? $ Prev = $ page-1 ;???? $ Next = $ page + 1 ;????? $ Lastpage = ceil ($ total_pages/$ limit );???? $ LastPagem1 = $ lastpage-1 ;????? $ Paginate = ";????? If ($ lastpage> 1 ){???????? $ Paginate. ='

';????????? # Previous ???????? If ($ page> 1 ){???????????? $ Paginate. = 'previous ';????????} Else {???????????? $ Paginate. = 'previous ';????????}????????? # Pages ???????? If ($ lastpage <7 + ($ stages * 2 )){???????????? For ($ counter = 1; $ counter <= $ lastpage; $ counter ++ ){???????????????? If ($ counter = $ page ){???????????????????? $ Paginate. = ''. $ counter .'';????????????????} Else {???????????????????? $ Paginate. = ''. $ counter. '';????????????????}????????????}??????? ?} Elseif ($ lastpage> 5 + ($ stages * 2 )){???????????? If ($ page <1 + ($ stages * 2 )){???????????????? For ($ counter = 1; $ counter <4 + ($ stages * 2); $ counter ++) {???????????????????? If ($ counter = $ page ){???????????????????????? $ Paginate. = ''. $ counter .'';????????????????????} Else {???????????????????????? $ Paginate. = ''. $ counter. '';????????????????????}???????????????? }????????????????? $ Paginate. = '... ';???????????????? $ Paginate. = ''. $ LastPagem1 .'';???????????????? $ Paginate. = ''. $ lastpage .'';????????????} Elseif ($ lastpage-($ stages * 2)> $ page & $ page> ($ stages * 2 )) {???????????????? $ Paginate. = '1 ';???????????????? $ Paginate. = '2 ';???????????????? $ Paginate. = '... ';???????????????????? For ($ counter = $ page-$ stages; $ counter <= $ page + $ stages; $ counter ++) {???????????????????????? If ($ counter = $ page ){???????????????????????????? $ Paginate. = ''. $ counter .'';????????????????????????} Else {???????????????????????????? $ Paginate. = ''. $ counter. '';????????????????????????}???????????? ????????}????????????????? $ Paginate. = '... ';???????????????? $ Paginate. = ''. $ LastPagem1 .'';???????????????? $ Paginate. = ''. $ lastpage .'';????????????} Else {???????????????? $ Paginate. = '1 ';???????????????? $ Paginate. = '2 ';???????????????? $ Paginate. = '... ';???????????????????? For ($ counter = $ lastpage-(2 + ($ stages * 2); $ counter <= $ lastpage; $ counter ++) {???????????????????????? If ($ counter = $ page ){???????????????????????????? $ Paginate. = ''. $ counter .'';????????????????????????} Else {???????????????????????????? $ Paginate. = ''. $ counter. '';????????????????????????}???????????? ????????}????????????}????????}????????? # Next ???????? If ($ page <$ counter-1 ){???????????? $ Paginate. = 'Next ';????????} Else {???????????? $ Paginate. = 'Next ';????????}????????? $ Paginate. ='

';????}????? Echo $ paginate ;}

The PHP function has three required parameters -?$ Page, $ total_items, $ Restriction. The format of the SQL query written below tells you how to display the results on each current page.

?
$ SQL = "SELECT * FROM 'posts' LIMIT {$ start}, {$ limit }";

Here, we use the PHP codeStartAnd$ RestrictionsVariable. For example, if you are on page 3rd and the project displayed on each page is 10, the above query will be shown as.

1 $ SQL = "SELECT * FROM 'posts' LIMIT 20, 10 ″;
?

Now, use the following code to display the paging link for your code on your webpage to any parameters you want to change.

?
1 Pagination ($ page, $ total_posts, $ limit );

This is all the paging systems that need to be implemented on your website. Before leaving the pen, I have pasted the sample CSS in the paging link in the following section.

??
12345678910111213141516171819202122232425262728293031323334353637 /*?? @ Pagination */. paginate {?? Font-family: "arial", sans-serif ;?? Padding: 3px ;?? Margin: 3px ;}?. Paginate {?? Padding: 2px 5px ;?? Margin: 2px ;?? Border: 1px solid transparent ;?? Text-decoration: none ;?? Color: #333 ;}?. Paginate a: hover,. paginate a: active {?? Border: 1px solid # ff0000 ;?? Background: # ff0000 ;?? Color: # fff ;}?. Paginate span. current {?? Margin: 2px ;?? Padding: 2px 5px ;?? Border: 1px solid #000 ;?? Font-weight: bold ;?? Background-color: #000 ;?? Color: # fff ;}?. Paginate span. disabled {?? Padding: 2px 5px ;?? Margin: 2px ;?? Color: # ddd ;}

This tutorial is complete. I hope you like to read this article.

This article is from Li Xin's blog. For more information, see the source. Http://www.ilixin.net/406.html

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.