Here is my code:
// current page
$p = $_GET['page'];
// the number of all pages
$page_nums = 5;
// pagination should be created
if( $page_nums > 1 ) {
$data['pagination'] = '<div class="pagination_box">';
// backward btn
if ($p > 1) {
$data['pagination'] .= "<a class='pagination_backward' href=''>قبلی</a>";
$data['pagination'] .= '<span style="color:#848d95; margin:0px 10px;">…</span>';
}
$pagination_active = "pagination_active";
for($i = $p; $i <= $page_nums; $i++){
$data['pagination'] .= "<a class='$pagination_active' href=''>$i</a> ";
$pagination_active = '';
if ($i >= 2 && $i % 2) {
$data['pagination'] .= '<span style="color:#848d95; margin:0px 10px;">…</span>';
$data['pagination'] .= "<a href=''>$page_nums</a>";
break;
}
}
// forward btn
$data['pagination'] .= ($page_nums > $p) ? '<a class="pagination_backward" href="">بعدی</a>' : null;
The result of it doesn't look good all the times. Here are some examples:
$_GET['page'] = 0;
$_GET['page'] = 1;
$_GET['page'] = 4;
As you see, the first example looks good.
The second one is not bad (it would be better if the number of page 1
been visible instead of ...
)
The third one is awful. It's totally wrong.
I really don't know how can I fix the problem. Do you have any idea?
Noted: the language I use is right-to-left and قبلی
means prev
, and بعدی
means next
.