2010-07-21 43 views
0

由于某些原因,我的搜索脚本将只显示第一页的第一组结果,但当我点击分页链接到下一页时,搜索脚本生成的结果将会不会显示如何解决此问题?PHP和MySQL - 分页显示问题

这是我的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 & 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'; 
     } 
    } 
} 

回答

0

这是一个很大的代码,翻阅,但我可以大胆地猜测,当你点击一个分页链接,从而造成代码的整个第二块不再运行$_POST['search']没有设置?

+0

(如果是的话,你会想在生成页面链接时传递查询字符串中的搜索参数,然后使用该参数代替帖子值。) – user11977 2010-07-21 10:06:43

+0

你究竟是什么意思,你可以举个例子,原谅我的无知。 – lone 2010-07-21 10:07:09

+0

@smerriman如果你提交了一个表单发布到你的结果页面,但是只要你点击一个像search.php这样的链接,那么我将不得不从URL中获取值 – lone 2010-07-21 10:08:22

0

使用会话。下面我向你展示一个如何去做的例子。

$number = $_POST['studentid']; 


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

//take a look 

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

把下面的只是脚本导航之前(下一页/上一页)

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