Examples of pagination and SqlHelper usage in php

Source: Internet
Author: User
This article mainly introduces the paging and SqlHelper usage in php, and analyzes the definitions and usage skills of the query and paging classes in php databases in combination with the instance form, for more information about paging and SqlHelper usage in php, see the following example. We will share this with you for your reference. The details are as follows:

The document directory structure is as follows:

The SqlHelper. php code is as follows:

 Mysqli = new MySQLi (self: $ host, self: $ user, self: $ pwd, self: $ db ); if ($ this-> mysqli-> connect_error) {die ("failed to connect to the database! ". $ This-> mysql-> connect_error);} $ this-> mysqli-> query ("set names utf8");} function execute_dql_all ($ SQL) {// execute the query statement $ arr = array (); $ this-> result = $ this-> mysqli-> query ($ SQL) or die ($ this-> mysql-> connect_error); // transfers data to the $ arr array while ($ row = mysqli_fetch_array ($ this-> result, MYSQL_BOTH )) {$ arr [] = $ row;} $ this-> result-> free (); return $ arr;} function execute_dql_num ($ SQL) {// execute the query statement $ arr = array (); $ this-> r Esult = $ this-> mysqli-> query ($ SQL) or die ($ this-> mysql-> connect_error ); // save the data to the $ arr array while ($ row = mysqli_fetch_array ($ this-> result, MYSQLI_NUM) {$ arr [] = $ row ;} $ this-> result-> free (); return $ arr;} function execute_dql_assoc ($ SQL) {// execute the query statement $ arr = array (); $ this-> result = $ this-> mysqli-> query ($ SQL) or die ($ this-> mysql-> connect_error ); // convert the data to the $ arr array while ($ row = mysqli_fetch_array ($ this-> result, MYSQLI _ ASSOC) {$ arr [] = $ row;} $ this-> result-> free (); return $ arr ;} // query the number of records in a table. function execute_dql_counts ($ table, $ id = "*") {$ this-> SQL = "select count ($ id) from $ table "; $ this-> result = $ this-> mysqli-> query ($ this-> SQL); $ row = mysqli_fetch_all ($ this-> result ); $ this-> result-> free (); return $ row [0] [0];} function execute_dml ($ SQL) {// execute deleting and modifying $ this-> result = $ this-> mysqli-> query ($ SQL); if (! $ This-> result) {return-1; // An error occurred while performing positive deletion.} else {if ($ this-> mysqli-> affected_rows> 0) {return 1; // The execution is successful, affecting the number of rows} else {return 0; // The execution is successful, but the number of rows is not affected }}}}

The Paging. php code is as follows:

 SqlHelper = new SqlHelper (); $ this-> returnArr = array ();} /** parameter description ** $ pagination of data in the table when the table is paged * $ id: used to query the total number of records of the data table on the current page * $ pageSize: how many information records are displayed on each page number * $ pagingSize the number of pages displayed in each loop * $ What page is nowPage currently, by default, the first page * $ href is displayed. where is the hyperlink connecting to? */function paging_prev_next ($ table, $ id = "*", $ pageSize, $ pagingSize, $ nowPage = 1, $ href) {$ this-> counts = $ this-> sqlHelper-> execute_dql_counts ($ table, $ id ); $ this-> pageCount = ceil ($ this-> counts/$ pageSize); $ this-> returnArr ["count"] = $ this-> counts; $ this-> returnArr ["start"] = ($ nowPage-1) * $ pageSize; $ this-> returnArr ["limit"] = $ pageSize; if ($ nowPage> $ this-> pageCount | $ nowPage <= 0) {return false;} $ t = (ceil ($ nowPage/$ pagingSize)-1) * $ pagingSize + 1; $ pre = $ nowPage-$ pagingSize; $ nex = $ nowPage + $ pagingSize; echo "{$ nowPage}/{$ this-> pageCount} <"; for ($ I = $ t; $ I <$ t + $ pagingSize; $ I ++) {if ($ I * $ pageSize> $ this-> pageCount * $ pageSize) {break;} else {if ($ nowPage = $ I) {echo "{$ I}" ;}else {echo "{$ I}" ;}}} echo ">"; return $ this-> returnArr ;}}

The paging-list-link.css code is as follows:

/*** Created by JetBrains PhpStorm. * User: lee * Date: 13-7-27 * Time: * To change this template use File | Settings | File Templates. */. paging-list-a {border: 1px solid # b5b5af; background-color: # efebed; font-family: 'meiryo Ui'; font-size: 16px; font-weight: 600; padding: 0px 8px 0px 8px;/* cursor: pointer; */text-decoration: none; color: #292927 ;}. paging-list-a-withBg {background-color: #1D92E2; color: white ;}

The usePaging. php code is as follows:

    
 
 Paging_prev_next ("city", "ID", $ meiyexiansi, $ meiyelianjieshu, $ nowPage, "usePaging. php"); // if (! $ ReceiveArr) {return;} // query the data to be displayed on each page. the size limit exists in the $ receiveArr array $ sqlHelper = new SqlHelper (); $ result = $ sqlHelper-> execute_dql_num ("select * from city limit ". $ receiveArr ['start']. ",". $ receiveArr ['limit']. ""); echo"
";print_r($result);echo "
";?>

The database used is the world database that comes with MySQL5.6.

The following figure shows the running result:

However, there is a Bug in the code. It is not displayed when pages are turned to the end, because the 41 ~ of the Paging. php file ~ About 43 rows.

The error code is as follows:

if($nowPage>$this->pageCount || $nowPage<=0){  return false;}

I hope this article will help you with PHP programming.

For more articles about pagination and SqlHelper usage examples in php, refer to the Chinese PHP website!

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.