Sunday, 29 September 2013

Pagination showing more page links than there should be

Pagination showing more page links than there should be

Im using this query:
$sql = "SELECT * FROM {$table} ";
$sql .= "WHERE gallery_name='pancakes' ";
$sql .= "ORDER BY id DESC ";
$sql .= "LIMIT {$per_page} ";
$sql .= "OFFSET {$pagination->offset()}";
$pages = Photograph::find_by_sql($sql);
This is my pagination class:
class Pagination extends DatabaseObject{
public $page;
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(){
global $blog;
return $this->current_page - 1;
}
public function next_page(){
global $blog;
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;
}
}
$pagination = new Pagination();
This is my pagination function:
public function pagination(){
global $address;
global $selected_page;
global $pagination;
global $page;
$min = max($selected_page - 2, 1);
$max = min($selected_page + 2, $pagination->total_pages());
for($i = $min; $i <= $max; ++$i){
if(static::$table_name == "pages"){
if($i == $this->page){
echo "\n <span class=\"selected_page\">{$i}</span> ";
}else{
echo "\n <a href=" . $address . "{$i}>{$i}</a> ";
}
}elseif(static::$table_name == "cakes"){
if($i == $selected_page){
echo "\n <span class=\"selected_page\">{$i}</span> ";
}else{
echo "\n <a href=" . $address . "{$i}>{$i}</a> ";
}
}
}
}
I wanted to divide my photo gallery into sub-galleries so I added a column
"gallery_name" into the table called "photographs". It worked, but the
pagination still shows the same number of pages as before, I'm guessing
because it still counts all the photos instead of just the ones with the
given name in the gallery_name column. The first few links of the selected
sub-gallery pagination work, depending on the number of photos selected by
the query, and the rest are dead links. How do I get rid of the dead links
in pagination and display just the ones that reflect the values of the
query?

No comments:

Post a Comment