Dysan Posted February 27, 2008 Share Posted February 27, 2008 Hi, The following code creates a simple pagination page, that displays a "NEXT " and "PREVIOUS" button in order to navigate between pages. How do I add page number links, to enable the user to click a specific page number e.g. 3 in order to go directly to page 3? <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die(mysql_error()); } mysql_select_db("db", $con); $perpage = 2; $start = (isset($_GET['id'])) ? $_GET['id'] : 0; $TotalRec = mysql_result(mysql_query("SELECT COUNT(*) FROM persons"), 0); $select = "SELECT * FROM persons LIMIT $start, $perpage"; $result = mysql_query($select) or die(mysql_error()); # Dislay your rows here in the loop while($row = mysql_fetch_array($result)) { echo $row['name']." "; echo $row['age']." "; echo $row['sex'].'<br />'; } if($start == 0) { echo "PREVIOUS"; } else { echo '<a href="pagination.php?id=' . ($start - $perpage) . '">'."PREVIOUS".'</a>'; } $page = ($_GET['id'] / $perpage) + 1; $total = ceil($TotalRec / $perpage); if($start + $perpage >= $TotalRec) { echo "NEXT"; } else { echo '<a href="pagination.php?id=' . ($start + $perpage) . '">'."NEXT".'</a>'; } ?> Link to comment https://forums.phpfreaks.com/topic/93344-pagination-page-numbers/ Share on other sites More sharing options...
amites Posted February 27, 2008 Share Posted February 27, 2008 something along the lines of $pages = $TotalRec / $perpage; for ($i = 1; $i <= $pages; $i++) { echo '<a href="LINK TO PAGE?pg=' . $i . '">'.$i.'</a>'; } with a condition at the beginning of the page where if (isset($_GET['pg'])) { $start = intval($_GET['pg']) * $perpage; } rough example but it's a good starting point Link to comment https://forums.phpfreaks.com/topic/93344-pagination-page-numbers/#findComment-478255 Share on other sites More sharing options...
sasa Posted February 27, 2008 Share Posted February 27, 2008 try ... $page = ($_GET['id'] / $perpage);// + 1; $total = ceil($TotalRec / $perpage); for($i=0; $i<$total;){ if($i==$page) echo " ",++$i; else echo '<a href="pagination.php?id=' , ($i * $perpage) , '"> ',++$i,' </a>'; } if($start + $perpage >= $TotalRec) { echo "NEXT"; } ... Link to comment https://forums.phpfreaks.com/topic/93344-pagination-page-numbers/#findComment-478308 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.