|
Posted by william on 11/03/05 14:46
I am trying to build a page where you search by selecting a type, and then
choosing either an area or entering a search term, that is all working
fine. My problem is in implementing paging.
I have managed to restrict the number of results but have been unable to
get the links to the next set of results working.
The code (not complete) is:
<?php
##includes here##
if (isset($_GET['submit'])){
##error checking and opening DB##
// how many rows per page
$rowsPerPage = 3;
// 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;
if ($search ==""){
$query = mysql_query(
"SELECT t.id,
t.name,
t.addr_1,
t.addr_2,
t.addr_3,
t.addr_4,
t.post_code,
t.short_desc,
t.phone_1,
t.email,
t.website,
t.cost_single,
t.cost_double,
GROUP_CONCAT(facilities.path)
FROM hotel t
JOIN h_link l ON t.id = l.id
JOIN facilities ON l.f_id = facilities.f_id
WHERE addr_4 = $area'
GROUP BY t.id LIMIT $offset, $rowsPerPage" );
} else {$query = mysql_query(
"SELECT t.id,
t.name,
t.addr_1,
t.addr_2,
t.addr_3,
t.addr_4,
t.post_code,
t.short_desc,
t.phone_1,
t.email,
t.website,
t.cost_single,
t.cost_double,
GROUP_CONCAT(facilities.path)
FROM hotel t
JOIN h_link l ON t.id = l.id
JOIN facilities ON l.f_id = facilities.f_id
WHERE name like '%$search%'
GROUP BY t.id LIMIT $offset, $rowsPerPage" );
}
if (!$query)
die( 'Error selecting details from database please try again later: ' .
mysql_error() );
##output data##
// rows in database
$query = "SELECT COUNT(id) AS numrows FROM hotel";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages ?
$maxPage = ceil($numrows/$rowsPerPage);
$self = $_SERVER['PHP_SELF'];
####################################
// creating links
// 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?area=$area?search=$search?accom=$accom?submit='submit'\">[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;
##close connection more includes etc##
?>
I'm fairly sure that the problem is that the query is not being rerun to
get the next set of results so I have tried to pass the variables through
the url(as you can see on the $last entry), but I just go back to the
reloaded search page when clicking on the link.
Any suggestions.
Thanks
[Back to original message]
|