| <?php /********************************************* Class Name: Pagesupport Features: Paging display of data in MySQL database ***********************************************/ Class pagesupport{ Property var $sql; The SQL query statement to display the data var $page _size; Show maximum number of rows per page var $start _index; The first line ordinal of the record you want to display var $total _records; Total Records var $current _records; Number of records read on this page var $result; read out the results var $total _pages; Total pages var $current _page; Current page var $display _count = 30; The first few pages displayed and the last few pages var $arr _page_query; An array that contains pagination to display the parameters that need to be passed var $first; var $prev; var $next; var $last; Method /********************************************* Constructors: __construct () Input parameters: $ppage _size: Show the maximum number of rows per page ***********************************************/ function Pagesupport ($ppage _size) { $this->page_size= $ppage _size; $this->start_index=0; } /********************************************* Constructors: __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: Read the corresponding record from the table according to the SQL query statement Return value: Property two-dimensional array result[record number [field name] ***********************************************/ function Read_data () { $psql = $this->sql; Query data, database links and other information should be implemented outside the class call $result =mysql_query ($psql) or Die (Mysql_error ()); $this->total_records=mysql_num_rows ($result); Use the Limit keyword to get the records that are 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); Place query results in a result array $i = 0; while ($row =mysql_fetch_array ($result)) { $this->result[$i]= $row; $i + +; } } Get total pages, 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: Display the first page, the next page, the top, the not page ***********************************************/ function Standard_navigate () { echo "<div align=center>"; echo "<form action=". $_server[' php_self '. "method=" get ">"; echo "<font color = red size = ' 4 ' >". $this->current_page. " Page/Total ". $this->total_pages." Page </font> "; echo ""; echo jumps to <input type= "text" Size=ř "name=" Current_page "value=". $this->current_page. "' /> page "; echo "<input type=" Submit "value=" submitted "/>"; Generate navigation links if ($this->current_page > 1) { echo "<a href=". $_server[' php_self '. Current_page= ". $this->first." > Home </a>| "; echo "<a href=". $_server[' php_self '. Current_page= ". $this->prev." > On a page </a>| "; } if ($this->current_page < $this->total_pages) { echo "<a href=". $_server[' php_self '. Current_page= ". $this->next." > next page </a>| "; echo "<a href=". $_server[' php_self '. Current_page= ". $this->last." > Last </A> "; } echo "</form>"; echo "</div>"; } /********************************************* Function name: Full_navigate () Function: Display the first page, the next page, the top, the not page Generate navigation links such as 1 2 3 ... 10 11 ***********************************************/ function Full_navigate () { echo "<div align=center>"; echo "<form action=". $_server[' php_self '. "method=" get ">"; echo "<font color = red size = ' 4 ' >". $this->current_page. " Page/Total ". $this->total_pages." Page </font> "; echo ""; echo jumps to <input type= "text" Size=ř "name=" Current_page "value=". $this->current_page. "' /> page "; echo "<input type=" Submit "value=" submitted "/>"; Generate navigation links such as 1 2 3 ... 10 11 $front _start = 1; if ($this->current_page > $this->display_count) { $front _start = $this->current_page-$this->display_count; } for ($i = $front _start; $i < $this->current_page; $i + +) { echo "<a href=". $_server[' php_self '. Page= ". $i." >[". $i."] </a> "; } echo "[". $this->current_page. "]"; $displayCount = $this->display_count; if ($this->total_pages > $displayCount && ($this->current_page+ $displayCount) < $this->total_ Pages) { $displayCount = $this->current_page+ $displayCount; }else{ $displayCount = $this->total_pages; } for ($i = $this->current_page+1; $i <= $displayCount; $i + +) { echo "<a href=". $_server[' php_self '. Current_page= ". $i." >[". $i."] </a> "; } Generate navigation links if ($this->current_page > 1) { echo "<a href=". $_server[' php_self '. Current_page= ". $this->first." > Home </a>| "; echo "<a href=". $_server[' php_self '. Current_page= ". $this->prev." > On a page </a>| "; } if ($this->current_page < $this->total_pages) { echo "<a href=". $_server[' php_self '. Current_page= ". $this->next." > next page </a>| "; echo "<a href=". $_server[' php_self '. Current_page= ". $this->last." > Last </A> "; } echo "</form>"; echo "</div>"; } } ?> Call: <?php Include_once (".. /config_jj/sys_conf.inc "); Include_once (".. /pagesupportclass.php ");//Paging class Include_once ('.. /smarty_jsnhclass.php '); $smarty = new SMARTY_JSNH (); Include_once (".. /include/smarty_changed_dir.php "); $smarty->assign (' title ', "Smarty News pagination Test"); <?php $pageSupport = new Pagesupport ($PAGE _size); Instantiating a Pagesupport object $current _page=$_get["Current_page"];//Paging current page if (Isset ($current _page)) { $pageSupport->__set ("Current_page", $current _page); } else { $pageSupport->__set ("Current_page", 1); } ?> $pageSupport->__set ("SQL", "SELECT * from News"); $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"]; $id = $pageSupport->result[$i] ["id"]; $news _arr[$i] = Array (' News ' => Array (' ID ' => $id, ' title ' => $title)); } } Close Database Mysql_close ($DB); $pageinfo _arr = Array ( ' Total_records ' => $pageSupport->total_records, ' Current_page ' => $pageSupport->current_page, ' Total_pages ' => $pageSupport->total_pages, ' => $pageSupport->first, ' prev ' => $pageSupport->prev, ' Next ' => $pageSupport->next, ' Last ' => $pageSupport->last ); $smarty->assign (' Results ', $news _arr); $smarty->assign (' Pagesupport ', $pageinfo _arr); $smarty->display (' News/list.tpl '); ?> Template List.tpl {* I am a Smarty comment, I don ' t exist in the compiled output *} {* {$pageSupport .total_records}<br/> {$pageSupport .current_page}<br/> {$pageSupport .total_pages}<br/> {$pageSupport .first}<br/> {$pageSupport .prev}<br/> {$pageSupport .next}<br/> {$pageSupport .last}<br/> *} <meta http-equiv= "Content-type" content= "text/html; CHARSET=GBK "/> <title>{$title}</title> <body> {foreach item=o from= $results} {$o. News.id} {$o. News.title} <br> {Foreachelse} There is no data you want to view! {/foreach} <br/> {if ($pageSupport. total_records > 0)} <form action= "" method= "get" > Total {$pageSupport. Total_records} Records $pageSupport. Current_page} page/Total {$pageSupport. total_pages} page {if ($pageSupport. current_page > 1)} <a href=?current_page={$pageSupport .first}> Home </A> <a href=?current_page={$pageSupport .prev}> prev </A> {/if} {if ($pageSupport. current_page < $pageSupport. Total_pages)} <a href=?current_page={$pageSupport .next}> next page </A> <a href=?current_page={$pageSupport .last}> last page </A> {/if} Skip to <input type= "text" size= "4" name= "Current_page" value= "{$pageSupport. Current_page}"/> Page <input type= "Submit" value= "Go"/> </form> {/if} </body>
|