Problem with Pagination calling a record excluded from the sql

If this posted twice I’m sorry. I got an error the first time that it was already submitted but I didn’t see it in the forum and I couldn’t view it through my posts.

I developed a site. It is a message board/photo sharing site. When users upload images to the message board it posts them in the photo share but not the other way around. Now I’ve found that when someone doesn’t post a picture to the forum page the id value becomes 0. Which is fine, but since there is no photo it craps it out on the forum page and the no-image post doesn’t show nor does anything before it because the posts are listed newest first. So I created a dummy image and uploaded it and called it no_image.jpg, then changed its id to 0. So the forum page now works fine, it shows all of the posts. Now there is a new issue. I don’t want the no_image.jpg file to show up in the Photo Share section of the site. So I altered the sql code to
[php]$sql = "SELECT * FROM photographs WHERE id != 0 ";
$sql .= "LIMIT {$per_page} ";
$sql .= “OFFSET {$pagination->offset()}”;
[/php]

So the image itself doesn’t show up in the Photo Share, but Pagination still sees that there is a record and gives it a page number. This no_image file is set as the fifth record, so Pagination has it as 5. If you click 5 the page broke and messed up the layout. So I played with the code trying to fix it and I got it to not break the layout now it just doesn’t show the image. So the sql is right, but the Pagination is wrong, but I don’t see where the Pagination could be picking it up.

Pagination code on the header.php file:

[php] //1. the current page number ($current_page)
$page = !empty($_GET[‘page’]) ? (int)$_GET[‘page’] : 1;

            //2. records per page ($per_page)
            $per_page = 1;
            
            //3. total record count ($total_count)
            $total_count = Photograph::count_all();
                            
            
            //Find all photos
            //use Pagination instead
            //$photos= Photograph::find_all();
            
            $pagination = new Pagination($page, $per_page, $total_count);
            
            //Instead of finding all records, just find the records
            //for this page
            $sql = "SELECT * FROM photographs HAVING id != 0 ";
            $sql .= "LIMIT {$per_page} ";
            $sql .= "OFFSET {$pagination->offset()}";
            $photos = Photograph::find_by_sql($sql);
            
            //Need to add ?page=$page to all links we want to
            //maintain the current page(or store $page in $session)

[/php]

Part where this code is called.

[php]<?php
foreach($photos as $photo):
if($photo->id == 0) {
echo ‘no file’;
} else {
?>

Photos of our pets.



Photo Share Photo

<?php echo $photo->caption; ?>


<?php } endforeach; ?>
<div id="pagination" style="clear: both;" align="center">
<?php
            if($pagination->total_pages() > 1) {
                            
                            if($pagination->has_previous_page()) {
                                            echo "<a href=\"index.php?page=";
                                            echo $pagination->previous_page();
                                            echo "\">&laquo; Previous</a> ";
                            }
                            
                            for($i=1; $i <= $pagination->total_pages(); $i++) {
                                            if($i == $page) {
                                                            echo " <span class=\"selected\">{$i}</span> ";
                                            } else {
                                            echo " <a href=\"index.php?page={$i}\">{$i}</a> ";
                            }
                            }
                            
                            if($pagination->has_next_page()) {
                                            echo " <a href=\"index.php?page=";
                                            echo $pagination->next_page();
                                            echo "\">Next &raquo;</a> ";
                            }
            }
            ?><br />
<div align="center">
            <a href="photo_upload.php">Upload a new photograph</a>
[/php]

Pagination.php where the pagination class is held:

[php]class Pagination {

            public $current_page;
            public $per_page;
            public $total_count;
            
            public function __construct($page=1, $per_page=20, $total_count=0){
                            $this->current_page = (int)$page;
                            $this->per_page = (int)$per_page;
                            $this->total_count = (int)$total_count;
            }
            
            public function offset() {
                            //Assuming 20 items per page:
                            //page 1 has an offset of 0                           (1-1) * 20
                            // page 2 has an offset of 20       (2-1) * 20
                            //in other words, page 2 starts with item 21
                            return ($this->current_page - 1) * $this->per_page;
            }
            
            public function total_pages() {
                            return ceil($this->total_count/$this->per_page);                             
            }
            
            public function previous_page() {
                            return $this->current_page - 1;
            }
            
            public function next_page() {
                            return $this->current_page +1;
            }
            
            public function has_previous_page() {
                            return $this->previous_page() >= 1 ? true : false;
            }
            
            public function has_next_page() {
                            return $this->next_page() <= $this->total_pages() ? true : false;
            }

}
[/php]

I can’t see where the issue is, but I need to get it fixed. Please let me know if you can help me or if you need further code or anything. Thanks!

DC

Sponsor our Newsletter | Privacy Policy | Terms of Service