Pageing problem with results not displaying

I am in the process of adding pageing to my output, but although the code seems fine to me, no results are coming out.


<?
 if (!(isset($pagenum)))
 {
 $pagenum = 1;
 }
// Build result query on values of the array
$sql.="select * FROM tbl_hotels $max". ((!empty($sqlcategory)) ? $sqlcategory : "") ." WHERE Act_Hot='1' ". ((!empty($sqlcategory2)) ? $sqlcategory2 : "") ." ";
if ($sqlregion==""){
	$sql.= " AND IdCntry_Hot='".$selectCountry."'" ;
} else {
	$sql.= ""  .$sqlregion . "";
}
if ($sqlstar==""){
	$sql.= " AND IdCat_Hot IN (1,2,3,4,5,6)";	
} else {
	$sql.= "" . $sqlstar . "";
}

$order = " GROUP by Id_Hot ASC";
$order2 = " GROUP by tbl_hotels.Id_Hot ASC";

$sql.= "". ((!empty($sqlcategory)) ? $order2 : $order) ."";

$result = mysql_query($sql);
$rows = mysql_num_rows($result);
//echo $sql;


//Start of pageing

 $page_rows = 4;
 $last = ceil($rows/$page_rows);

 if ($pagenum < 1)
 {
 $pagenum = 1;
 }
 elseif ($pagenum > $last)
 {
 $pagenum = $last;
 }

 $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;


//End of paging

while($q=mysql_fetch_array($rows)){

?>
<div class="result_Hotel">
<p style="position:relative; margin:10px;"><?=$q['Id_Hot']?></p>
<p style="position:relative; margin:10px;"><?=$q['Nom_Hot']?></p>
</div>
<? }
// End Build query	
?>
</div>

</div>

The output returns, when I change


while($q=mysql_fetch_array($rows)){

back to


while($q=mysql_fetch_array($result)){

The reason you’re not getting any output from $q=mysql_fetch_array($rows) is because you previously set $rows to be the number of rows in the result, so it’s just a number, not a result set.

To paginate your results you actually need to do two queries… the first one should just count the number of rows that match your criteria. Rather that return all the data from the table (which is inefficient) you can just return the row count like this: “SELECT COUNT(*) AS total_rows FROM ….”.

Then do your main query to the DB with your LIMIT statement appended, and it will return the correct page of results.

Hi fretburner,

OK right, thanks for that.

Lets have a look at this again.

Thanks