Created
February 12, 2010 05:13
-
-
Save whalesalad/302325 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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()))); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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) ?>">← Previous</a><?php endif; ?> | |
| <?php if ($pagination->next_page()): ?><a href="<?= $pagination->next_page_link($wrl) ?>">Next →</a><?php endif; ?> | |
| </div> | |
| <?php endif ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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