Give a good example!
- google -> pagination - Vitaly Kustov
|
2 answers
I once studied with this example: page navigation .
|
I will give the code of the function that I recently wrote for one Open Source project $ per_page - the number of entries per page. $ page - page number $ total_rows - total number of records
function paging(){ global $per_page,$page,$common_get,$calendar_url,$title,$total_rows; $per_page=(int)$per_page; $page=(int)$page; $count=$total_rows; if ($per_page==1){ $pages=1; } else { $pages = ceil($count/$per_page); } if ($page < 1) { $page = 1; } elseif ($page > $pages) { $page = $pages; } $neighbours = 2; $left_neighbour = $page - $neighbours; if ($left_neighbour < 1) $left_neighbour = 1; $right_neighbour = $page + $neighbours; if ($right_neighbour > $pages) $right_neighbour = $pages; echo '<div id="paging">'; if ($pages!=1){ echo ' <div id="pages"><ul>'; if ($page > 1) { print ' <li><a href="'.$calendar_url.'index.php?'.$common_get.'&page=1"><<</a></li><li><a href="'.$calendar_url.'index.php?'.$common_get.'&page=' . ($page-1) . '"><</a></li> '; } for ($i=$left_neighbour; $i<=$right_neighbour; $i++) { if ($i != $page) { print ' <li><a href="'.$calendar_url.'index.php?'.$common_get.'&page=' . $i . '">' . $i . '</a></li> '; } else { print ' <li><span>' . $i . '</span></li> '; } } if ($page < $pages) { print '<li><a href="'.$calendar_url.'index.php?'.$common_get.'&page=' . ($page+1) . '">></a></li><li><a href="'.$calendar_url.'index.php?'.$common_get.'&page=' . $pages . '">>></a></li>'; } echo '</ul></div>'; } echo ' <div class="events-for-page">'; echo '<p>Events per page:</p>'; echo '<form action="" method="post"> <input type="hidden" name="pagesh" value="1" />'; echo ' <select name="pages" id="pages">'; $selected=''; if ($per_page==1){$selected='selected="selected"';} else {$selected='';} echo '<option '.$selected.' value="1">All</option>'; if ($per_page==5){$selected='selected="selected"';} else {$selected='';} echo '<option '.$selected.' value="5">5</option>'; if ($per_page==10){$selected='selected="selected"';} else {$selected='';} echo '<option '.$selected.' value="10">10</option>'; if ($per_page==20){$selected='selected="selected"';} else {$selected='';} echo '<option '.$selected.' value="20">20</option>'; if ($per_page==50){$selected='selected="selected"';} else {$selected='';} echo '<option '.$selected.' value="50">50</option>'; echo '</select> <input type="submit" value="Go"/>'; echo ' </form> </div> <p>Total Events: '.$count.'</p> </div>'; }
In another script where the sql query is formed, you need to write the following: $ per_page - from the form with the paging () function, I save the value in the session. $ page = of $ _GET parameters.
$q='SELECT SQL_CALC_FOUND_ROWS * FROM `table`'; if ($per_page!=1){ $start = ($page-1)*$per_page; $limit=' LIMIT '.$start.','.$per_page; $q.=$limit; }
Find the total number of records will help SQL function - FOUND_ROWS ()
|