One php + mysql paging code
/************************************
- Class name: pagesupport
- Function: displays data in the mysql database by page.
- *************************************/
- Class pagesupport {
- // Attributes
- Var $ SQL; // SQL query statement of the data to be displayed
- Var $ page_size; // maximum number of lines per page
-
- Var $ start_index; // The sequence number of the first row of the record to be displayed
- Var $ total_records; // The total number of records.
- Var $ current_records; // number of records read on the current page
- Var $ result; // read the result
-
- Var $ total_pages; // total number of pages
- Var $ current_page; // Current page number
- Var $ display_count = 30; // The first and last pages displayed
-
- Var $ arr_page_query; // array, which contains the parameters to be passed for pagination
-
- Var $ first;
- Var $ prev;
- Var $ next;
- Var $ last;
-
- // Method
- /*************************************** ******
- Construct ()
- Input parameters:
- $ Ppage_size: maximum number of lines per page
- **************************************** *******/
- Function PageSupport ($ ppage_size)
- {
- $ This-> page_size = $ ppage_size;
- $ This-> start_index = 0;
- }
/*************************************** ******
- Constructor :__ destruct ()
- Input parameters:
- **************************************** *******/
- Function _ destruct ()
- {
- }
-
- /*************************************** ******
- Get function :__ get ()
- **************************************** *******/
- Function _ get ($ property_name)
- {
- If (isset ($ this-> $ property_name ))
- {
- Return ($ this-> $ property_name );
- }
- Else
- {
- Return (NULL );
- }
- }
-
- /*************************************** ******
- Set function :__ set ()
- **************************************** *******/
- Function _ set ($ property_name, $ value)
- {
- $ This-> $ property_name = $ value;
- }
-
- /*************************************** ******
- Function name: read_data
- Function: reads records from a table based on SQL query statements.
- Returned value: two-dimensional attribute array result [record number] [field name]
- **************************************** *******/
- Function read_data ()
- {
- $ Psql = $ this-> SQL;
-
- // Query data, database links, and other information should be implemented outside class calls
- $ Result = mysql_query ($ psql) or die (mysql_error ());
- $ This-> total_records = mysql_num_rows ($ result );
-
- // Use the LIMIT keyword to obtain the record to be displayed on this page
- If ($ this-> total_records> 0)
- {
- $ This-> start_index = ($ this-> current_page-1) * $ this-> page_size;
- $ Psql = $ psql. "LIMIT". $ this-> start_index. ",". $ this-> page_size;
-
- $ Result = mysql_query ($ psql) or die (mysql_error ());
- $ This-> current_records = mysql_num_rows ($ result );
-
- // Put the query result in the result array
- $ I = 0;
- While ($ row = mysql_fetch_Array ($ result ))
- {
- $ This-> result [$ I] = $ row;
- $ I ++;
- }
- }
-
- // Obtain the total number of pages and current page information
- $ This-> total_pages = ceil ($ this-> total_records/$ this-> page_size );
- $ This-> first = 1;
- $ This-> prev = $ this-> current_page-1;
- $ This-> next = $ this-> current_page + 1;
- $ This-> last = $ this-> total_pages;
- }
-
- /*************************************** ******
- Function name: standard_navigate ()
- Function: displays the home page, next page, last page, and not pages.
- **************************************** *******/
- Function standard_navigate ()
- {
- Echo"
";
- Echo "";
- Echo"";
- }
-
- /*************************************** ******
- Function name: full_navigate ()
- Function: displays the home page, next page, last page, and not pages.
- Generate navigation links such as 1 2 3... 10 11
- **************************************** *******/
- Function full_navigate ()
- {
- Echo"
";
- Echo "";
- Echo"";
- }
- }
- ?>
Paging usage:
- Include_once ("fenye_php.php"); // introduces a class
- //////////////////////////////////////// ///////////////////////////////
- $ Con = mysql_connect ("localhost", "root ","");
- If (! $ Con)
- {
- Die ('could not connect: '. mysql_error ());
- }
Mysql_select_db ("myblog", $ con); // select a database
-
- $ PAGE_SIZE = 10; // you can specify the number of entries displayed on each page.
- //////////////////////////////////////// ///////////////////////////////
- $ PageSupport = new PageSupport ($ PAGE_SIZE); // instantiate the PageSupport object
- $ Current_page = $ _ GET ["current_page"]; // The current page number.
- If (isset ($ current_page )){
- $ PageSupport->__ set ("current_page", $ current_page );
- } Else {
- $ PageSupport->__ set ("current_page", 1 );
- }
-
- $ PageSupport->__ set ("SQL", "select * from article ");
- $ PageSupport-> read_data (); // read data
-
- If ($ pageSupport-> current_records> 0) // if the data is not empty, assemble the data
- {
- For ($ I = 0; $ I <$ pageSupport-> current_records; $ I ++)
- {
- $ Title = $ pageSupport-> result [$ I] ["title"];
- $ Content = $ pageSupport-> result [$ I] ["content"];
-
- $ Part = substr ($ content, 0,400 );
- // Output each data recyclically
- Echo'
'. $ Title .'
'. $ Part .'
- Update delet
-
- ';
- }
- }
- $ PageSupport-> standard_navigate (); // call this function in the class to display the HTML page
- // Close the database
- Mysql_close ($ con );
- ?>
-
|