<html>
<head>
<title>Implementing Paging with next and prev</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
require("includes/configure.php");
$rowsPerPage = 2;
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
$term=$_GET['terms'];
$keywords = split(' ' , $term);
if($options['st']==1)
if ($hf=fopen('stopwords.xml','r'))
{
for ($sfile='';$buf=fread($hf,4096);)
{
$sfile.=$buf;
}
$A=XML_unserialize($sfile);
$stopword = split(' ' , $A['stopwords']);
foreach($stopword as $w1)
{
foreach($keywords as $key=>$w2)
{
if(strcmp($w1,$w2)==0)
{
unset($keywords[$key]);
}
}
}
}
foreach($keywords as $keyword) {
if($options['w']!=1) {
if($options['t']==1) {
$qArray[] = "title LIKE '%$keyword%'";
}
if($options['u']==1) {
$qArray[] = "url LIKE '%$keyword%'";
}
if($options['alt']==1) {
$qArray[] = "alttext LIKE '%$keyword%'";
}
if($options['d']==1) {
$qArray[] = "metadescription LIKE '%$keyword%'";
}
if($options['k']==1) {
$qArray[] = "metakeywords LIKE '%$keyword%'";
}
if($options['b']==1) {
$qArray[] = "content LIKE '%$keyword%'";
}
if($options['l']==1) {
$qArray[] = "links LIKE '%$keyword%'";
}
}elseif($options['w']==1) {
if($options['t']==1) {
$qArray[] = "title LIKE '$keyword'";
}
if($options['u']==1) {
$qArray[] = "url LIKE '$keyword'";
}
if($options['alt']==1) {
$qArray[] = "alttext LIKE '$keyword'";
}
if($options['d']==1) {
$qArray[] = "metadescription LIKE '$keyword'";
}
if($options['k']==1) {
$qArray[] = "metakeywords LIKE '$keyword'";
}
if($options['b']==1) {
$qArray[] = "content LIKE '$keyword'";
}
if($options['l']==1) {
$qArray[] = "links LIKE '$keyword'";
}
}
if ((isset($qArray)) && (sizeOf($qArray>0))) {
$qString = ' where ';
$qString .= implode(" or ", $qArray);
$resource=tep_db_query("select * from files".$qString. " order by file_id $offset, $rowsPerPage");
$resource1=tep_db_query("select * from files".$qString. " order by file_id");
$rows=tep_db_num_rows($resource1);
}else {
$resource=tep_db_query("select * from files order by file_id");
}
while($results=tep_db_fetch_array($resource)) {
$res[$results[file_id]]['title']=$results['title'];
$res[$results[file_id]]['url']=$results['url'];
$res[$results[file_id]]['filesize']=$results['filesize'];
}
}
$sum=0;
for($l=0;$l<count($res);$l++)
{
$sum+=$res[$l]->wordcount;
}
function getmicrotime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = getmicrotime();
for ($i=0; $i < 1000; $i++) {
// do nothing, 1000 times
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
// how many rows to show per page
$query = "SELECT * FROM files".$qString. " order by file_id LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');
// how many rows we have in database
$query = "SELECT COUNT(*) AS numrows FROM files";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
$self = $_SERVER['PHP_SELF'];
// creating 'previous' and 'next' link
// plus 'first page' and 'last page' link
// print 'previous' link only if we're not
// on page one
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ' [Prev] '; // we're on page one, don't enable 'previous' link
$first = ' [First Page] '; // nor 'first page' link
}
// print 'next' link only if we're not
// on the last page
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' [Next] '; // we're on the last page, don't enable 'next' link
$last = ' [Last Page] '; // nor 'last page' link
}
// print the page navigation link
echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
include '../library/closedb.php';
?>
<table width="95%" border="0" align="center">
<tr>
<td>
<?php
$k=1;
foreach($res as $key=>$value)
{
?>
<dl>
<dt><font color="#000000" size=2 face="verdana, arial, helvetica, sans-serif"><b>
<?php echo $k;?>
. <a href="<?php echo $value['url'];?>">
<?php echo $value['title'];?>
</a></b></font></dt>
<dd>
<font size="1" color="#000000" face="verdana, arial, helvetica, sans-serif"><a href="<?php echo $value['url'];?>">
<?php echo 'http://'.$_SERVER['HTTP_HOST'].'/'.$value['url'];?>
</a><br>
<b>Matches:</b> <?php echo $res[$l]->wordcount;?>
<!--<b>Score:</b> 3.24-->
<b>File Size:</b> <?php echo $value['filesize'];?>KB
</font> </dd>
</dl>
<?php $k++; }
?>
</td>
</tr>
</table>
</body>
</html>