1

我最近在做关于分页的项目,但问题是当我点击“搜索”按钮时,第一页中的数据会出现,但第二页是空白的,当我回到第一页时,页面完全空白. 这是代码。谢谢!

<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'>&nbsp;</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).'">&laquo;</a>&nbsp';

        for ($i = $range['start']; $i <= $range['end']; $i++)
        {
            if($i == $page)
                echo '<strong>'.$i.'</strong>&nbsp;';
            else
                echo '<a href="?page='.$i.'">'.$i.'</a>&nbsp;';
        }
    if ($page < $totalPages)
        echo '<a href="?page='.($page + 1).'">&raquo;</a>&nbsp;';
4

1 回答 1

0

如果你想携带有关搜索参数的数据,你想将它添加到你的分页链接中,并使用$_GET;访问它。或使用会话。否则,您的应用程序将丢失$_POST以下页面上的所有数据知识。

于 2013-02-12T03:38:41.323 回答