Jump to content

Pagination Help please...


Andrew7v

Recommended Posts

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
?>


Link to comment
Share on other sites

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 by akphidelt2007
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.