如何将搜索结果回显到另一个页面


how to echo out search results to another page

@Evan试图将0个结果回显到newpage.php上,我如何从newpage.php定义$searchQuery……似乎我必须将所有代码重写到newpagephp上……有没有办法只在另一个页面上形成操作?

    <?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once("db_connects.php");
$queryArray = array();
$goodQuery = true;
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
    $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
{
    $sqlCommand = "(SELECT id, links, page_body, page_title AS title FROM pages WHERE MATCH (page_title,page_body) AGAINST ('$searchquery'))";
} 
$query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
    $search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />";
    while($row = mysql_fetch_assoc($query)){
        $queryArray[] = $row;
    } 
}
else {
     $goodQuery = false;
     $_SESSION['error'] = true;
     header("Location: newpage.php");
}
if($goodQuery){
    $_SESSION['search_output'] = $queryArray;
    header("Location: newpage.php");
    exit;
}
else{
    echo $search_output;
}
}
  ?>

这是newpage.php上的代码。。

 <?php
session_start(); 
if(isset($_SESSION['error'])){
     $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
    } else {
    foreach($_SESSION['search_output'] as $value){
        $value['links'];
        $value['title'];
        $value['page_body'];

        $title = $value['title'];
        $link = $value['links'];
        $body = $value['page_body'];
        $search_output .= "<a href='".$link."'>".$title."</a> - $body<br>";
}
}
?>
<div>
<?php echo $search_output; ?>
</div>

您需要将查询结果存储在array中。完成后,可以在php中使用header将用户带到新页面。一旦用户进入新页面,您就可以echo返回查询结果(现在存储在$_SESSION中(:

首先,创建一个数组:

$queryArray = array();

其次,查询数据库并将每一行存储在我们刚刚创建的数组中

while($row = mysql_fetch_array($query)){
       $queryArray[] = $row;

        } // close while

第三,将数组移动到SESSION变量:

$_SESSION['search_output'] = $queryArray;

第四,将用户移动到新页面:

header("Location: newpage.php");

最后,回显您的数据:

foreach($_SESSION['search_output'] as $value){
    echo $value;
}

编辑:使用完整代码更新:

    <?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once("db_connects.php");
$queryArray = array();
$goodQuery = true;
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
    $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
    $sqlCommand = "(SELECT id, links, page_body, page_title AS title FROM pages WHERE MATCH      enter code here(page_title,page_body) AGAINST ('$searchquery'))";
} 
$query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
    $search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />";
    while($row = mysql_fetch_array($query)){
        $queryArray[] = $row;
    } 
}
else {
    $goodQuery = false;
    $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
}
if($goodQuery){
    $_SESSION['search_output'] = $queryArray;
    header("Location: newpage.php");
    exit;
}
else{
    echo $search_output;
}
  ?>

现在,在newpage.php上:

现在,您需要从查询中获得结果(这些结果存储在SESSION中(。您可以通过SESSION:循环来实现这一点

session_start(); // Make sure you add this at the top of the page 
foreach($_SESSION['search_output'] as $value){
    echo $value;
}