Pagination not returning all the search results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MikeSpider
Forum Commoner
Posts: 25
Joined: Sat Oct 22, 2011 6:45 pm

Pagination not returning all the search results

Post by MikeSpider »

Hi guys,

I got a problem with a pagination script,
The page is a gallery page with thumbnails that show as soon as the page loads.
There is a limit parameter in the sql, so that only 6 thumbs are presented at a time.
The next and previous links are also working as it's supposed to.

However, when the next get's clicked, it returns the next 6 pictures. I thought everything was fine,
until when I had more than 12 pictures, in my case ,18 pictures. It only shows next results, 2 times!
when I click next the third time, nothing happens! althoug there are still results to show.

Code: Select all

//My class function with sql

 //fetch all images
    private function fetchAllImages()
    {
        try{ 
            //pagination script
             if(!isset($_GET['startrow']) || !is_numeric($_GET['startrow']))
            {
                $startrow =0;
            }else{
                $startrow = (int)$_GET['startrow'];
            }

            $prev = $startrow - 6;

            //only print a "Previous" link if a "Next" was clicked
            if ($prev >= 0){

            $this->display = true;
        }else{
         $this->display = false;
        }
            if("" !=$this->cat){
            //run query

            $sql = "SELECT * FROM images WHERE category='{$this->cat}'";
        }else{

            $sql = "SELECT * FROM images ORDER BY category LIMIT $startrow, 6";
        }
            $result = $this->db->mysqli->query($sql);
                    //test result
                    if(!$result)
                    {       //error handler
                           throw new Exception("Query failed: " . $sql . " - " . $this->db->mysqli->error);

                    }else{
                            //return result from query
                        
                            return $result;

                    }
                    //error handler
            } catch(Exception $e){
               echo("Message: " . $e->getMessage());
            }

    }


Code: Select all

// my gallery.php page

....
<?php
//display
$gallery = new Content();
$gallery->PublicDisplayGallery($prev);

?>
...

 <?php  for($loop = 0; $loop < count($gallery->data);$loop ++) { ?>

    <div style="float:left;margin-right: 4px; margin-left: 4px;">
<a href="images/gallery/<?php echo($gallery->data[$loop]['image']);?>" target="_blank"><img  alt="" border="2px" width ="150px" height="113px" src="images/gallery/<?php echo($gallery->data[$loop]['image']);?>"></a>
        <br>
        <span style="font-size:small">description: <?php echo($gallery->data[$loop]['description']);?></span>
        <br>
          <span style="font-size:small">category: <?php echo($gallery->data[$loop]['category']);?></span>
        <br>

   </div>

<?php } ?>

 <!--PAGINATION NEXT AND PREVIOUS -->
         <div align="right">
         <table>
             <tr>
                   <?php

                    //only print a "Previous" link if a "Next" was clicked
                   $prev = $gallery->prev;
                   $startrow = $gallery->startrow;

                    ?>

                 <td><?php if(true == $gallery->Display()){ echo '<a style ="color:red" href="'.$_SERVER['PHP_SELF'].'?startrow='.$prev.'"><< Previous</a>';

                 }else{echo("");}?></td>

                  <td> <?php  echo'<a  style ="color:red" href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+6).'">Next >></a>';  ?></td>

                </tr>

        </table>
              </div>
            <!--END OF PAGINATION NEXT AND PREVIOUS -->
...


Any help will be greatly appreciated.
Thanks,
Mike Spider
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Pagination not returning all the search results

Post by Celauran »

MikeSpider wrote:There is a limit parameter in the sql, so that only 6 thumbs are presented at a time.
The next and previous links are also working as it's supposed to.

However, when the next get's clicked, it returns the next 6 pictures. I thought everything was fine,
until when I had more than 12 pictures, in my case ,18 pictures. It only shows next results, 2 times!
when I click next the third time, nothing happens!
I don't understand what the problem is. The code looks fine and, by your description, everything appears to be working properly.
Initial page shows images 1-6. Click next once, it shows images 7-12. Click next a second time, it shows images 13-18. There are no images left to show, so clicking next a third time is supposed to show a blank page. Where's the problem?
MikeSpider
Forum Commoner
Posts: 25
Joined: Sat Oct 22, 2011 6:45 pm

Re: Pagination not returning all the search results

Post by MikeSpider »

I made a mistake, it only works when I click " once" on the next, twice won't work!

Thanks for your reply,
Mike Spider
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Pagination not returning all the search results

Post by Celauran »

Have you tried echoing the SQL query and running it manually? Does that produce results?
MikeSpider
Forum Commoner
Posts: 25
Joined: Sat Oct 22, 2011 6:45 pm

Re: Pagination not returning all the search results

Post by MikeSpider »

If you mean if query is working fine yes!
if I remove the limit parameter it displays the entire results

what you mean by running manualy?

thanks,
Mike spider
MikeSpider
Forum Commoner
Posts: 25
Joined: Sat Oct 22, 2011 6:45 pm

Re: Pagination not returning all the search results

Post by MikeSpider »

Other problem I encountered,
when doing the search by category, the next does'nt work.
The query does work, as it limits the search results, but whenever the next is clicked, it returns to default page state as if no search was made.

The way I made it was something like:

Code: Select all

<?php 

$default_display = new Content();
$default_display->Display();

if("Search by category"  == $_POST['category']){

$search=true;
$display_by_cat = new Content();
$display_by_cat->DisplayByCat();
}
?>

<?php PublicHeader(); ?>

<div id="main_content">

<?php if(true == $search) { ?>
<?php  for( $loop = 0; $loop < count($display_by_cat->data); $loop++ ) { ?>

<img alt="" src="<?php echo($display_by_cat->data[$loop]['image']); ?>">
<?php } ?>

<!-- PAGINATION NEXT AND PREVIOUS-->

<div align="right">
         <table>
             <tr>
                   <?php

                    //only print a "Previous" link if a "Next" was clicked
                   $prev = $display_by_cat->prev;
                   $startrow = $display_by_cat->startrow;

                    ?>

                 <td><?php if(true == $display_by_cat->Display()){ echo '<a style ="color:red" href="'.$_SERVER['PHP_SELF'].'?startrow='.$prev.'"><< Previous</a>';

                 }else{echo("");}?></td>

                  <td> <?php  echo'<a  style ="color:red" href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+6).'">Next >></a>'; ?></td>

                </tr>

        </table>
            <!--END OF PAGINATION NEXT AND PREVIOUS -->


<?php } else{  ?>

<?php  for( $loop = 0; $loop < count($default_display->data); $loop++ ) { ?>

<img alt="" src="<?php echo($default_display->data[$loop]['image']); ?>">
<?php } ?>

<!-- PAGINATION NEXT AND PREVIOUS-->

<div align="right">
         <table>
             <tr>
                   <?php

                   $prev = $default_display->prev;
                   $startrow = $default_display->startrow;

                    ?>

                 <td><?php if(true == $default-display->Display()){ echo '<a style ="color:red" href="'.$_SERVER['PHP_SELF'].'?startrow='.$prev.'"><< Previous</a>';

                 }else{echo("");}?></td>

                  <td> <?php  echo'<a  style ="color:red" href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+6).'">Next >></a>'; ?></td>

                </tr>

        </table>
            <!--END OF PAGINATION NEXT AND PREVIOUS -->

<?php }?>

</div>

<?php PublicFooter();?>
Any light shed will be greatly appreciated,
Mike spider
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Pagination not returning all the search results

Post by Celauran »

Code: Select all

if("" !=$this->cat){
            //run query

            $sql = "SELECT * FROM images WHERE category='{$this->cat}'";
        }
There's no LIMIT on the query when a category is selected, which is why it's behaving differently.
MikeSpider
Forum Commoner
Posts: 25
Joined: Sat Oct 22, 2011 6:45 pm

Re: Pagination not returning all the search results

Post by MikeSpider »

I added the LIMIT clause, still have the same problem.

Thanks for your reply,

Mike Spider
MikeSpider
Forum Commoner
Posts: 25
Joined: Sat Oct 22, 2011 6:45 pm

Re: Pagination not returning all the search results

Post by MikeSpider »

I think for it to work with the search, I might need to pass the search parameters to the next link??

Any ideias, please.

Mike Spider
Post Reply