SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    915
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    PHP Code:
    <?
     
    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
    PHP Code:
    while($q=mysql_fetch_array($rows)){ 
    back to

    PHP Code:
    while($q=mysql_fetch_array($result)){ 

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,446
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    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.

  3. #3
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    915
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi fretburner,

    OK right, thanks for that.

    Lets have a look at this again.

    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •