0

@Evan は 0 の結果を newpage.php にエコーアウトしようとしています。また、newpage.php から $searchQuery を定義するにはどうすればよいですか?別のページへのアクションを形成する方法は?

    <?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. これが完了したら、 in を使用headerphpてユーザーを新しいページに移動できます。ユーザーが新しいページに移動したらecho、クエリの結果を返すことができます (現在は に保存されています$_SESSION)。

まず、配列を作成します。

$queryArray = array();

次に、データベースにクエリを実行し、作成したばかりの配列に各行を格納します

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

       $queryArray[] = $row;


        } // close while

3 番目に、配列を SESSION 変数に移動します。

$_SESSION['search_output'] = $queryArray;

4 番目に、ユーザーを新しいページに移動します。

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 に答える