As a PHP developer, one thing that is probably very useful is the Pear class library. We know that Pear::Pager specializes in handling paging, and Pear::HTML_AJAX is a class library for Ajax applications. But Pager is not only used for paging links in HTML, it is already ready for Ajax, which is more popular than Super Girl. Let’s take a look at how these two packages are integrated and applied.
Pager and Javascript
Now that we have taken the first step, Pager can already build JavaScript links. Integration with Ajax should be possible in theory. The following is an example of Pager and JavaScript paging:
PLAIN TEXT
PHP:
require_once'Pager/Pager.php';
$data=range(1,100);//an array of data to paginate
$pager_params=array(
'mode' =>'Sliding',
'append' =>false, //don't 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</h1>
<?phpecho$links['pages'];?>
<hr />
<?php
for($i=1;$i<=$n_pages; ++$i){
echo'<div class="page" id="page'.$i.'">';
echo'<h2>Page '.$i.'</h2>';
foreach($pager->getPageData($i)as$item){
echo'Item '.$item.'<br />';
}
echo'</div>';
}
?>
<hr />
<script type="text/javascript"language="javascript">
revealDiv(1);
</script>
</body>
</html>
In the example, all the paging data is put into a div tag, and two important parameters are defined: path (empty) and fileName. fileName was originally supposed to be the file name of the link, but it is replaced with javascript here.
Ajax battle example includes three files page.html, server.php, testdata.php
page.html
Use dynamic javascript--server.php to process data, and call HTML_AJAX.replace('target', 'testdata.php') to replace the content of the div with the id of target.
PLAIN TEXT
PHP:
<html>
<body>
<h1>PEAR::Pagerexample with AJAX</h1>
<script type="text/javascript"src="server.php?client=all"></script>
<div id="target">I'm the target</div>
<script type="text/javascript">
HTML_AJAX.replace('target', 'testdata.php');
</script>
</body>
</html>
testdata.php
This is a very simple php program that gets the paging data and outputs html
PLAIN TEXT
PHP:
<?php
require_once'Pager/Pager.php';
$data=range(1,100);//an array of data to paginate
$pager_params=array(
'mode' =>'Sliding',
'append' =>false, //don't append the GET parameters to the url
'path' =>'',
'fileName'=>'javascript:HTML_AJAX.replace('target','testdata.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('Ymd H:i:s').'</span></p>';
echo'<h3>Page '.$pager->getCurrentPageId().'</h3>';
foreach($pager->getPageData()as$item){
echo'Item '.$item.'<br />';
}
echo'<hr />'.$pager->links;
?>
Finally, paste the code of the behind-the-scenes worker server.php and use HTML_AJAX_Server to handle the troublesome things like ajax.
PLAIN TEXT
PHP:
<?php
include'HTML/AJAX/Server.php';
$server=newHTML_AJAX_Server();
$server->handleRequest();
?>