2013-04-04 28 views
0

@Evan試圖回聲出到newpage.php還如何從newpage.php定義$ SEARCHQUERY 0結果...好像我不得不重寫所有的代碼到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> 

回答

0

你將要存儲在array查詢結果。一旦你這樣做,你可以使用headerphp帶給用戶一個新的頁面。一旦用戶在新頁面上,你可以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_start(); // Make sure you add this at the top of the page 

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

我不太清楚你是什麼意思...我是新來的PHP ...我會刪除else語句嗎? – arianna 2013-04-04 04:45:56

+0

@arianna所以它更清楚我會更新我的答案。 – 2013-04-04 04:48:34

+0

是的,我剛看到它非常感謝你埃文 – arianna 2013-04-04 04:50:27