0

我正在尝试在我的代码中实现分页。它不能很好地工作:

1.我想每页显示 5 个结果,但它在第一页上显示了所有内容。

2.当我点击页码时,出现错误:未定义索引:搜索。

非常感谢您的帮助!

首先我搜索一个词,

<form method="post" action="AF12P.php">
    <input type="submit" name="submit" value="search">
    <input type="text" name="search" />
</form>

这是AF12P.php

<div id="wrapper">

    <div id="comments">

        <?php
        include('includes/db_AF.php'); //includes the db credentials
        include ('paginate.php'); //include paginate page
        $per_page = 5;         // number of results to show per page

        $connection = @new mysqli(HOSTNAME, MYSQLUSER, MYSQLPASS, MYSQLDB);
        if ($connection->connect_error) {
         die('Connect Error: ' . $connection->connect_error);
        }
        else {

        $search=$_POST["search"];


        $query="SELECT *, comment FROM comments AS c
        JOIN wordtable2 AS w
        ON c.w1 = w.w1
        WHERE word like '%$search%' 
        ORDER BY word DESC";                         // Run the query

        $resultx = mysqli_query($connection, $query);
        $total_results = mysqli_num_rows($resultx);
        $total_pages = ceil($total_results / $per_page);//total pages 


        $result_obj = '';
        $result_obj = $connection->query($query);            



if (isset($_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;
    $show_page=0;
}
// display pagination
if(isset($_GET['page'])){
    $page = intval($_GET['page']);
}else{
     $page =0;
}

$tpages=$total_pages;
if ($page <= 0)
    $page = 1;



        while($result = $result_obj->fetch_array(MYSQLI_ASSOC)) {   
        $items[] = $result;
        }                               


        $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
            //echo '<div class="pagination"><ul>';
            if ($total_pages > 1) {
                        echo paginate($reload, $show_page, $total_pages);
                    }

        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;
                        }
            }
        foreach ($items as $item) {



            echo('<a href="AF11G.php?search='.$item['word'].'">'.$item['word'].'</a>');

            echo '<br />';
            echo $item['comment'];                  
            echo '<br />';



        }

        }
        ?>

    </div>

</div>

这是 paginate.php {来源:http ://www.techumber.com/2012/08/simple-pagination-with-php-mysql.html }

function paginate($reload, $page, $tpages) {
    $adjacents = 2;
    $prevlabel = "&lsaquo; Prev";
    $nextlabel = "Next &rsaquo;";
    $out = "";
    // previous
    if ($page == 1) {
        $out.= "<span>" . $prevlabel . "</span>\n";
    } elseif ($page == 2) {
        $out.= "<li><a  href=\"" . $reload . "\">" . $prevlabel . "</a>\n</li>";
    } else {
        $out.= "<li><a  href=\"" . $reload . "&amp;page=" . ($page - 1) . "\">" . $prevlabel . "</a>\n</li>";
    }

    $pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
    $pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
    for ($i = $pmin; $i <= $pmax; $i++) {
        if ($i == $page) {
            $out.= "<li  class=\"active\"><a href=''>" . $i . "</a></li>\n";
        } elseif ($i == 1) {
            $out.= "<li><a  href=\"" . $reload . "\">" . $i . "</a>\n</li>";
        } else {
            $out.= "<li><a  href=\"" . $reload . "&amp;page=" . $i . "\">" . $i . "</a>\n</li>";
        }
    }

    if ($page < ($tpages - $adjacents)) {
        $out.= "<a style='font-size:11px' href=\"" . $reload . "&amp;page=" . $tpages . "\">" . $tpages . "</a>\n";
    }
    // next
    if ($page < $tpages) {
        $out.= "<li><a  href=\"" . $reload . "&amp;page=" . ($page + 1) . "\">" . $nextlabel . "</a>\n</li>";
    } else {
        $out.= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
    }
    $out.= "";
    return $out;
}
4

3 回答 3

0

我建议您在 mysql 查询中使用LIMIT。因此,在点击页面后,它将被加载而没有不必要的行。我的意思是改变你的查询是这样的:

$query="SELECT *, comment FROM comments AS c
        JOIN wordtable2 AS w
        ON c.w1 = w.w1
        WHERE word like '%$search%' 
        ORDER BY word DESC
        LIMIT $start_element,$page_limit";

$page_limit = 每页的项目数。

$start_element = 查询中的项目数,您从中查询将受到限制。

于 2013-10-29T11:36:02.900 回答
0

POST应该是GET因为你正在search通过url,这就是你得到未定义索引的原因,尝试使用GET搜索方法

改变

  $search=$_POST["search"];

  $search=$_GET["search"];
于 2013-10-29T11:25:20.300 回答
0

在您的搜索中,您使用 $_POST 发布值。改用 $_GET 并将所有值与下一个或上一个链接连接起来。这应该可以解决问题

<form method="get" action="AF12P.php">
<input type="submit" name="submit" value="search">
<input type="text" name="search" />
 </form>

并在 php 页面上使用 $_GET 而不是 $_POST

于 2013-10-29T11:26:39.077 回答