Dysan Posted December 25, 2007 Share Posted December 25, 2007 I am currently developing a pagination script. The following code is what I have so far, but for some reason, the code in bold throws up errors. Why is this? <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db", $con); $perpage = 2; $start = (isset($_GET['id'])) ? $_GET['id'] : 0; $TotalRec = mysql_result(mysql_query("SELECT COUNT(*) FROM person"), 0); $select = "SELECT * FROM person 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=\"./display.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=\"./display.php?id="' . ($start + $perpage) . '"\">NEXT</a>'; } echo '</div>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83142-pagination-script/ Share on other sites More sharing options...
Dane Posted December 25, 2007 Share Posted December 25, 2007 echo '<a href=\"display.php?id='.$start - $perpage.'">Previous</a>'; echo '<a href=\"display.php?id='.$start + $perpage.'">Next</a>'; Try those. Quote Link to comment https://forums.phpfreaks.com/topic/83142-pagination-script/#findComment-422893 Share on other sites More sharing options...
Dysan Posted December 25, 2007 Author Share Posted December 25, 2007 It doesn't quite work, the link displayed, is ">Next, and should be just Next. What needs to be changed? Quote Link to comment https://forums.phpfreaks.com/topic/83142-pagination-script/#findComment-422895 Share on other sites More sharing options...
Dysan Posted December 25, 2007 Author Share Posted December 25, 2007 OK, I now have the following code, that limits the records displayed to only 5 per page. How do I add page number links to the script, so a user can go to a particular page directly? e.g. PREVIOUS |1|2|3|4|5|6|7|8|9| NEXT <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("wb", $con); $perpage = 5; $start = (isset($_GET['id'])) ? $_GET['id'] : 0; $TotalRec = mysql_result(mysql_query("SELECT COUNT(*) FROM person"), 0); $select = "SELECT * FROM person 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="display.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="display.php?id=' . ($start + $perpage) . '">'."NEXT".'</a>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/83142-pagination-script/#findComment-422904 Share on other sites More sharing options...
Dane Posted December 25, 2007 Share Posted December 25, 2007 Just echo $page. Quote Link to comment https://forums.phpfreaks.com/topic/83142-pagination-script/#findComment-422923 Share on other sites More sharing options...
Dysan Posted December 25, 2007 Author Share Posted December 25, 2007 how do I display all the page numbers, and as links, using the $page variable? Quote Link to comment https://forums.phpfreaks.com/topic/83142-pagination-script/#findComment-422926 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.