2016-02-12 43 views
0

我有以下文件:如何將搜索結果中特定成分會進入另一個頁面

file1.php:包括搜索表單:

<form action="search.php" method="GET"> 
<input type="text" name="query" /> 
<input type="Submit" value="Search" /> 
</form> 

和 文件2「的search.php」是提交結果的文件:

<?php 
require "../session.php"; 
$query = isset($_GET['query']) ? $_GET['query'] : ""; 
$raw_results = mysql_query("SELECT * FROM Component WHERE (`ComName` LIKE '%".$query."%')") or die(mysql_error()); 
$number = mysql_num_rows($raw_results); 
if(mysql_num_rows($raw_results) > 0){ 
     $i=1; 
echo "<p><h3>" ."There are (<font color='red'>".$number ." </font>) matches to your search of" ."<font color='red'> $query </font>" ."</h3></p>" 
while($results = mysql_fetch_array($raw_results)) { 
$_SESSION['ComName']= $results['ComName']; 
print_r ("<p><h3>" ."<font color=yellow>" ."&nbsp" .$i."- </font>" ."<a href=\"ViewComponents.php?flag=Submit&status_Component=".$_SESSION['ComName']."\">" . str_ireplace($query, "<font color=red>$query</font>", $_SESSION['ComName']) .'</font>'."</h3>" ."</a>"; 
$i++; 

} else{ 
echo "<p><h3>" ."Sorry there are no results for your search of " ."<font color='red'> $query</font>." ."</h3></p>"; 
} 

} 
?> 

問題是當我從搜索結果頁面(如component2)點擊特定組件時;這應該傳遞到file3「ViewComponents.php」,但文件「ViewComponents.php只顯示搜索結果中的最後一個組件會話,而不是點擊組件

如何使點擊的組件傳遞給其他文件」ViewComponents .php「而不是僅傳遞最後一個組件?

+1

你可以在這裏發佈「ViewComponents.php」文件中負責查看組件的部分嗎? 您應該避免使用mysql_ *函數,因爲它們已被棄用並從PHP 7中刪除。 – Alfasatwi

+0

代碼是使用php5編寫的。部分文件「ViewComponents.php」是:<?php $ temp_package = $ _ SESSION ['ComName']; $ sql =「SELECT ProductName FROM組件其中ComName ='$ temp_package'ORDER BY ProductName ASC」; $ result = mysql_query($ sql); $ number = mysql_num_rows($ result); echo「
注意:我們的記錄顯示有(」。$ number。「)產品版本包含$ temp_package。 ?>

<? – Louie

回答

0

有沒有辦法,這裏的問題是,您正在使用SESSION數組獲取組件詳細信息,如第二個文件search.php所示,您只能保留最後一個組件元件名稱

在您的search.php文件中,刪除不必要的行

$_SESSION['ComName']= $results['ComName']; 

和鏈接改變傳遞GET參數,使其從結果陣列,而不是SESSION得到它:

這是你的search.php文件應該是什麼樣子:

<?php 
require "../session.php"; 
$query = isset($_GET['query']) ? $_GET['query'] : ""; 
$raw_results = mysql_query("SELECT * FROM Component WHERE (`ComName` LIKE '%".$query."%')") or die(mysql_error()); 
$number = mysql_num_rows($raw_results); 
if(mysql_num_rows($raw_results) > 0){ 
     $i=1; 
echo "<p><h3>" ."There are (<font color='red'>".$number ." </font>) matches to your search of" ."<font color='red'> $query </font>" ."</h3></p>" 
while($results = mysql_fetch_array($raw_results)) { 
print_r ("<p><h3>" ."<font color=yellow>" ."&nbsp" .$i."- </font>" ."<a href=\"ViewComponents.php?flag=Submit&status_Component=".$results['ComName']."\">" . str_ireplace($query, "<font color=red>$query</font>", $results['ComName']) .'</font>'."</h3>" ."</a>"; 
$i++; 

} else{ 
echo "<p><h3>" ."Sorry there are no results for your search of " ."<font color='red'> $query</font>." ."</h3></p>"; 
} 

} 
?> 

至於你在這裏的主要問題,即ViewComponent.php文件只顯示數據庫中的最後一個組件,請更改該文件中的第二行,以便變量$temp_packageGET數組獲取軟件包名稱在SESSION的EAD,所以這行:

$temp_package=$_SESSION['ComName']; 

變爲:

這應該做的工作。此外,mysql_*函數已被棄用並從PHP 7中刪除,您應該停止使用它們,請嘗試閱讀更多關於MySQLiPDO的信息。

+0

非常感謝你的幫助 – Louie