nealios Posted February 19, 2008 Share Posted February 19, 2008 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 More sharing options...
suttercain Posted February 19, 2008 Share Posted February 19, 2008 Can I see the URL? Link to comment https://forums.phpfreaks.com/topic/91924-pagination-question/#findComment-470720 Share on other sites More sharing options...
almystersv Posted February 19, 2008 Share Posted February 19, 2008 There are easier ways of doing pagination. Are you fairly new to php?! ... an amateur perhaps? Link to comment https://forums.phpfreaks.com/topic/91924-pagination-question/#findComment-470721 Share on other sites More sharing options...
duclet Posted February 19, 2008 Share Posted February 19, 2008 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 More sharing options...
nealios Posted February 19, 2008 Author Share Posted February 19, 2008 Thanks Almyster for pointing out that im an amateur The URL is http://localhost:8888/customers.php Once the next is clicked it goes up 5 as thats the number of records i want per page. http://localhost:8888/customers.php?startrow=5 Link to comment https://forums.phpfreaks.com/topic/91924-pagination-question/#findComment-470747 Share on other sites More sharing options...
duclet Posted February 19, 2008 Share Posted February 19, 2008 Don't post localhost links here. We can't access your localhost. Link to comment https://forums.phpfreaks.com/topic/91924-pagination-question/#findComment-470752 Share on other sites More sharing options...
nealios Posted February 19, 2008 Author Share Posted February 19, 2008 Sorry i thought suttercain just wanted to see the variable in the URL, its not on the internet only local. i will have to try your method duclet Link to comment https://forums.phpfreaks.com/topic/91924-pagination-question/#findComment-470756 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.