搜索结果的分页不起作用

时间:2013-10-31 08:19:36

标签: php pagination

搜索结果中的分页问题

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

由于我是这个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>"; 
?>

0 个答案:

没有答案