Andrew7v Posted July 18, 2015 Share Posted July 18, 2015 Hello, I have a very basic SQL query, and i added some pagination code i found from a website cause i'm not good at that logic. It works fine but it doesn't have the code to highlight the current page link within the page numbers. So if on page 2 for example: 1 - [2] - 3 - 4 (page 2 would be highlighted (and non-hyper-linked). Can anyone please help me add the additional code required to do this?Thanks Here's the code... <?PHP include("includes/myconnection.php"); $num_rec_per_page=1; if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; $start_from = ($page-1) * $num_rec_per_page; $app="yes"; $sql = "SELECT * FROM listings WHERE approved='$app' order by auto_id desc LIMIT $start_from, $num_rec_per_page"; $rs_result = mysql_query ($sql); //run the query ?> <?php //The listing of results... while ($row = mysql_fetch_assoc($rs_result)) { ?> <tr> <td><?php echo $row['title']; ?><br /> <?php echo $row['descrip']; ?><br /><br /> </td> </tr> <?php }; ?> <?php //Bottom Pagination.... $sql = "SELECT * FROM listings"; $rs_result = mysql_query($sql); //run the query $total_records = mysql_num_rows($rs_result); //count number of records $total_pages = ceil($total_records / $num_rec_per_page); echo "<a href='pagination.php?page=1'>".'First'."</a> "; // Goto 1st page for ($i=1; $i<=$total_pages; $i++) { echo "<a href='pagination.php?page=".$i."'>".$i."</a> "; }; echo "<a href='pagination.php?page=$total_pages'>".'Last'."</a> "; // Goto last page ?> Quote Link to comment Share on other sites More sharing options...
akphidelt2007 Posted July 18, 2015 Share Posted July 18, 2015 (edited) You have to check to see if $i = the current page. Something like this... //for ($i=1; $i<=$total_pages; $i++) { echo "<a href='pagination.php?page=".$i."'>".$i."</a> "; }; //make sure the page variable is set than check to see if the current iteration equals the page. If so just echo out the page # for ($i=1; $i<=$total_pages; $i++) { echo (isset($_GET['page']) && $i==$_GET['page']) ? "<span style='background-color:red;'>$i</span>" : "<a href='pagination.php?page=".$i."'>".$i."</a> "; } Edited July 18, 2015 by akphidelt2007 Quote Link to comment Share on other sites More sharing options...
Andrew7v Posted July 19, 2015 Author Share Posted July 19, 2015 Ok, thanks for that. I'll work on it. Quote Link to comment 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.