Examples of pagination and SqlHelper usage in php, and pagination sqlhelper

Source: Internet
Author: User

Examples of pagination and SqlHelper usage in php, and pagination sqlhelper

This example describes the paging and SqlHelper class usage in php. 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:

<? Php/*** Created by JetBrains PhpStorm. * User: lee * Date: 13-7-26 * Time: * To change this template use File | Settings | File Templates. */class SqlHelper {private $ mysqli; private static $ host = "localhost"; private static $ user = "root"; private static $ pwd = ""; private static $ db = "world"; private $ SQL = false; private $ result = false; function _ construct () {$ this-> 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:

<? Php/*** Created by JetBrains PhpStorm. * User: lee * Date: 13-7-27 * Time: * To change this template use File | Settings | File Templates. */header ("Content-type: text/html; charset = UTF-8;"); require_once ("SqlHelper. php "); class Paging {private $ sqlHelper = false; private $ pageCount = false; // Number of pages private $ counts = false; // total number of records private $ returnArr = false; // pagination function _ construct () {$ this-> sqlHelp Er = 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/$ pageSiz E); $ 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 "<span class = 'paging-list-a paging-list-a-withbg'> {$ nowPage}/{$ this-> pageCount} </span> <a href = '{ $ Href }? NowPage = {$ pre} 'class = 'paging-list-a'> </a> "; for ($ I = $ t; $ I <$ t + $ pagingSize; $ I ++) {if ($ I * $ pageSize> $ this-> pageCount * $ pageSize) {break ;} else {if ($ nowPage = $ I) {echo "<a href = '{$ href }? NowPage = {$ I} 'class = 'paging-list-a paging-list-a-withbg' >{$ I} </a> ";} else {echo "<a href = '{$ href }? NowPage = {$ I} 'class = 'paging-list-a' >{$ I} </a> ";}}} echo "<a href = '{$ href }? NowPage = {$ nex} 'class = 'paging-list-a' >></a> "; 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:

<! DOCTYPE html> 

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

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.