بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
صفحه بندی سایت (paging)
کلاسی رو قبلا آماده کردم که براحتی با هر الگوی آدرسی میتونید سایتو صفحه بندی کنید.
<?php /** * @author Mohsen Movahed <l3iidak@yahoo.com> * @copyright 2014 Mohsen Movahed * @date 19 May 2014 - 1393/2/28 * @version 1.0 * @license GPL * @link http://www.bidakplus.ir */ class Pagination { private $items; private $output; /** * construct method */ public function __construct($params = null) { $this->items = array( 'items_per_page' => 5, // Records per page to display 'total_records' => 0, // Total records in database 'total_pages' => 0, // Total number of pages 'url_address' => '', // For example: http://www.mysite.com/?page= 'concat_to_url' => '', // This comes after page value - [optional] 'current_page' => 1, // Number of current page 'page_num_per_section' => 5, // Total number of pages to display - for example CurrentPage=16 :=> [first][next]...,14,15,[16],17,18,...[prev][last] 'mode' => false, // Mode = true or anything => Display:: 1,...,14,15,[16],17,18,...,20 ); // set values if (isset($params) && count($params) > 0) { if (is_array($params)) { foreach ($params as $key => $value) { if (!empty($value)) { $this->$key = $value; } } } } // run paginate method $this->paginate(); } /** * get values * @param string $key Must be index of items array * @return mixed|boolean if there is $key, returns array value otherwise returns false. */ public function __get($key) { if (isset($this->items[$key])) { return $this->items[$key]; } return false; } /** * set values * @param string $key Index of items array * @param mixed $value a value for set */ public function __set($key, $value) { if (isset($this->items[$key])) { $this->items[$key] = $value; } } /** * get total pages * @return integer Return the total pages */ private function getTotalPages() { $this->items_per_page = ($this->items_per_page <= 0 ? 1 : $this->items_per_page); $total = ceil($this->total_records / $this->items_per_page); if ($total <= 0) { $total = abs($total) + 1; } return $total; } /** * this manages to display pagination */ private function paginate() { $this->total_pages = $this->getTotalPages(); // set total pages $check = $this->checkItems(); // check item values and page number if ($check) { // start of section $start = $this->current_page - floor($this->page_num_per_section / 2); // maximum start $max = $this->total_pages - floor($this->page_num_per_section / 2); if($start <= 0 || $start > $max) { if ($start > $max) $this->current_page = 1; $start = 1; } // end of section $end = $start + $this->page_num_per_section - 1; if($end > $this->total_pages) { $end = $this->total_pages; } $this->output .= '<ul class="paging">' . PHP_EOL; // print first page button if ($this->mode == false && $this->current_page != 1) { $this->output .= '<li><a href="'. $this->url_address . 1 . $this->concat_to_url .'">First</a></li>' . PHP_EOL; } // print next page button if ($this->current_page < $this->total_pages && $this->mode == false) { $this->output .= '<li><a href="'. $this->url_address . $this->nextPage() . $this->concat_to_url .'">Next</a></li>' . PHP_EOL; } // print page number for ($i = $start; $i <= $end ; $i++) { // print dots in right if ($i == $start && $start > 1 && $i != 1) { // print page one if ($this->mode) { $this->output .= '<li><a href="'. $this->url_address . 1 . $this->concat_to_url .'">1</a></li>' . PHP_EOL; } $this->output .= '<li class="dot-paginator">...</li>' . PHP_EOL; } // print pages number $this->output .= '<li><a class="'. ($i == $this->current_page ? 'current-page' : '') .'" href="'. $this->url_address . $i .'">'. $i .'</a></li>' . PHP_EOL; // print dots in left if ($i <= $this->total_pages && $i == $end && $i != $this->total_pages) { $this->output .= '<li class="dot-paginator">...</li>' . PHP_EOL; } } // print prev page button if ($this->current_page > 1 && $this->mode == false) { $this->output .= '<li><a href="'. $this->url_address . $this->prevPage() . $this->concat_to_url .'">Previous</a></li>' . PHP_EOL; } // print last page number if ($this->mode && $this->total_pages != $this->current_page && $this->total_pages != $end) { $this->output .= '<li><a href="'. $this->url_address . $this->total_pages . $this->concat_to_url .'">'. $this->total_pages .'</a></li>' . PHP_EOL; } // print last page button if ($this->mode == false && $this->current_page != $this->total_pages) { $this->output .= '<li><a href="'. $this->url_address . $this->total_pages . $this->concat_to_url .'">Last</a></li>' . PHP_EOL; } $this->output .= '</ul><br>' . PHP_EOL; } } /** * check item values * @return boolean The result true if item values is not empty, false otherwise */ private function checkItems() { foreach ($this->items as $key => $value) { if (empty($value)) { settype($key, 'string'); switch ($key) { case 'current_page': $this->$key = 1; // not required beacause by default equal to 1 break; case 'concat_to_url': break; case 'mode': break; default: return false; break; } } } if ($this->total_pages == 1) { return false; } $this->checkPageNumber(); return true; } /** * check page number */ private function checkPageNumber() { $this->current_page = intval($this->current_page); if ($this->current_page > $this->total_pages) { $this->current_page = $this->total_pages; } elseif ($this->current_page <= 0) { $abs = abs($this->current_page); $this->current_page = ($this->current_page < 0 ? $abs : $abs + 1); } } /** * previous page * @return integer */ private function prevPage() { return $this->current_page - 1; } /** * next page * @return integer */ private function nextPage() { return $this->current_page + 1; } /** * show part of the records => for example: 1 - 10 of 200 */ public function recordsInfo() { $var = $this->current_page * $this->items_per_page; $sectionEnd = $var; $sectionStart = $sectionEnd - $this->items_per_page + 1; $sectionEnd = ($var > $this->total_records ? $this->total_records : $var); echo 'Showing ' . $sectionStart . ' to ' . $sectionEnd . ' of ' . $this->total_records . ' entries'; } /** * show current page of all pages */ public function pagesInfo() { echo 'Page ' . $this->current_page . ' of ' . $this->total_pages; } /** * get query limit * @return array Return start and end section for query limit */ public function limit() { $start = $this->items_per_page * ($this->current_page - 1); $limit = $this->items_per_page; return array('start' => $start, 'limit' => $limit); } /** * show paging */ public function display() { echo $this->output; } }
کافیه تعداد رکورد جدولتونو بدست بیارید و کلاس pagination رو new کنید و آبجکتی بسازید.
یک نمونه مثال:
mysql_connect('localhost', 'root', ''); mysql_select_db('pagination'); mysql_query('set names \'utf8\''); $total = mysql_query('select count(*) as `total` from posts'); $result = mysql_fetch_assoc($total); $paging = new Pagination( array( 'items_per_page' => 5, 'total_records' => $result['total'], 'url_address' => 'http://example.com/?page=', 'current_page' => (isset($_GET['page']) ? $_GET['page'] : 1), //'mode' => true, ) ); //$start = $paging->items_per_page * ($paging->current_page - 1); //$limit = $paging->items_per_page; $section = $paging->limit(); $start = $section['start']; $limit = $section['limit']; $result = mysql_query("select * from posts LIMIT $start, $limit"); while($row = mysql_fetch_assoc($result)) { echo '<strong>' . $row['title'] . '</strong><hr>'; } // display $paging->display(); echo '<br>'; $paging->recordsInfo(); echo '<hr>'; $paging->pagesInfo();
زمان تست , یکبار با mode => true هم تست کنید. نمایش فرق میکنه.
با متد display صفحه بندی نمایش داده میشه.
میتونید از این لینک دمو رو ببینید.
از این لینک دانلود کنید.
اگر سؤالی بود بپرسین.
+1
0
ممنون ، خیلی هم عالی کار کرد ، ازش استفاده هم شد ...
(9 سال پیش)
برای این سوال پاسخی وجود ندارد.
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .