mysql - How to paginate a form in php?

147

I have a problem. Last time, I made a grading system for teachers. I have a form for grading teachers but I am confused on how to paginate that form. If the entries are more than 10 then it causes problems.

<form action="n.php" method="post" enctype="multipart/form-data">
<table width="642" height="215" border="10" align="left" cellspacing="0" >
<tr>
  <th >Teacher ID</th>
  <th width="90" >Teacher Name</th>
  <th width="127" >Teacher Registration</th>
  <th width="135" >Teacher Qualification</th>
  <th width="92" >Teacher Subject</th>
  <th width="92" >Action</th>
</tr>
<?php
include 'conn.php';
$sql = "SELECT * FROM teacher ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
  while($row = $result->fetch_array()){
  $id=$row['tid'];
  ?>
  <tr>
    <td height="50" align="center" ><?php echo $row['tid'];?></td>
    <td align="center" ><?php echo $row['tname'];?></td>
    <td align="center" ><?php echo $row['treg'];?></td>
    <td align="center" ><?php echo $row['qualification'];?></td>
    <td align="center" ><?php echo $row['subject'];?></td>
     <td align="center"> <input type="text" name="rating[<?php echo $id; ?>]">
   </td>
  </tr>
  <?php
  }  
}else{
  echo "<center><p><font size=10/> No Records</p></center>";
}

$conn->close();
?><tr><td colspan="6">
<input type="submit" name="submit" value="Enter"></td></tr>
</table>
</form>
743

Answer

Solution:

In your PHP you could define a page size:

$page_size = 10; // number of teachers per page

And start at page 1:

$page = 1;

You change your SQL to use these values:

$sql = "SELECT * FROM teacher LIMIT " . $page_size * ($page - 1) . ", " . $page_size;

Now you'll only be shown 10 teachers from page 1.

You can change your script so that the page is read from the URL/yourscript.php?page=2:

$page = isset($_GET['page']) ? $_GET['page'] : 1; // take from query string or default to 1

Next step is to insert next and prev link in your html:

<?php 
$next = $page + 1;
$prev = $page - 1 > 0 ? $page - 1 : 1;
echo "<a href='?page=$prev'>prev page</a>";
echo "<a href='?page=$next'>next page</a>";

You can improve your scripts by checking how many pages there is using a count of teacher records

$pages = ceil(count($teachers) / $page_size);

I think you can take it from here.

UPDATE: I've merged my suggestions with your code:

<form action="n.php" method="post" enctype="multipart/form-data">
  <table width="642" height="215" border="10" align="left" cellspacing="0">

    ... your table stuff ...
<?php
include 'conn.php';

$page_size = 10;

// Get total pages
$sql = "SELECT COUNT(*) as cnt FROM teacher";
$result = $conn->query($sql);
$teacher_count = $result[0]["cnt"]; // I'm unsure how you DB class works;
$pages = ceil($teachers / $page_size);

$page = isset($_GET['page']) ? $_GET['page'] : 1; // take from query string or default to 1
$next = $page + 1 > $pages ? $pages : $page + 1;
$prev = $page - 1 > 0 ? $page - 1 : 1;

$sql = "SELECT * FROM teacher LIMIT " . $page_size * ($page - 1) . ", " . $page_size;
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  while($row = $result->fetch_array()){
  $id=$row['tid'];
  ?>
  <tr>
    <td height="50" align="center" ><?php echo $row['tid'];?></td>
    <td align="center" ><?php echo $row['tname'];?></td>
    <td align="center" ><?php echo $row['treg'];?></td>
    <td align="center" ><?php echo $row['qualification'];?></td>
    <td align="center" ><?php echo $row['subject'];?></td>
     <td align="center"> <input type="text" name="rating[<?php echo $id; ?>]">
   </td>
  </tr>
  <?php
  }  
}else{
  echo "<center><p><font size=10/> No Records</p></center>";
}

$conn->close();
?><tr><td colspan="6">
<input type="submit" name="submit" value="Enter"></td></tr>
</table>
<?php
if ($next > 1) {
  echo "<a href='?page=$prev'>prev page</a>";
}
if ($prev > 0 && $page !== 1) {
  echo "<a href='?page=$next'>next page</a>";
}
?>
</form>

People are also looking for solutions to the problem: run php-script with cron does not work

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.