php - loop with i++ and pagination -
i have :
$page = (int) (!isset($_get["page"]) ? 1 : $_get["page"]); $limit = 50; $startpoint = ($page * $limit) - $limit; //to make pagination $statement = "`users`"; <?php $sql = mysql_query("select * users order respect desc limit {$startpoint} , {$limit}"); $i = 1; while ($row = mysql_fetch_assoc($sql)) { echo ' <tr> <td style="text-align:center;"> '; if ($i == '1') { echo '<i class="fa fa-trophy"></i> '; } if ($i == '2') { echo '<i class="fa fa-trophy"></i> '; } if ($i == '3') { echo '<i class="fa fa-trophy"></i> '; } echo ' ' . $i++ . '
and function pagination :
function pagination($query, $per_page = 10,$page = 1, $url = '?') { $query = "select count(*) `num` {$query}"; $row = mysql_fetch_array(mysql_query($query)); $total = $row['num']; $adjacents = "2"; $page = ($page == 0 ? 1 : $page); $start = ($page - 1) * $per_page; $prev = $page - 1; $next = $page + 1; $lastpage = ceil($total/$per_page); $lpm1 = $lastpage - 1; $pagination = ""; if($lastpage > 1) { $pagination .= "<ul class='pagination'>"; $pagination .= "<li class='details'>page $page of $lastpage</li>"; if ($lastpage < 7 + ($adjacents * 2)) { ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<li><a class='current'>$counter</a></li>"; else $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>"; } } elseif($lastpage > 5 + ($adjacents * 2)) { if($page < 1 + ($adjacents * 2)) { ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) $pagination.= "<li><a class='current'>$counter</a></li>"; else $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>"; } $pagination.= "<li class='dot'>...</li>"; $pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>"; $pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>"; } elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination.= "<li><a href='{$url}page=1'>1</a></li>"; $pagination.= "<li><a href='{$url}page=2'>2</a></li>"; $pagination.= "<li class='dot'>...</li>"; ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) $pagination.= "<li><a class='current'>$counter</a></li>"; else $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>"; } $pagination.= "<li class='dot'>..</li>"; $pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>"; $pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>"; } else { $pagination.= "<li><a href='{$url}page=1'>1</a></li>"; $pagination.= "<li><a href='{$url}page=2'>2</a></li>"; $pagination.= "<li class='dot'>..</li>"; ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<li><a class='current'>$counter</a></li>"; else $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>"; } } } if ($page < $counter - 1){ $pagination.= "<li><a href='{$url}page=$next'>next</a></li>"; $pagination.= "<li><a href='{$url}page=$lastpage'>last</a></li>"; }else{ $pagination.= "<li><a class='current'>next</a></li>"; $pagination.= "<li><a class='current'>last</a></li>"; } $pagination.= "</ul>\n"; } return $pagination; }
the limit 50 on every page , on first page looks 1 50 , wen move page?2 same 1 50 , why not 51 101 on page3 102 152 ?
see it
select * tbl limit 5,10; # retrieve rows 6-15
to context, have change {$startpoint} value based on page,
select * users order respect desc limit {$startpoint} , {$limit}")
for example,
//determine page number $_get $page = 1; if(!empty($_get['page'])) { $page = filter_input(input_get, 'page', filter_validate_int); if(false === $page) { $page = 1; } } // set number of items display per page $items_per_page = 50; // build query $offset = ($page - 1) * $items_per_page; $sql = "select * users limit " . $offset . "," . $items_per_page;
Comments
Post a Comment