Hi:
I am using the following code and trying to achieve pagination. Right now the code works but with one exception. I only have six records in my database, and I've set the page to display 5 results, so technically my results should be Prev { Page 1 of 2 } Next Last; however, I'm getting Prev { Page 1 of 0 } Next Last. Can someone direct me to my error?
<?php
$image_dir = '/test/images/inventory/';
$pl = trim($_GET['pl']);
$ph = trim($_GET['ph']);
if(empty($ph))
$ph = 999999;
else
$ph = (int)$ph; //make integer
if(empty($pl))
$pl = 0;
else
$pl = (int)$pl; //make integer
//echo "pl: $pl <br/>";
//echo "ph: $ph <br/>";
$new_pre = $_GET['new_pre'];
//echo "new_pre: $new_pre <br/>";
$hull_material = $_GET['hull_material'];
//echo "hull_material: $hull_material <br/>";
$make = $_GET['make'];
//echo "make: $make <br/>";
//$where_condition = "WHERE condition = '$new'
//AND condition = '$pre-owned'";
switch ($new_pre) {
case "b":
$where_new_pre = "AND new_pre LIKE '%'";
break;
case "n":
$where_new_pre = "AND new_pre = 'new'";
break;
case "p":
$where_new_pre = "AND new_pre = 'pre-owned'";
break;
}
switch ($hull_material) {
case "a":
$where_hull_material = "AND hull_material = 'aluminum'";
break;
case "f":
$where_hull_material = "AND hull_material = 'fiberglass'";
break;
case "b":
$where_hull_material = "AND hull_material LIKE '%'";
break;
}
switch ($make) {
case "any":
$where_make = "AND make LIKE '%'";
break;
case "contender":
$where_make = "AND make = 'contender'";
break;
case "century":
$where_make = "AND make = 'century'";
break;
case "hydra-sports":
$where_make = "AND make = 'hydra-sports'";
break;
case "alweld":
$where_make = "AND make = 'alweld'";
break;
case "hewes":
$where_make = "AND make = 'hewes'";
break;
case "cobia":
$where_make = "AND make = 'cobia'";
break;
case "maverick":
$where_make = "AND make = 'maverick'";
break;
case "pathfinder":
$where_make = "AND make = 'pathfinder'";
break;
case "twin vee":
$where_make = "AND make = 'twin vee'";
break;
}
$where_price = "WHERE price > $pl
AND price < $ph";
$where = " $where_price $where_new_pre $where_hull_material $where_make ";
$simple = TRUE;
if ($pl <> 0) $simple = FALSE;
if ($make <> '') $simple = FALSE;
if ($new_pre <> '') $simple = FALSE;
if ($hull_material <> '') $simple = FALSE;
$query = "SELECT * FROM boats";
$result = mysql_query($query);
$num_record = mysql_num_rows($result);
$display = 5;
if(isset($_GET['page']))
{
$currentPage = $_GET['page'];
}else{
$currentPage = 1;
}
$lastPage = ceil($num_record/$display);
$limitQ = 'LIMIT ' .($currentPage - 1) * $display . ',' .$display;
$query_simple = "SELECT * FROM boats ORDER BY price DESC $limitQ";
$query_search = "SELECT * FROM boats
$where
ORDER BY price DESC $limitQ";
if ($simple) $query = $query_simple;
else $query = $query_search;
//echo "query: $query <br/>";
?>
and
<?php
// Connects to your Database
require_once('inc/dbstuff.php');
$result = mysql_query($query)
or die(mysql_error());
Echo "<table width=400 class=thrColLiqHdr>";
while($row = mysql_fetch_array( $result ))
{
Echo "<tr>";
Echo "<th>".$row['year'] . ' ' . $row['make'] . ' ' . $row['model'] . "</th>";
Echo "<th></th>";
Echo "<th></th>";
Echo "<th></th>";
Echo "<th>". (empty($row['price'])? 'Call for Price' : '$' . $row['price']) . "</th></tr>\n";
Echo "<td><img src=\"$image_dir{$row['pix']}\" width=\"140\" height=\"105\" /></td>";
Echo"<td><strong>Location:</strong> ".$row['location'] . "</td> ";
Echo"<td><strong>Condition:</strong> ".$row['new_pre'] . "</td> ";
Echo"<td><strong>Engine:</strong> ".$row['engine_make'] . "</td> ";
Echo"<td>".'<form id="search-form" action="" method="get">'
.'<input name="boat_details" type="submit" value="Details" />'
."</form></td></tr>\n";
}
Echo "</table>";
//pagination navigation (links)
//previous
if ($currentPage == 1) {
print "Prev ";
} else {
print "<a href=inventory copy.php?page=1>First page</a> ";
$previousPage = $currentPage-1;
print "<a href=inventory copy.php?page=$previousPage>Previous</a>";
}
print " { Page $currentPage of $lastPage } ";
//for next pages links
if ($currentPage== $lastPage) {
print "Next last";
} else {
$nextPage = $currentPage+1;
print " <a href=inventory copy.php?page=$nextPage>Next</a> ";
print " <a href=inventory copy.php?page=$lastPage>Last</a> ";
}
?>
Sorry for all the code, but as I'm not exactly sure what's causing the error, I didn't want to just provide bits and pieces.
Thank you!