如何在PHP中分页表单?

时间:2016-01-22 08:01:18

标签: php mysql

我有问题。上次,我为教师制作了评分系统。我有一个用于评分教师的表格,但我对如何对该表格进行分页感到困惑。如果条目超过10,则会导致问题。

<form action="n.php" method="post" enctype="multipart/form-data">
<table width="642" height="215" border="10" align="left" cellspacing="0"  >
<tr>
    <th class="style5">Teacher ID</th>
    <th width="90" class="style5">Teacher Name</th>
    <th width="127" class="style5">Teacher Registration</th>
    <th width="135" class="style5">Teacher Qualification</th>
    <th width="92" class="style5">Teacher Subject</th>
    <th width="92" class="style5">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" class="style5"><?php echo $row['tid'];?></td>
        <td align="center" class="style5"><?php echo $row['tname'];?></td>
        <td align="center" class="style5"><?php echo $row['treg'];?></td>
        <td align="center" class="style5"><?php echo $row['qualification'];?></td>
        <td align="center" class="style5"><?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>

1 个答案:

答案 0 :(得分:1)

在PHP中,您可以定义页面大小:

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

从第1页开始:

$page = 1;

您更改SQL以使用这些值:

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

现在,您只会在第1页看到10名教师。

您可以更改脚本,以便从网址/yourscript.php?page=2中读取页面:

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

下一步是在你的html中插入 next prev 链接:

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

您可以通过使用教师记录计数检查有多少页面来改进您的脚本

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

我想你可以从这里开始。

更新:我已将我的建议与您的代码合并:

<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" class="style5"><?php echo $row['tid'];?></td>
        <td align="center" class="style5"><?php echo $row['tname'];?></td>
        <td align="center" class="style5"><?php echo $row['treg'];?></td>
        <td align="center" class="style5"><?php echo $row['qualification'];?></td>
        <td align="center" class="style5"><?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>