Jump to content

Recommended Posts

I'd like to limit the number of records displayed per page. With the following code I'm able to get all the records.

 

<?php
$query1 = "SELECT FirstName from registration";
$query2 = "SELECT LastName from registration";
$firstName = mysql_query($query1, $conn);
$lastName = mysql_query($query2, $conn);
$row_firstName = mysql_fetch_assoc($firstName);
$row_lastName = mysql_fetch_assoc($lastName);
<table border="1" bordercolor="#00CC33">
?>
            <?php do { ?>
            <tr><td>
            <?php echo $row_firstName['FirstName']; ?>
            <?php echo $row_lastName['LastName']; ?>
            </td></tr>
         <?php } while ($row_firstName = mysql_fetch_assoc($firstName)); ?> 
            </table>                 

 

Can someone tell me how I can limit the amount of displayed records, say maybe 10 per page? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/132140-how-to-limit-recordspage/
Share on other sites

By the way I just noticed, why are you doing two queries?

 

Just do it like this:

 

<?php
$query = mysql_query("SELECT FirstName,LastName from registration LIMIT 5", $conn);

$row = mysql_fetch_array($query);

$firstname = $row['FirstName']; 
$lastname = $row['LastName']; 
?>

Thank you all for the help. I failed to make clear one aspect of the limiting/displaying in my original question/post. Not only do I want to limit the records to a particular number but I want to have the option to cycle through the record set. For example, let's say I have 100 records. I'd like to display 10 of them at a time and then using "Previous" or "Next" move forward or back to the next 10 appropriate set of records. How can I do that?

 

Oh and as for the reason for two queries - query1 & query2, I did that because I was fiddling around with the code and trying a few different things. There was no purpose to it.

After doing some research on paging/pagination, I've put together some code. Unfortunately, the code doesn't work as intended. The code correctly shows "First," "Previous," "Next," "Last" - but instead of showing the assigned limit, shows all the records... By looking at my code can anyone let me know where I went wrong? Thanks.

 

<?php
$query = "SELECT FirstName, LastName FROM registration " . $limit;
$result = mysql_query($query, $conn);
$name = mysql_fetch_assoc($result);

if (isset($_GET['pageNum'])) {
   $pageNum = $_GET['pageNum'];
} else {
   $pageNum = 1;
} 

$resultRows = mysql_num_rows($result);
$rowsPerPage = 2;
$lastPage = ceil($resultRows/$rowsPerPage);

if ($pageNum > $lastPage) { $pageNum = $lastPage; } else if ($pageNum < 1) { $pageNum = 1; }

$limit = "LIMIT " . ($pageNum - 1) * $rowsPerPage . ", " . $rowsPerPage;

?>
<?php do { ?>
            <br />
               <?php echo $name['FirstName'] . " " . $name['LastName']; ?>
            <div class="borderLine"><p/></div>
            <?php } while ($name = mysql_fetch_assoc($result)); ?>
            
            <?php
            
if ($pageNum > 1) {
   $prevPage = $pageNum-1; 
   echo " <a href='{$_SERVER['PHP_SELF']}?pageNum=1'>FIRST</a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?pageNum=$prevPage'>PREV</a> ";
}

echo " ( Page $pageNum of $lastPage ) ";

if ($pageNum < $lastPage) {
   $nextPage = $pageNum+1; 
   echo " <a href='{$_SERVER['PHP_SELF']}?pageNum=$nextPage'>NEXT</a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?pageNum=$lastPage'>LAST</a> ";
}
?> 

Order of operations:

 

<?php
if (isset($_GET['pageNum'])) {
   $pageNum = $_GET['pageNum'];
} else {
   $pageNum = 1;
} 

$resultRows = mysql_num_rows($result);
$rowsPerPage = 2;
$lastPage = ceil($resultRows/$rowsPerPage);

if ($pageNum > $lastPage) { $pageNum = $lastPage; } else if ($pageNum < 1) { $pageNum = 1; }

$limit = "LIMIT " . ($pageNum - 1) * $rowsPerPage . ", " . $rowsPerPage;

$query = "SELECT FirstName, LastName FROM registration " . $limit;
$result = mysql_query($query, $conn);
$name = mysql_fetch_assoc($result);


?>
<?php do { ?>
            <br />
               <?php echo $name['FirstName'] . " " . $name['LastName']; ?>
            <div class="borderLine"><p/></div>
            <?php } while ($name = mysql_fetch_assoc($result)); ?>
            
            <?php
            
if ($pageNum > 1) {
   $prevPage = $pageNum-1; 
   echo " <a href='{$_SERVER['PHP_SELF']}?pageNum=1'>FIRST</a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?pageNum=$prevPage'>PREV</a> ";
}

echo " ( Page $pageNum of $lastPage ) ";

if ($pageNum < $lastPage) {
   $nextPage = $pageNum+1; 
   echo " <a href='{$_SERVER['PHP_SELF']}?pageNum=$nextPage'>NEXT</a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?pageNum=$lastPage'>LAST</a> ";
}
?> 

 

You cannot use limit if it has not been defined yet.

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.