Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将搜索结果回显到其他页面_Php_Search - Fatal编程技术网

Php 如何将搜索结果回显到其他页面

Php 如何将搜索结果回显到其他页面,php,search,Php,Search,@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 = arra

@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;
}
}
  ?>

您需要将查询结果存储在
数组中。完成后,您可以在
php
中使用
header
将用户带到新页面。用户进入新页面后,您可以
回显
查询结果(现在存储在
$\u会话中
):

首先,创建一个数组:

$queryArray = array();
$_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;
}

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

while($row = mysql_fetch_array($query)){

       $queryArray[] = $row;


        } // close while
第三,将数组移动到会话变量:

$queryArray = array();
$_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;
}

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

$queryArray = array();
$_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;
}

  ?>
最后,回显您的数据:

$queryArray = array();
$_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;
}

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

$queryArray = array();
$_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;
}

  ?>

我不太清楚你的意思。。。我是新来的php。。。我可以删除else语句吗?@arianna让我知道哪个部分让您感到困惑,我会在我醒来时解决它。它正在重定向到“newpage.php”,但其显示注意:未定义变量:\ newpage.php中的会话在第1行警告:为foreach()提供的参数无效在第1行的\newpage.php中,您需要将
session\u start
添加到您正在使用的每个页面的顶部
session
,您一定对我很生气,我很抱歉。。但其显示注意事项:未定义索引:第5行的\newpage.php中的search\u输出警告:为第5行的\newpage.php中的foreach()提供的参数无效我已更新了代码(参见上文)