ale nie wiem jak zrobić limit na np 10
Ť Previous 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Next ť
Jeżeli ktoś może mi pomóc to będę wdzięczny:)
1. plik index.php
<?php // Find all photos $photos = Photograph::find_all(); ?> <?php // 1. the current page number ($current_page) // 2. records per page ($per_page) $per_page = 2; // 3. total record count ($total_count) $total_count = Photograph::count_all(); // Find all photos //$photos = Photograph::find_all(); $pagination = new Pagination($page, $per_page, $total_count); // Instead of finding all records, just find the records // for this page $sql = "SELECT * FROM photographs "; $sql .= "LIMIT {$per_page} "; $sql .= "OFFSET {$pagination->offset()}"; $photos = Photograph::find_by_sql($sql); ) ?> -------------- nawigacja <?php if($pagination->total_pages() > 1) { if($pagination->has_previous_page()) { } for($i=1; $i <= $pagination->total_pages(); $i++) { if($i == $page) { } else { } } if($pagination->has_next_page()) { } } ?>
klasa - pagination.php
<?php // This is a helper class to make paginating // records easy. class Pagination { 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() { } public function previous_page() { return $this->current_page - 1; } public function next_page() { 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; } } ?>
dodatkowa klasa
$sql = "SELECT COUNT(*) FROM ".self::$table_name; $result_set = $database->query($sql); $row = $database->fetch_array($result_set); }