Jump to content

Pagination Question


nealios

Recommended Posts

Hello

 

i have this simple method of pagination. It scrolls up and down through the records correctly however it doesnt know when to stop when it runs out of records.

 

for instance when its record 1 you press the previous button and it takes you to a blank screen rather than not letting you go any further.

 

Can anyone help me implement this?

 

current code

 

<?php

require "connect.php";
if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
  //we give the value of the starting row to 0 because nothing was found in URL
  $startrow = 0;
//otherwise we take the value from the URL
} else {
  $startrow = (int)$_GET['startrow'];
// echo ("this way");
}




$sql = "SELECT * FROM `customer` ORDER BY `surname` ASC limit $startrow, 5";
$result = mysql_query($sql, $connection)
	or die("unable to perform query<br>$sql");

?>
<p><p>
<table border="1" align="left">
<tr>
	<td><p class="red"><?=$_SESSION['username']?> logged in</p></td>
</tr>
</table><p>
<p><p>
<table align="center" cellpadding="0">
<tr>
	<td align="center">
	<h5>Click on the CID for any record you wish to Modify, Delete, or view Additional Info.</h5>
	<h5>Click on the Table Headers to sort by that field (currently only ascending).</h5>
	<h5>Click on a Surname to view all jobs by that customer.</h5></td>
</tr>

</table>

<table border="1" align="center" cellspacing="0" cellpadding="4">
  <tr>
    <th><a href="customers.php?sort=cid">CID</a></th>
    <th><a href="customers.php?sort=title">Title</a></th>
    <th><a href="customers.php?sort=first">FirstName</a></th>
    <th><a href="customers.php">Surname</a></th>
    <th><a href="customers.php?sort=add1">Address 1</a></th>
    <th><a href="customers.php?sort=add2">Address 2</a></th>
    <th><a href="customers.php?sort=town">Town</a></th>
    <th><a href="customers.php?sort=county">County</a></th>
    <th><a href="customers.php?sort=postcode">Postcode</a></th>
    <th><a href="customers.php?sort=tele">Telephone</a></th>
    <th><a href="customers.php?sort=mobile">Mobile</a></th>
    <th><a href="customers.php?sort=email">Email</a></th>
  </tr>


	<?php while($row= mysql_fetch_array($result))
	{ ?>
<tr>
	<td align="center"><a href="dbadmin.php?cid=<?= $row['cid'] ?>"><?=$row['cid']?></a></td>
	<td><?=$row['title']?></td> 
	<td><?=$row['first_name']?></td> 
	<td><a href="customerjob.php?cid=<?= $row['cid'] ?>"><?=$row['surname']?></td>
	<td><?=$row['address1']?></td>
	<td><?=$row['address2']?></td>
	<td><?=$row['town']?></td>
	<td><?=$row['county']?></td>
	<td><?=$row['postcode']?></td>
	<td><?=$row['telephone']?></td>
	<td><?=$row['mobile']?></td>
	<td><?=$row['email']?> </td>

</tr>


<?php }?>
</table>
<br />
<?php echo '
<table width=60% align=center>
<tr>
	<td align=left><a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow-5).'"><< Prev</a></td>
	<td align=right><a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+5).'">Next >></a></td>
</tr>                         
</table> '; ?>

 

thank you

Link to comment
https://forums.phpfreaks.com/topic/91924-pagination-question/
Share on other sites

To fix the previous problem, just add an extra check to your first if statement to have sure startrow is non-negative. To fix the next link problem though, it requires some more work. Best solution is probably to change your query so it retrieves all the rows and cache the result. Then use mysql_num_rows to see if startrow is within 5 of the result of mysql_num_rows. If it is, the next link shouldn't be there.

Link to comment
https://forums.phpfreaks.com/topic/91924-pagination-question/#findComment-470729
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.