PHP Ajax page-type code _php tips

Source: Internet
Author: User
Tags error handling



<?php


This page class does not handle SQL;


Dramatically speeds up paging functionality


Http://blog.csdn.net/fkedwgwy


Xiaoxiang Blog--Xiaoxiang


/**


Demonstrate


Require_once ('.. /libs/classes/page.class.php ');


$page =new page (' Total ' =>1000, ' perpage ' =>20));


Echo ' mode:1<br> '. $page->show ();


Echo ' <hr>mode:2<br> ' $page->show (2);


Echo ' <hr>mode:3<br> ' $page->show (3);


Echo ' <hr>mode:4<br> ' $page->show (4);


Echo ' <hr> start Ajax mode: ';


$ajaxpage =new page (' Total ' =>1000, ' perpage ' =>20, ' Ajax ' => ' ajax_page ', ' page_name ' => ' Test '));


Echo ' mode:1<br> '. $ajaxpage->show ();


*/


Class Zend_page


{


/**


* Config, public


*/


The Var $page _name= "page";//page label, used to control the URL page. such as xxx.php? The Pb_page in the pb_page=2


var $next _page= ' > ';//Next page


var $pre _page= ' < ';//Previous Page


var $first _page= ' first ';//Home


var $last _page= ' last ';//End


var $pre _bar= ' << ';//previous page bar


var $next _bar= ' >> ';//Next page Bar


var $format _left= ';


var $format _right= ';


var $is _ajax=false;//Whether Ajax paging mode is supported


var $next _ten_page= ">>>";


var $per _ten_page= "<<<";





/**


* Private


*


*/


The Var $pagebarnum =10;//control the number of record bars.


=0;//var $totalpage total pages


var $ajax _action_name= ';//ajax action name


var $nowindex =1;//Current Page


var $url = "";//url address Header


var $offset = 0;


var $total = ';





/**


* Constructor Constructor


*


* @param array $array [' total '], $array [' Perpage '], $array [' Nowindex '], $array [' url '], $array [' Ajax '] ...


*/


function Zend_page ($array)


{


if (Is_array ($array)) {


if (!array_key_exists (' Total ', $array)) $this->error (__function__, ' need a param to total ');


$total =intval ($array [' total ']);


$perpage = (array_key_exists (' Perpage ', $array))? intval ($array [' Perpage ']): 10;


$nowindex = (array_key_exists (' Nowindex ', $array))? intval ($array [' Nowindex ']): ';


$url = (array_key_exists (' url ', $array))? $array [' url ']: ';


}else{


$total = $array;


$perpage = 10;


$nowindex = ';


$url = ';


}


if ((!is_int ($total)) | | ($total <0)) $this->error (__function__, $total. ' is not a positive integer! ');


if ((!is_int ($perpage)) | | ($perpage <=0)) $this->error (__function__, $perpage. ' is not a positive integer! ');


if (!empty ($array [' page_name '])) $this->set (' page_name ', $array [' page_name ']);/set pagename


$this->_set_nowindex ($nowindex);//Set the current page


$this->_set_url ($url);//Set link address


$this->totalpage=ceil ($total/$perpage);


$this->total= $total;


$this->offset= ($this->nowindex-1) * $perpage;


if (!empty ($array [' Ajax '])) $this->open_ajax ($array [' Ajax ']);//Open Ajax mode


}


/**


* Set the value of the variable name specified in the class, if the amount of change does not belong to this class, will throw a exception


*


* @param string $var


* @param string $value


*/


function set ($var, $value)


{


if (In_array ($var, Get_object_vars ($this))


$this-> $var = $value;


else {


$this->error (__function__, $var. "Does not belong to pb_page!");


}





}


/**


* Turn on inverted Ajax mode


*


* @param string $action The default Ajax-triggered action.


*/


function Open_ajax ($action)


{


$this->is_ajax=true;


$this->ajax_action_name= $action;


}


/**


* Get the code that displays the next page


*


* @param string $style


* @return String


*/


function next_page ($style = ')


{


if ($this->nowindex< $this->totalpage) {


return $this->_get_link ($this->_get_url ($this->nowindex+1), $this->next_page, $style);


}


Return ' <span class= '. $style. ' " > '. $this->next_page. ' </span> ';


}





/**


* Get the code that shows "previous page"


*


* @param string $style


* @return String


*/


function pre_page ($style = ')


{


if ($this->nowindex>1) {


return $this->_get_link ($this->_get_url ($this->nowindex-1), $this->pre_page, $style);


}


Return ' <span class= '. $style. ' " > '. $this->pre_page. ' </span> ';


}





/**


* Get the code that shows "up to 10 pages"


*


* @param string $style


* @return String


*/


function pre_ten_page ($style = ')


{


if (Intval ($this->nowindex/10) +1>1) {


return $this->_get_link ($this->_get_url ($this->nowindex/10) *10-5), $this->pre_ten_page, $style );


}


Return ' <span class= '. $style. ' " > '. $this->pre_ten_page. ' </span> ';


}





/**


* Get the code that shows "next 10 pages"


*


* @param string $style


* @return String


*/


function next_ten_page ($style = ')


{


if (Intval ($this->nowindex/10) <intval ($this->totalpage/10)) {


return $this->_get_link ($this->_get_url (intval ($this->nowindex/10) +1) *10+5), $this->next_ten_page,$ Style);


}


Return ' <span class= '. $style. ' " > '. $this->next_ten_page. ' </span> ';


}





/**


* Get the code to display "Home page"


*


* @return String


*/


function first_page ($style = ')


{


if ($this->nowindex==1) {


Return ' <span class= '. $style. ' " > '. $this->first_page. ' </span> ';


}


return $this->_get_link ($this->_get_url (1), $this->first_page, $style);


}





/**


* Get the code that shows "last"


*


* @return String


*/


function last_page ($style = ')


{


if ($this->nowindex== $this->totalpage) {


Return ' <span class= '. $style. ' " > '. $this->last_page. ' </span> ';


}


return $this->_get_link ($this->_get_url ($this->totalpage), $this->last_page, $style);


}





function Nowbar ($style = ', $nowindex _style= ')


{


$plus =ceil ($this->PAGEBARNUM/2);


if ($this->pagebarnum-$plus + $this->nowindex> $this->totalpage) $plus = ($this->pagebarnum-$this-> totalpage+ $this->nowindex);


$begin = $this->nowindex-$plus +1;


$begin = ($begin >=1) $begin: 1;


$return = ';


for ($i = $begin; $i < $begin + $this->pagebarnum; $i + +)


{


if ($i <= $this->totalpage) {


if ($i!= $this->nowindex)


$return. = $this->_get_text ($this->_get_link ($this->_get_url ($i), $i, $style));


Else


$return. = $this->_get_text (' <span class= '. $nowindex _style. ') > '. $i. ' </span> ');


}else{


Break


}


$return. = "\ n";


}


Unset ($begin);


return $return;


}


/**


* Get the code to display the Jump button


*


* @return String


*/


function Select ()


{


$return = ' <select name= ' Pb_page_select "onchange=" Location.replace (this.value) ">";


For ($i =1 $i <= $this->totalpage; $i + +)


{


if ($i = = $this->nowindex) {


$return. = ' <option value= '. $this->_get_url ($i). ' selected> '. $i. ' </option> ';


}else{


$return. = ' <option value= '. $this->_get_url ($i). ' > '. $i. ' </option> ';


}


}


Unset ($i);


$return. = ' </select> ';


return $return;


}





/**


* Get the value required by limit in the MySQL statement


*


* @return String


*/


function offset ()


{


return $this->offset;


}





/**


* Control pagination display style (you can add the corresponding style)


*


* @param int $mode


* @return String


*/


Function Show ($mode =1, $url = ")


{


Switch ($mode)


{


Case ' 1 ':


$this->next_page= ' next page ';


$this->pre_page= ' up a page ';


return $this->pre_page (). $this->nowbar (). $this->next_page (). ' $this->select (). ' Page ';


Break


Case ' 2 ':


$this->next_page= ' next page ';


$this->pre_page= ' up a page ';


$this->first_page= ' home ';


$this->last_page= ' last ';


return $this->first_page (). $this->pre_page (). ' [->nowindex. $this] '. $this->next_page (). $this->last_page (). ' $this->select (). ' Page ';


Break


Case ' 3 ':


$this->next_page= ' next page ';


$this->pre_page= ' up a page ';


$this->first_page= ' home ';


$this->last_page= ' last ';


return $this->first_page (). $this->pre_page (). $this->next_page (). $this->last_page ();


Break


Case ' 4 ':


$this->next_page= ' next ';


$this->pre_page= ' last ';


return $this->pre_page (). $this->nowbar (). $this->next_page ();


Break


Case ' 5 ':


return $this->pre_bar (). $this->pre_page (). $this->nowbar (). $this->next_page (). $this->next_bar ();


Break


Case ' 6 ':


Previous page, next page, last page is enabled. You can enable "first page" as appropriate.


$this->first_page= ' <img src= '. Hostpath. ' Images/propageup.jpg ' width= "height=" ">";


$this->pre_page= ' <img src= '. Hostpath. ' images/propageup.jpg ' style= ' width:58px; height:18px; " > ';


$this->next_page= ' <img src= '. Hostpath. ' images/propagedown.jpg ' style= ' width:58px; height:18px; " > ';


$this->last_page= ' last page ';


Return "<TD width=39 align= ' center ' >". $this->select (). " </td> "." <td><span style= ' color: #666666 ' > Page </span></td> '. <TD width=72 align=right> ". $this->first_page ()." </td> "." <TD width=72 align=right> ". $this->pre_page ()." </td> "." <TD width=72 align=right> ". $this->next_page ()." </td> "." <TD width=72 align=right> ". $this->last_page ()." </td> ";


Return "<td align= ' right ' > Total [". $this->total. "] Item ". $this->nowindex." /". $this->totalpage." Page </td> "." <td width=145> ". $this->pre_page ()." ". $this->next_page (). " </td> "." <td width=145> ". $this->last_page ()." ".  $this->select (). " Page ";


Break


Case ' 7 ':


$this->next_page= ' <img src= '. Hostpath. ' Images/arrpagedown.jpg '/> ';


$this->pre_page= ' <img src= '. Hostpath. ' Images/arrpageup.jpg '/> ';


$this->first_page= ' <img src= '. Hostpath. ' Images/arrhome.jpg '/> ';


$this->last_page= ' <img src= '. Hostpath. ' Images/arrend.jpg '/> ';


if ($this->totalpage==0)


{$this->nowindex=0;}


return $this->first_page (). "". $this->pre_page (). "". $this->next_page (). "". $this->last_page (). "". $this->select ();


Break


Case ' 8 ':


Previous page, next page, last page is enabled. You can enable "first page" as appropriate.


$this->first_page= ' <img src= '. Hostpath. ' Images/propageup.jpg ' width= "height=" ">";


$this->pre_page= ' <img src= '. Hostpath. ' images/propageup.jpg ' style= ' width:58px; height:18px; " > ';


$this->next_page= ' <img src= '. Hostpath. ' images/propagedown.jpg ' style= ' width:58px; height:18px; " > ';


$this->last_page= ' last page ';


if ($this->totalpage==0)


{$this->nowindex=0;}


Return "<TD width=39 align= ' center ' >". $this->select (). " </td> "." <td><span style= ' color: #666666 ' > Page </span></td> '. <TD width=72 align=right> ". $this->first_page ()." </td> "." <TD width=72 align=right> ". $this->pre_page ()." </td> "." <TD width=72 align=right> ". $this->next_page ()." </td> "." <TD width=72 align=right> ". $this->last_page ()." </td> ";


Return "<td align= ' right ' > Total [". $this->total. "] Message ". $this->nowindex." /". $this->totalpage." Page </td> "." <td width=145> ". $this->pre_page ()." ". $this->next_page (). " </td> "." <td width=145> ". $this->last_page ()." ".  $this->select (). " Page ";


Break


}





}


/*----------------Private Function (private method)-----------------------------------------------------------* *


/**


* Set URL header address


* @param: String $url


* @return Boolean


*/


function _set_url ($url = "")


{


if (!empty ($url)) {


Manually set


$this->url= $url. (Stristr ($url, '? ')] & ': '? '). $this->page_name. " =";


}else{


Automatically get


if (Empty ($_server[' query_string ')) {


When there is no query_string


$this->url=$_server[' Request_uri ']. $this->page_name. " =";


}else{


//


if (Stristr ($_server[' query_string '), $this->page_name. ' =')){


Address has page parameters


$this->url=str_replace ($this->page_name. = '. $this->nowindex, ', $_server[' Request_uri ']);


$last = $this->url[strlen ($this->url)-1];


if ($last = = '? ' | | $last = = ' & ') {


$this->url.= $this->page_name. " =";


}else{


$this->url.= ' & '. $this->page_name. " =";


}


}else{


//


$this->url=$_server[' Request_uri ']. ' & '. $this->page_name. ' =';


}//end if


}//end if


}//end if


}





/**


* Set the current page


*


*/


function _set_nowindex ($nowindex)


{


if (empty ($nowindex)) {


System access





if (Isset ($_get[$this->page_name])) {


$this->nowindex=intval ($_get[$this->page_name]);


}


}else{


Manually set


$this->nowindex=intval ($nowindex);


}


}





/**


* Returns the address value for the specified page


*


* @param int $pageno


* @return String $url


*/


function _get_url ($pageno =1)


{


Return $this->url. $pageno;


}





/**


* Get pagination display text, for example, by default _get_text (' <a href= ' ">1</a> ') will return [<a href=" ">1</A>]


*


* @param String $str


* @return String $url


*/


function _get_text ($STR)


{


Return $this->format_left. $str $this->format_right;


}





/**


* Get the link address


*/


function _get_link ($url, $text, $style = ') {


$style = (Empty ($style))? ': ' class= '. $style. ' ";


if ($this->is_ajax) {


If you are using AJAX mode


Return ' <a '. $style. ' href= ' javascript: '. $this->ajax_action_name. ' (\ $url. \ ' > '. $text. ' </a> ';


}else{


Return ' <a '. $style. ' href= '. $url. ' " > '. $text. ' </a> ';


}


}


/**


* Error Handling mode


*/


Function error ($function, $errormsg)


{


Die (' Error in File <b> '. __file__. ') </b>, Function <b> '. $function. ' () </b>: '. $errormsg);


}


}


?>

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.