我最近在做关于分页的项目,但问题是当我点击“搜索”按钮时,第一页中的数据会出现,但第二页是空白的,当我回到第一页时,页面完全是空白的。这是代码。谢谢!
<form method="post">
<div><table>
<td><b>Select Section:</b></td>
<td><select name="level">
<option value="Nursery">Nursery</option>
<option value="K-1">K-1</option>
<option value="K-2">K-2</option>
<option value="Grade 1">Grade 1</option>
<option value="Grade 2">Grade 2</option>
<option value="Grade 3">Grade 3</option>
<option value="Grade 4">Grade 4</option>
<option value="Grade 5">Grade 5</option>
<option value="Grade 6">Grade 6</option>
<option value="please_select" selected="selected" id="ps">Please Select</option>
</select></td>
<td><input type='image' name='submit' src='images/search1.jpg' id='img11' value='Search'/></td>
<span class='label'> </span>
</tr>
</table></div>
<CENTER><table class="table1" border = "1" cellspacing="0" cellpadding="9">
<tr class="tr1">
<th class="th1">Student Name</th>
<th class="th1">Year Level</th>
</tr>
<?php
if (!isset($_POST['level']))
{
$_POST['level'] = "undefine";
}
$level = $_POST['level'];
@$page = (int) $_GET['page'];
if ($page < 1) $page = 1;
$numPages = 6;
$resultsPerPage = 5;
$startResults = ($page - 1) * $resultsPerPage;
$numberOfRows = mysql_num_rows(mysql_query('SELECT * FROM students'))or die(mysql_error());
$totalPages = ceil($numberOfRows / $resultsPerPage);
$id=1;
$sql="SELECT * FROM students where level = '$level' LIMIT $startResults, $resultsPerPage" or die(mysql_error());
$result=mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
if($level == ""){
echo "<script>alert(\"Please Enter Year Level!\");</script>";
return 'student_list';
}
else{
echo "<tr class='tr1'>";
echo "<center>";
echo "<td class='1'>" . $row['surname'] .", " .$row['firstname']." ". $row['middlename']. "</td>";
echo "<td class='1'>". $row['level'] . "</td>";
echo "</center>";
echo "</tr>";
}
}
$id++;
$boundaryPages = floor($numPages / 2);
$range = array('start' => 1, 'end' => $totalPages);
$even = ($numPages % 2 == 0);
$theEnd = $totalPages - $boundaryPages;
if($even) $theEnd++;
if($totalPages > $numPages)
{
if($page <= $boundaryPages)
$range['end'] = $numPages;
elseif ($page >= $theEnd)
$range['start'] = $totalPages - $numPages + 1;
else
{
$range['start'] = $page - $boundaryPages;
$range['end'] = $page + $boundaryPages;
if($even) $range['end']--;
}
}
if($page > 1)
echo '<a href="?page='.($page - 1).'">«</a> ';
for ($i = $range['start']; $i <= $range['end']; $i++)
{
if($i == $page)
echo '<strong>'.$i.'</strong> ';
else
echo '<a href="?page='.$i.'">'.$i.'</a> ';
}
if ($page < $totalPages)
echo '<a href="?page='.($page + 1).'">»</a> ';
答案 0 :(得分:0)
如果您想要随身携带有关搜索参数的数据,您需要将其添加到您的分页链接,并使用$_GET
访问它;或使用sessions。否则,您的应用程序将失去对以下页面中$_POST
数据的所有了解。