Post by suhanto » Wed Oct 07, 2009 9:27 am

I've seen that in pagination function, it is common to have URL like http://opencart.local/category?page=2. Without 'page' query string, implicitly we access the first page. So, it is not necessary to specify http://opencart.local/category?page=1, because it will produce duplicate links to the same contents.

I've changed the pagination function a little bit to change the default OpenCart behavior, so it won't mention ?page=1 anymore.

Here is the modification (/system/library/pagination.php):

Code: Select all

	public function render() {
		$total = $this->total;
		$page = $this->page;
		$limit = $this->limit;
		$num_links = $this->num_links;
		$num_pages = ceil($total / $limit);
		
		$output = '';
		
		if ($page > 1) {
			$output .= ' <a href="' . ereg_replace('[\?|&]page=%s', '', $this->url) . '">' . $this->text_first . '</a>';
			if ($page - 1 == 1)
				$output .= '<a href="' . ereg_replace('[\?|&]page=%s', '', $this->url) . '">' . $this->text_prev . '</a> ';
			else
				$output .= '<a href="' . sprintf($this->url, $page - 1) . '">' . $this->text_prev . '</a> ';
    	}

		if ($num_pages > 1) {
			if ($num_pages <= $num_links) {
				$start = 1;
				$end = $num_pages;
			} else {
				$start = $page - floor($num_links / 2);
				$end = $page + floor($num_links / 2);
			
				if ($start < 1) {
					$end += abs($start) + 1;
					$start = 1;
				}
						
				if ($end > $num_pages) {
					$start -= ($end - $num_pages);
					$end = $num_pages;
				}
			}

			if ($start > 1) {
				$output .= ' .... ';
			}

			for ($i = $start; $i <= $end; $i++) {
				if ($page == $i) {
					$output .= ' <b>' . $i . '</b> ';
				} else {
					if ($i == 1)
						$output .= ' <a href="' . ereg_replace('[\?|&]page=%s', '', $this->url) . '">' . $i . '</a> ';
					else
						$output .= ' <a href="' . sprintf($this->url, $i) . '">' . $i . '</a> ';
				}	
			}
							
			if ($end < $num_pages) {
				$output .= ' .... ';
			}
		}
		
   		if ($page < $num_pages) {
			$output .= ' <a href="' . sprintf($this->url, $page + 1) . '">' . $this->text_next . '</a> <a href="' . sprintf($this->url, $num_pages) . '">' . $this->text_last . '</a> ';
		}
				
		return ($output ? '<div class="' . $this->style_links . '">' . $output . '</div>' : '') . '<div class="' . $this->style_results . '">' . sprintf($this->text, ($total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($total - $limit)) ? $total : ((($page - 1) * $limit) + $limit), $total, $num_pages) . '</div>';
	}

http://suhanto.net


User avatar
New member

Posts

Joined
Sat Sep 19, 2009 2:57 pm
Location - Jakarta, Indonesia
Who is online

Users browsing this forum: No registered users and 22 guests