Hello!
Sorry for posting this long code, been struggling for this a long time now) 2 days), and don't know where in my code the problem is really.
With this paging tutorial I have tried to add paging it to my code. http://www.phpnoise.com/tutorials/9/1
Before adding it, the code worked fine, and the results was printed in a long list(without paging).
Now, there is no result at all, and no error message. (its is just one time with "contact person", "area code" etc, but no database output data)
I belive I have made one of these two faults.
1) Use the class incorrect. (haven't used classes before)
2) Somehow miss to get the data from the dataset when echo:ing the data from the database.
Thanks in advance.
The whole page:
<?php
class Pager
{
function getPagerData($numHits, $limit, $page)
{
$numHits = (int) $numHits;
$limit = max((int) $limit, 1);
$page = (int) $page;
$numPages = ceil($numHits / $limit);
$page = max($page, 1);
$page = min($page, $numPages);
$offset = ($page - 1) * $limit;
$ret = new stdClass;
$ret->offset = $offset;
$ret->limit = $limit;
$ret->numPages = $numPages;
$ret->page = $page;
return $ret;
}
}
?>
<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7">
<?php include "head.html"; ?>
</head>
<body>
<div align="center">
<b>Company directory search results</b>
<p>
<A HREF="search.php"><b>Search again</b></A>
<p>
<TABLE>
<TR>
<TD>
<font size="2">
<?
include 'settings1.php';
if ($category=="allcats")
$strCategory = "";
else
$strCategory = " AND category = '" . $category . "' ";
if ($town=="alltowns")
$strTown = "";
else
$strTown = " AND town = '" . $town . "' ";
// get the pager input values
$page = $_GET['page'];
$limit = 5;
$result = mysql_query("select count(*) from companies WHERE name LIKE '%" . $companyname . "%' " . $strCategory . $strTown);
$total = mysql_result($result, 0, 0);
// work out the pager values
$pager = Pager::getPagerData($total, $limit, $page);
$offset = $pager->offset;
$limit = $pager->limit;
$page = $pager->page;
// use pager values to fetch data
$query = "select * from companies order by someField limit $offset, $limit";
while($result = mysql_query($query)){
echo "<b>";
echo $result['name'];
echo "</b>";
echo "<br>Contact person:";
echo $result['contactperson'];
echo "<br>";
echo $result['address'];
echo "<br>";
echo $result['areacode'];
echo " ";
echo $result['town'];
echo "<br>Phone number:";
echo $result['phonenumber'];
echo "<br>Email:";
echo $result['email'];
echo "<br>Web site:";
echo "<a href=\"".$result['website']."\" target=\"_blank\">".$result['website']."</a>";
echo "<p>";
}
// output paging system (could also do it before we output the page content)
if ($page == 1) // this is the first page - there is no previous page
echo " Previous ";
else // not the first page, link to the previous page
echo "<a href=\"search2test.php?page=" . ($page - 1) . "\"> Previous </a>";
for ($i = 1; $i <= $pager->numPages; $i++) {
echo " | ";
if ($i == $pager->page)
echo "Page $i";
else
echo "<a href=\"search2test.php?page=$i\">Page $i</a>";
}
if ($page == $pager->numPages) // this is the last page - there is no next page
echo " Next ";
else // not the last page, link to the next page
echo "<a href=\"search2test.php?page=" . ($page + 1) . "\"> Next </a>";
//TEMP just to check the output data
echo "<br>";
echo "<br>";
echo "pager:";
echo $pager;
echo "<br>";
echo "offset:";
echo $offset;
echo "<br>";
echo "limit:";
echo $limit;
echo "<br>";
echo "page:";;
echo $page;
echo "<br>";
?>
</TD>
</TR>
</TABLE>
</body>
</html>