2017-03-24 31 views
0

我有從MySQL搜索自動完成的代碼,匹配查詢可以點擊並指引我到一個新的頁面。我怎樣才能將點擊查詢的結果顯示在新頁面中,無需使用數據庫中的任何URL,因爲我需要避免使用大量的HTML文件。謝謝。PHP Ajax顯示點擊結果

<p id="searchresults"> 
 
<?php 
 
\t // PHP5 Implementation - uses MySQLi. 
 
\t // mysqli('localhost', 'yourUserbookTitle', 'yourPassword', 'yourDatabase'); 
 
\t $db = new mysqli('localhost', 'root', '', 'book'); 
 
\t 
 
\t if(!$db) { 
 
\t \t // Show error if we cannot connect. 
 
\t \t echo 'ERROR: Could not connect to the database.'; 
 
\t } else { 
 
\t \t // Is there a posted query string? 
 
\t \t if(isset($_POST['queryString'])) { 
 
\t \t \t $queryString = $db->real_escape_string($_POST['queryString']); 
 
\t \t \t 
 
\t \t \t // Is the string length greater than 0? 
 
\t \t \t if(strlen($queryString) >0) { 
 
\t \t \t \t $query = $db->query("SELECT * FROM bookinfo WHERE bookTitle LIKE '%" . $queryString . "%'"); 
 
\t \t \t \t 
 
\t \t \t \t if($query) { 
 
\t \t \t \t \t // While there are results loop through them - fetching an Object. 
 
\t \t \t \t \t 
 
\t \t \t \t \t // Store the category id 
 
\t \t \t \t \t $bookTitle = 0; 
 
\t \t \t \t \t while ($result = $query ->fetch_object()) { 
 
\t \t \t \t \t \t if($result->bookTitle != $bookTitle) { // check if the category changed 
 
\t \t \t \t \t \t \t echo '<span class="category">'.$result->bookTitle.'</span>'; 
 
\t \t \t \t \t \t \t $bookTitle = $result->bookTitle; 
 
\t \t \t \t \t \t } 
 
\t   \t \t \t echo '<a href="'.$result->url.'">'; 
 
\t   \t \t \t echo '<img src="search_images/'.$result->bookimage.'" alt="" />'; 
 
\t   \t \t \t 
 
\t   \t \t \t $bookTitle = $result->bookTitle; 
 
\t   \t \t \t if(strlen($bookTitle) > 35) { 
 
\t   \t \t \t \t $bookTitle = substr($bookTitle, 0, 35) . "..."; 
 
\t   \t \t \t } \t   \t \t \t 
 
\t   \t \t \t echo '<span class="searchheading">'.$bookTitle.'</span>'; 
 
\t   \t \t \t 
 
\t   \t \t \t $author = $result->author; 
 
\t   \t \t \t if(strlen($author) > 80) { 
 
\t   \t \t \t \t $author = substr($author, 0, 80) . "..."; 
 
\t   \t \t \t } 
 
\t   \t \t \t 
 
\t   \t \t \t echo '<span>'.$author.'</span></a>'; 
 
\t   \t \t } 
 
\t   \t \t echo '<span class="seperator"><a href="http://www.marcofolio.net/sitemap.html" title="Sitemap">Nothing interesting here? Try the sitemap.</a></span><br class="break" />'; 
 
\t \t \t \t } else { 
 
\t \t \t \t \t echo 'ERROR: There was a problem with the query.'; 
 
\t \t \t \t } 
 
\t \t \t } else { 
 
\t \t \t \t // Dont do anything. 
 
\t \t \t } // There is a queryString. 
 
\t \t } else { 
 
\t \t \t echo 'There should be no direct access to this script!'; 
 
\t \t } 
 
\t } 
 
?> 
 
</p>

+3

請顯示你的代碼。 –

+0

請幫忙。謝謝 –

回答

0

我不知道如果我得到你的問題所在,但如果需要顯示與所選書的數據/審查/評論了新的一頁,我想補充:

echo '<span>'.$author.'</span></a>'; /* after this line */ 
echo '<span>Read more > <a href="newpage.php?id='.$bookID.'">click here</a></span>'; /* where $bookID id the ID column in your DB */ 

然後,在新頁面上,根據$ BOOKID值從數據庫中檢索數據並顯示它...