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

时间:2013-04-04 04:12:45

标签: php search

@Evan尝试将0结果回显到newpage.php,我怎样才能从newpage.php定义$ searchQuery ...似乎我必须将所有代码重写到newpage.php ..有没有办法只对另一个页面形成动作?

    <?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>

1 个答案:

答案 0 :(得分:0)

您希望将查询结果存储在array中。完成后,您可以使用header中的php将用户带到新页面。用户进入新页面后,您可以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;
}