0

@Evan 试图在 newpage.php 上回显 0 结果,我如何从 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>
4

1 回答 1

0

您将希望将查询结果存储在array. 完成后,您可以使用headerinphp将用户带到新页面。用户进入新页面后,您可以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;
}
于 2013-04-04T04:36:30.793 回答