PHP Ajax显示单击结果

时间:2017-03-24 16:34:15

标签: php ajax

我有从MySQL搜索自动填充的代码,可以点击匹配查询并将我引导到新页面。如何使用来自数据库的任何URL将新单击的查询结果显示在新页面中,因为我需要避免使用大量HTML文件。谢谢。



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

1 个答案:

答案 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值从DB检索数据并显示它......