Skip to content

Instantly share code, notes, and snippets.

@whalesalad
Created February 12, 2010 05:13
Show Gist options
  • Select an option

  • Save whalesalad/302325 to your computer and use it in GitHub Desktop.

Select an option

Save whalesalad/302325 to your computer and use it in GitHub Desktop.
<?php
/**
* My simple paginator.
*/
class Paginator {
private $total_items;
private $total_pages;
private $paginate_by;
private $current_page;
function __construct($total_items, $paginate_by, $current_page = 1) {
$this->current_page = $current_page;
$this->total_items = $total_items;
$this->paginate_by = $paginate_by;
$this->total_pages = ceil($total_items / $paginate_by);
$this->pages = array();
for ($i=0; $i < $this->total_pages; $i++) {
$this->pages[$i+1] = $i * $this->paginate_by;
}
// Template::debug($this);
}
// Sets the new page and returns the offset
public function set_page($page) {
$this->current_page = $page;
return $this->pages[$page];
}
public function get_offset($page = false) {
return ($page) ? $this->pages[$page] : $this->pages[$this->current_page];
}
public function prev_page() {
// If this is not the first page return the page number of the previous page
if ($this->current_page == 0) return false;
return $this->current_page - 1;
}
public function next_page() {
// If this is not the last page return the page number of the next page
if ($this->current_page == $this->total_pages) return false;
return $this->current_page + 1;
}
public function prev_page_link($wrl) {
return Template::lang_link(sprintf('/products/browse/%s/', $wrl->build('p', $this->prev_page())));
}
public function next_page_link($wrl) {
return Template::lang_link(sprintf('/products/browse/%s/', $wrl->build('p', $this->next_page())));
}
}
<ul class="productGrid">
<?php foreach ($products as $key => $product): ?>
<li id="product-<?= $product->ID.'-'.$product->model_number ?>" class="product">
<a href="<?= $product->get_absolute_url() ?>">
<img class="productImg" src="<?= $product->get_photo('small') ?>" alt="<?= $product->get_full_name() ?>" />
<span class="productName rounded">
<?= Template::rounded_filler() ?>
<?= $product->get_full_name() ?>
</span>
</a>
</li>
<?php endforeach ?>
</ul>
<?php if ($pagination->prev_page() or $pagination->next_page()): ?>
<div class="pagination">
<?php if ($pagination->prev_page()): ?><a href="<?= $pagination->prev_page_link($wrl) ?>">&larr; Previous</a><?php endif; ?>
<?php if ($pagination->next_page()): ?><a href="<?= $pagination->next_page_link($wrl) ?>">Next &rarr;</a><?php endif; ?>
</div>
<?php endif ?>
<?php
/// ~~~~~ snip snip snip
if ($query) {
$product_count = $outlet->from('Product')->where(implode($query, ' AND '))->count();
$pagination = new Paginator($product_count, 9, $page_num);
$products = $outlet->from('Product')->where(implode($query, ' AND '))->limit(9)->offset($pagination->get_offset())->find();
} else {
$product_count = $outlet->selectCount('Product');
$pagination = new Paginator($product_count, 9, $page_num);
$products = $outlet->select('Product', sprintf('LIMIT 10 OFFSET %s', $offset));
}
// Template::debug($pagination->get_offset($page_num));
// [RENDER]
include ARB_TEMPLATES.'product_browse.php';
/// ~~~~~~ snip snip snip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment