A módosított Paginator Class.

1
2
3
4
5
6
7
8
9
<?php class Paginator{ var $base_url = 'http://www.interangol.hu'; var $total_rows = 0; var $per_page = 10; var $total_pages = 0; var $num_links = 4; var $cur_page = 0; var $first_link = 'First ‹'; var $previous_link = '<'; var $last_link = 'Last ›'; var $next_link ='Previous >'; var $full_tag_open = ''; var $full_tag_close = ''; var $first_tag_open = '<span class="first_tag">'; var $first_tag_open_faded = '<span class="first_tag faded">'; var $first_tag_close = '</span>'; var $last_tag_open = '<span class="last_tag">'; var $last_tag_open_faded = '<span class="last_tag faded">'; var $last_tag_close = '</span>'; var $cur_tag_open = '<strong>'; var $cur_tag_close = '</strong>'; var $next_tag_open = '<span class="next_tag">'; var $next_tag_open_faded = '<span class="next_tag faded">'; var $next_tag_close = '</span>'; var $previous_tag_open = '<span class="previous_tag">'; var $previous_tag_open_faded = '<span class="previous_tag faded">'; var $previous_tag_close = '</span>'; var $num_tag_open = '<span class="num_tag">'; var $num_tag_close = '</span>'; var $navi_wrapper_tag_open='<div class="navi_wrapper_open">'; var $navi_wrapper_tag_close = '</div>'; var $output = ''; var $need_previous = true; var $need_first = true; var $need_next = true; var $need_last = true; function Paginator($params = array()){ if(count($params) > 0){ $this->init($params);} else { echo "Paginator class was initiated without the neccessary parameters!";} } function init($params){ if(count($params) > 0){ foreach($params as $key => $value){ if(isset ($this->$key)){ $this->$key = $value; } } } this.create_links(); } function create_links(){ if($this->total_rows == 0 || $this->per_page == 0){ return ''; } $this->total_pages = ceil($this->total_rows/$this->per_page); if($this->total_pages == 1){ return '';} if(!is_numeric($this->cur_page)|| $this->cur_page <= 0) {$this->cur_page = 1;} if(($this->cur_page * $this->per_page) > $this->total_rows){ $this->cur_page = $this->total_pages; } $start = ($this->cur_page - $this->num_links > 0)? $this->cur_page - $this->num_links : 1; $end = (($this->cur_page + $this->num_links) > $this->total_pages)? $this->total_pages : ($this->cur_page + $this->num_links); $diff = ($end +1 - $start); $full_length = ($this->num_links * 2) + 1; if($full_length < $this->total_pages){ if($diff < $full_length){ if($start - $this->num_links < 1){ $end += ($full_length - $diff); if($end > $this->total_pages){ $end = $this->total_pages; $this->need_last = false; } } if(($this->cur_page + $this->num_links) > $this->total_pages){ $start -= ($full_length - $diff); if($start < 1){ $start = 1; $this->need_first = false; } } } } else{ $start=1; $end = $this->total_pages; $this->need_next = false; $this->need_last = false; $this->need_previous = false; $this->need_first = false; } $this->base_url = rtrim($this->base_url,'/').'/'; $output = ''; if($full_length < $this->total_pages){ if( $this->need_first === true && $this->cur_page > ($this->num_links +1)){ $output.= $this->first_tag_open. '<a href="' .$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close; } else{ $output.= $this->first_tag_open_faded.'<a>' .$this->first_link.'</a>'.$this->first_tag_close; } if($this->need_previous === true && $this->cur_page > 1){ $output.= $this->previous_tag_open.'<a href="' .$this->base_url.($this->cur_page -1) .'">' . $this->previous_link.'</a>'. $this->previous_tag_close; } else{ $output.= $this->previous_tag_open_faded.'<a>' . $this->previous_link.'</a>'. $this->previous_tag_close; } } for($i = $start; $i <= $end; $i++){ if( $this->cur_page == $i){ $output .= $this->cur_tag_open . $i . $this->cur_tag_close; } else{ $output .= $this->num_tag_open . '<a href="' . $this->base_url.$i .'">' . $i . '</a>'. $this->num_tag_close; } } if($full_length < $this->total_pages){ if($this->need_next === true && $this->cur_page < $this->total_pages){ $output .= $this->next_tag_open . '<a href="'. $this->base_url .($this->cur_page + 1).'">'. $this->next_link .'</a>' . $this->next_tag_close; } else{ $output .= $this->next_tag_open_faded . '<a>' . $this->next_link .'</a>' . $this->next_tag_close; } if($this->need_last === true && ($this->cur_page + $this->num_links) < $this->total_pages){ $output .= $this->last_tag_open . '<a href ="' . $this->base_url . $this->total_pages .'">' . $this->last_link . '<span>( ' .$this->total_pages.' )</span></a>'. $this->last_tag_close; } else{ $output .= $this->last_tag_open_faded . '<a>'. $this->last_link . '<span>( ' .$this->total_pages.' )</span></a>'. $this->last_tag_close; } } $output = rtrim($output,'/'); $output = $this->full_tag_open .$output .$this->full_tag_close; $this->output = $this->navi_wrapper_tag_open .$output.$this->navi_wrapper_tag_close; } function get_output(){ return $this->output;} function get_limit(){ $limit = ($this->cur_page - 1) * $this->per_page; return $limit; } } ?>