1

搜索结果的分页问题

我正在尝试对搜索结果进行分页,这是我用来从用户表单中查找搜索查询的代码。我正在对搜索结果进行分页,但是当我单击结果的第 2 页时,它显示发布的那些项目的未定义索引...虽然通过教程我看到使用 $_GET 而不是 $_POST ,但在这样做之后也没有改善结果

由于我是这个 php 代码的新手,你们能帮助或指导我正确的方向吗?////////////test.php////////////////

    mysqli_report(MYSQLI_REPORT_ERROR);


    // number of results to show per page
    $per_page = 2;

    // figure out the total pages in the database
    if ($result = $mysqli->query("SELECT * FROM bridegroom where Gender like '%$Name%' and Castest like'%$caste%' and Location like '%$location%' order by AGE"))
    {
        if ($result->num_rows != 0){
            $total_results = $result->num_rows;
            // ceil() returns the next highest    integer value by rounding up value if necessary
            $total_pages = ceil($total_results / $per_page); 
            if (isset($_GET['page']) && is_numeric($_GET['page']))
            {
                $show_page = $_GET['page'];
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                   $start = ($show_page -1) * $per_page;
                   $end = $start + $per_page; 
                }
                else
                {
                    $start = 0;
                    $end = $per_page; 
                }               
            }
            else
            {
                $start = 0;
                $end = $per_page; 
            }

            // display pagination
            echo "<p> <b>View search results:</b> ";
            for ($i = 1; $i <= $total_pages; $i++)
            {
                if (isset($_GET['page']) && $_GET['page'] == $i)
                {
                     echo $i . " ";
                }
                else
                {
                    echo "<a href='test.php?page=$i'>$i</a> ";
                }
            }
            echo "</p>";

            // display data in table

            // loop through results of database query, displaying them in the table 
            for ($i = $start; $i < $end; $i++)
            {
                // make sure that PHP doesn't try to show results that don't exist
                if ($i == $total_results) { break; }

                // find specific row
                $result->data_seek($i);
                $row = $result->fetch_row();
                // echo out the contents of each row into a table
                echo ("Name:$row[1]<br><span class=\"old\"> Age:$row[4]</span><br><span        class=\"sold\">Caste:$row[5]</span><br><span class=\"old\">Location:</span><span class=\"sold\">$row[6]</span><br><br>");

            }

            // close table>

        }         
        else
        {
            echo "No results to display!";
        } 
    }

    // error with the query
    else
    {
        echo "Error: " . $mysqli->error;
    }

    // close database connection
    $mysqli->close();

    // display pagination
    echo "<p> <b>Your search results:</b> ";
    for ($i = 1; $i <= $total_pages; $i++)
    {
        if (isset($_GET['page']) && $_GET['page'] == $i)
        {
            echo $i . " ";
        }
        else
        {
            echo "<a href='test.php?page=$i'>$i</a> ";
        }
    }

    echo "</p>"; 
?>

4

0 回答 0