As a PHP developer, one of the most useful things is the Pear class library. We know that Pear: Pager is dedicated to processing pages, while Pear: HTML_AJAX is some class libraries of Ajax applications. However, Pager is not only used for html paging links. It is ready for Ajax, which is more popular than the Supergirl. Let's take a look at how these two packages integrate the application.
Pager and Javascript
Now, the first step is to build a javascript link and integrate it with Ajax. In theory, it should also be implemented. The following is an example of Pager and javascript paging:
PLAIN TEXT
PHP:
Require_oncePager/Pager. php;
$ Data = range (1,100); // an array of data to paginate
$ Pager_params = array (
Mode => Sliding,
Append => false, // dont append the GET parameters to the url
Path =>,
FileName => javascript: revealDiv (% d), // Pager replaces "% d" with the page number...
PerPage => 10, // show 10 items per page
Delta => 5,
ItemData => $ data,
);
$ Pager = & Pager: factory ($ pager_params );
$ N_pages = $ pager-> numPages ();
$ Links = $ pager-> getLinks ();
?>
<Html>
<Head>
<Script type = "text/javascript" language = "javascript">
Varn_pages = <? Phpecho $ n_pages?>;
FunctionrevealDiv (n)
{
For (varcount = 1; count <= n_pages; count ++ ){
Document. getElementById ("page" + count). style. display = none;
}
Document. getElementById ("page" + n). style. display = block;
}
</Script>
<Style type = "text/css">
Div. page {
Background: # FFFF99;
Border-top: 1px solid # FFBF99;
Border-bottom: 1px solid # FFBF99;
}
</Style>
</Head>
<Body>
<H1> PEAR: Pagerexample with JavaScript <? Phpecho $ links [pages];?>
<Hr/>
<? Php
For ($ I = 1; $ I <= $ n_pages; ++ $ I ){
Echo <div class = "page" id = "page. $ I.">;
Echo Foreach ($ pager-> getPageData ($ I) as $ item ){
EchoItem. $ item. <br/>;
}
Echo </div>;
}
?>
<Hr/>
<Script type = "text/javascript" language = "javascript">
RevealDiv (1 );
</Script>
</Body>
</Html>
In this example, all the data in the page is put into a div tag and two important parameters are defined: path (empty) and fileName. fileName should be the file name of the link, but javascript is replaced here.
Getting started with Ajax
This example contains three files: page.html, server. php, and testdata. php.
Page.html
Use Dynamic javascript -- server. php to process data. Call HTML_AJAX.replace (target, testdata. php) to replace the content of the div whose id is target.
PLAIN TEXT
PHP:
<Html>
<Body>
<H1> PEAR: Pagerexample with AJAX
<Script type = "text/javascript" src = "server. php? Client = all "> </script>
<Div id = "target"> Im the target </div>
<Script type = "text/javascript">
HTML_AJAX.replace (target, testdata. php );
</Script>
</Body>
</Html>
Testdata. php
This is a simple php program. It retrieves paging data and outputs html
PLAIN TEXT
PHP:
<? Php
Require_oncePager/Pager. php;
$ Data = range (1,100); // an array of data to paginate
$ Pager_params = array (
Mode => Sliding,
Append => false, // dont append the GET parameters to the url
Path =>,
FileName => javascript: HTML_AJAX.replace (arget, estdata. php? PageID = % d);, // Pager replaces "% d" with the page number...
PerPage => 10, // show 10 items per page
Delta => 1,
ItemData => $ data,
);
$ Pager = & Pager: factory ($ pager_params );
$ N_pages = $ pager-> numPages ();
$ Links = $ pager-> getLinks ();
Echo <p> This container is loaded with an AJAX call </p>;
Echo <p> <span class = "datetime"> DateTime:. date (Y-m-d H: I: s). </span> </p>;
Echo Foreach ($ pager-> getPageData () as $ item ){
EchoItem. $ item. <br/>;
}
Echo ?>
Finally, paste the code of the behind-the-scenes worker server. php.
Use HTML_AJAX_Server to handle ajax.
PLAIN TEXT
PHP:
<? Php
IncludeHTML/AJAX/Server. php;
$ Server = newHTML_AJAX_Server ();
$ Server-> handleRequest ();
?>