PHP& MySQL - 分页显示问题

时间:2010-07-21 09:56:29

标签: php mysql search pagination

由于某种原因,我的搜索脚本只会显示第一页的第一组结果,但是当我点击下一页的分页链接时,搜索脚本生成的结果将不会显示如何更正这个问题?

这是我的PHP& MySQL分页代码。

$x = '';
$construct = '';

if(isset($_POST['search'])) {
    $search = $_POST['search'];
    if(strlen($search) <= 2){
        echo '';
    } else {

        $mysqli = mysqli_connect("localhost", "root", "", "sitename");
        mysqli_select_db($mysqli, "sitename");

            $search_explode = explode(" ", $search);

            foreach($search_explode as $search_each) {
                $x++;
                if($x == 1){
                    $construct .= " article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
                } else {
                    $construct .= " OR article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
                }
            }

            $construct = "SELECT users.*, users_articles.* FROM users_articles
                          INNER JOIN users ON users_articles.user_id = users.user_id
                          WHERE $construct";
            $run =  mysqli_query($mysqli, $construct);
            $search_term = mysqli_num_rows($run);
    }
}

// Number of records to show per page:
$display = 10;

// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined.

    $pages = mysqli_real_escape_string($mysqli, htmlentities(strip_tags($_GET['p'])));

} else { // Need to determine.

    // Count the number of records:
    $records = $search_term;

    // Calculate the number of pages...
    if ($records > $display) { // More than 1 page.
        $pages = ceil ($records/$display);
    } else {
        $pages = 1;
    }

} // End of p IF.

// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
    $start = mysqli_real_escape_string($mysqli, htmlentities(strip_tags($_GET['s'])));
} else {
    $start = 0;
}

// Make the links to other pages, if necessary.
if ($pages > 1) {

    // Add some spacing and start a paragraph:
    echo '<p>';

    // Determine what page the script is on:    
    $current_page = ($start/$display) + 1;

    //add this here... first will always be one
    if ($current_page != 1) {
        echo '<a href="search.php">First</a>';
    }

    // If it's not the first page, make a Previous button:
    if ($current_page != 1) {
        echo '<a href="search.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> ';
    }

      //create the links
      for ($i = max(1, $current_page - 3); $i <= min($current_page + 3, $pages); $i ++) {
            if ($i != $current_page) {
                echo '<a href="search.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
            } else {
                echo '<span>' . $i . '</span> ';
            }  
       }

    // If it's not the last page, make a Next button:
    if ($current_page != $pages) {
        echo '<a href="search.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>';
    }

    //add this here... Last will always be one
    if ($current_page != $pages) {
        echo '<a href="search.php?s=' . ($display * ($pages - 1)) . '&p=' . $pages . '">Last</a>';
    }

    echo '</p>'; // Close the paragraph.

} // End of links section.

以下是PHP&amp; MySQL搜索代码。

$x = '';
$construct = '';

if(isset($_POST['search'])) {
    $search = $_POST['search'];
    if(strlen($search) <= 2){
        echo 'Your search term is too short!';
    } else {

        $mysqli = mysqli_connect("localhost", "root", "", "sitename");
        mysqli_select_db($mysqli, "sitename");

            $search_explode = explode(" ", $search);

            foreach($search_explode as $search_each) {
                $x++;
                if($x == 1){
                    $construct .= " article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
                } else {
                    $construct .= " OR article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
                }
            }
            $construct = "SELECT users.*, users_articles.* FROM users_articles
                          INNER JOIN users ON users_articles.user_id = users.user_id
                          WHERE $construct";
            $construct .= " LIMIT $start, $display";

            $run =  mysqli_query($mysqli, $construct);
            $foundnum = mysqli_num_rows($run);

        if ($foundnum == 0) {
            echo 'Search term is too short!</p>No results found.';
        } else {
            echo 'results';
        }
    }
}

2 个答案:

答案 0 :(得分:0)

这是需要查看的大量代码,但是当我点击分页链接时,我可能会猜到$_POST['search']未设置,从而导致整个第二段代码无法运行?< / p>

答案 1 :(得分:0)

使用会话。下面我将向您展示如何操作的示例。

$number = $_POST['studentid'];


 if (empty($number)) {
   $number=$_SESSION['number']; 
}
    else{$number=$number ; )

//take a look

在页面顶部 - <? session_start(); ?>

将以下内容放在导航脚本(下一页/上一页)

之前
$_SESSION['number']=$number;
$_COOKIE['number'] = $number;