Hello,
I've had a go at paginating my database query results. The results still come up, the links appear on the bottom "PREV 1 2 3 4 5 NEXT" and are linkable but limiting the results is giving me problems. I try adding "LIMIT $limitvalue, $limit";" on the query but then it brings up just a blank page. If i remove it and leave the query as it is, it returns all the records. It calculates the number of pages needed for the reesult but doesnt actually spread the results over those pages.
LIMIT $limitvalue, $limit";
This is my query : $query = "SELECT * FROM bricks ".$where;
If i make it like this :
$query = "SELECT * FROM bricks ".$where LIMIT $limitvalue, $limit";
its just a blank page indicating a failed query
Can anyone spot my mistake?
Thanks
$query = "SELECT * FROM bricks ".$where LIMIT $limitvalue, $limit";
MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database");
function secured($val)
{
$val = strip_tags(trim(($val))) ;
$val = escapeshellcmd($val);
return stripslashes($val);
}
if (get_magic_quotes_gpc()) {
$Manufacturer = $_POST['Manufacturer'];
$Color = $_POST['Color'];
} else {
$Manufacturer = addslashes($_POST['Manufacturer']);
$Color = addslashes($_POST['Color']);
}
if(isset($_POST['brick'])) {
$where = "";
$sep = " WHERE ";
}
if($Manufacturer != "SelectManufacturer") {
$where .= $sep." Manufacturer = '".$Manufacturer."'";
$sep = " AND ";
}
if($Color != "SelectColor") {
$where .= $sep." colour = '".$Color."'";
$sep = " AND ";
}
$limit = 25;
if(empty($page)){
$page = 1;
}
$limitvalue = $page * $limit - ($limit);
$query = "SELECT * FROM bricks $where LIMIT $limitvalue, $limit";
$result = mysql_query($query) or die("Failed: ".mysql_error());
$totalrows = mysql_num_rows($result);
if(!$totalrows)
{
echo "<table align='center'><tr><td align='center'>Sorry, no bricks matching your selection were found. </td></tr></table>"; //message for when no bricks are returned
}
else
{
echo "<table align='center' border=0 ><tr>";
echo "<td align='left' width=250 >There are <b>$totalrows</b> matches to your query</td>"; //message saying how many matches for query
echo "<table align='center' border=1 background=/graycolour.gif><tr>";
echo "<td align='left' width=125 bgcolor='#11d163' color='#FF0000'><b>Supplier</b></td>";
echo "<td align='left' width=125 bgcolor='#11d163'><b>Color</b></td>";
echo "<td align='left' width=125 bgcolor='#11d163'><b>Details</b></td>";
echo "<td align='center' bgcolor='#11d163'><b>Picture</b></td>";
echo "</tr>";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr><td align='left'>".$row["Manufacturer"]."</td>";
echo "<td align='left'>".$row["Colour"]."</td>";
echo "<td align='left'>".$row["Brick Name"]."</td>";
echo "<td align='left'>".$row["Image"]."</td>";
echo "</tr>";
}
}
echo "</table>";
if($page != 1){
$pageprev = $page--;
echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> ");
}else{
echo("PREV".$limit." ");
}
$numofpages = $totalrows / $limit;
for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
}
if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
}
if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page++;
echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");
}else{
echo("NEXT".$limit);
}
mysql_free_result($result);
?>