سلام.
کلاسی برای صفحه بندی آماده کردم.ممنون میشم نظرتونو بگید.
کلاسو میذارم و فایلو هم ضمیمه میکنم(همراه با یه استایل) با یه نمونه مثال که البته بستگی داره شما چطور از کلاس و آیتم هاش بخواین استفاده کنید.ابتکار عمل دست خودتونه.
/**
* @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 .'">نخست</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 .'">بعدی</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 .'">قبلی</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 .'">آخرین</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;
}
}
=================================
یه نمونه مثال:
$paging = new Pagination(
array(
'items_per_page' => 5,
'total_records' => $result['total'],
'url_address' => 'http://localhost/pagination/?page=',
'current_page' => (isset($_GET['page']) ? $_GET['page'] : 1),
//'mode' => true,
)
);
$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>';
}
ترتیب ایندکس ها اهمیتی نداره.
متد limit ابتدا و انتهای یک سلکتو برمیگردونه.
ایندکس mode اگر مقدار دهی نشه بطور پیشفرض false هست که یک نوع نمایش داره و اگر بغیر از false مقدار دهی بشه نمایشش فرق میکنه و دکمه هایی مثل قبلی و بعدی و ... حذف میشن.
در ادامه صفحه بندی رو هر کجا خواستید نمایش بدید از متد display استفاده کنید:
$paging->display();
در ادامه میتونید اطلاعاتی از صفحه هم نمایش بدید. مثلا در چه صفحه ای هستید و اطلاعاتی در مورد نمایش پستها:
$paging->recordsInfo();
echo '<hr>';
$paging->pagesInfo();
کلاس اعتبار سنجی های لازم برای شماره صفحه و نمایش انجام میده و فقط کافیه زمان ساخت شی ء ورودی های مورد نیازو وارد کنید => (total_records, url_address, current_page)
نمایش دمو
یا علی مدد