[英]Search results with PHP and MySQL
我最近發布了另一個與我正在研究的搜索引擎相關的線程,基於Kevin Yank的“構建自己的數據庫驅動的網站等”。 搜索引擎正在運行,但是我想修復一些錯誤並且不知道如何。 我將在這個帖子中只關注其中一個,這樣就不會太混亂了。
在數據庫中,有一個用於笑話的表(稱為“笑話”)和另一個用於主題的表(稱為“主題”)。 這兩個表與另一個名為“joketheme”的表相關聯。 每個笑話應該能夠有超過1個主題,我希望結果列出所有主題而不重復輸入。 到目前為止,我還沒有實現這一目標。 事實上,我不確定如何在MySQL中為1個笑話指定2個主題。
table 1: joke
id~~~joketext~~~other data
1~~~joke1~~~
2~~~joke2~~~
3~~~joke3~~~
table 2: theme
id~~~name
1~~Knock knock
2~~Lawyers
table 3: joketheme
jokeid~~~themeid
1~~~~~~1
1~~~~~~2
2~~~~~~2
3~~~~~~1
您是否知道我需要在代碼(或MySQL)中更改以在每個結果中列出多個主題(如果該條目具有多個主題?)?
這是搜索結果頁面中的代碼。 預先感謝您的任何幫助!:
<?php
$dbcnx = @mysql_connect('localhost', 'root', 'password');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' . 'database server at this time.</p>');
}
if (!@mysql_select_db('ijdb')) {
exit('<p>Unable to locate the joke ' . 'database at this time.</p>');
}
$authors = @mysql_query('SELECT id, name FROM author');
if (!$authors) {
exit('<p>Unable to obtain author list from the database.</p>');
}
$cats = @mysql_query('SELECT id, name FROM category');
if (!$cats) {
exit( '<p>Unable to obtain category list from the database.</p>');
}
$themes = @mysql_query('SELECT id, name FROM theme');
if (!$themes) {
exit( '<p>Unable to obtain category list from the database.</p>');
}
$geofoci = @mysql_query('SELECT id, name FROM geofocus');
if (!$geofoci) {
exit( '<p>Unable to obtain category list from the database.</p>');
}
?>
搜索表單:
<form class="searchField" name="input" action="fundfetch_search.php" method="post">
<ul>
<li>
<label>Search by keyword:</label>
<input type="text" name="searchtext" class="styleSearchbox" placeholder="By keyword" value="<?php echo $_POST['searchtext']; ?>">
</li>
<li>
<label>OR by the following: </label>
<label><select name="aid" size="1" class="styleDropdown">
<option selected value="">Any Author</option>
<?php
while ($author = mysql_fetch_array($authors)) {
$aid = $author['id'];
$aname = htmlspecialchars($author['name']);
echo "<option value='$aid'>$aname</option>\n";
}
?>
</select></label>
</li>
<li>
<label><select name="cid" size="1" class="styleDropdown">
<option selected value="">Any Category</option>
<?php
while ($cat = mysql_fetch_array($cats)) {
$cid = $cat['id'];
$cname = htmlspecialchars($cat['name']);
echo "<option value='$cid'>$cname</option>\n";
}
?>
</select></label>
</li>
<li>
<label><select name="tid" size="1" class="styleDropdown">
<option selected value="">Any Theme</option>
<?php
while ($theme = mysql_fetch_array($themes)) {
$tid = $theme['id'];
$tname = htmlspecialchars($theme['name']);
echo "<option value='$tid'>$tname</option>\n";
}
?>
</select></label>
</li>
<li>
<label><select name="gfid" size="1" class="styleDropdown">
<option selected value="">Any Region</option>
<?php
while ($geofocus = mysql_fetch_array($geofoci)) {
$gfid = $geofocus['id'];
$gfname = htmlspecialchars($geofocus['name']);
echo "<option value='$gfid'>$gfname</option>\n";
}
?>
</select></label>
</li>
<li style="visibility:hidden"><a href="../FUNDER.COM website/searchfilteroption">Closing</a></li>
<li><input type="submit" value="Search" class="searchButton"></li>
</ul>
</form>
查詢:
<?php
$dbcnx = @mysql_connect('localhost', 'root', 'password');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' . 'database server at this time.</p>');
}
if (!@mysql_select_db('ijdb')) {
exit('<p>Unable to locate the joke ' . 'database at this time.</p>');
}
$select = 'SELECT DISTINCT joke.id, joke.joketext, joke.jokedate,
author.id AS author_id, author.name AS author_name,
jokecategory.jokeid AS cat_jokeid, jokecategory.categoryid AS joke_catid, category.id AS cat_id, category.name as cat_name,
joketheme.jokeid AS theme_jokeid, joketheme.themeid AS joke_themeid, theme.id AS theme_id, theme.name AS theme_name,
jokegeofocus.jokeid AS geofocus_jokeid, jokegeofocus.geofocusid AS joke_geofocusid, geofocus.id AS geofocus_id, geofocus.name AS geofocus_name';
$from = ' FROM joke, author, jokecategory, category, joketheme, theme, jokegeofocus, geofocus';
$where = ' WHERE joke.authorid = author.id AND joke.id = jokecategory.jokeid AND jokecategory.categoryid = category.id AND joke.id = joketheme.jokeid AND joketheme.themeid = theme.id AND joke.id = jokegeofocus.jokeid AND jokegeofocus.geofocusid = geofocus.id';
$in = ' ORDER BY jokedate DESC';
$aid = $_POST['aid'];
if ($aid != '') { // An author is selected
$where .= " AND authorid='$aid'";
}
$cid = $_POST['cid'];
if ($cid != '') { // A category is selected
$from .= ''; // usually written as ' ,tablename'
$where .= " AND joke.id=jokecategory.jokeid AND categoryid='$cid'";
}
$tid = $_POST['tid'];
if ($tid != '') { // A theme is selected
$from .= '';
$where .= " AND joke.id=joketheme.jokeid AND themeid='$tid'";
}
$gfid = $_POST['gfid'];
if ($gfid != '') { // A region is selected
$from .= '';
$where .= " AND joke.id=jokegeofocus.jokeid AND geofocusid='$gfid'";
}
$searchtext = $_POST['searchtext'];
if ($searchtext != '') { // Some search text was specified
$where .= " AND keywords LIKE '%$searchtext%'";
}
?>
結果
<?php
$jokes = @mysql_query($select . $from . $where . $in);
if (!$jokes) {
echo '</table>'; exit('<p>Error retrieving jokes from database!<br />'.
'Error: ' . mysql_error() . '</p>');
}
$numrows = mysql_num_rows($jokes);
if ($numrows>0){
while ($joke = mysql_fetch_array($jokes)) {
$id = $joke['id'];
$joketext = htmlspecialchars($joke['joketext']);
$jokedate = htmlspecialchars($joke['jokedate']);
$aname = htmlspecialchars($joke['author_name']);
$category = htmlspecialchars($joke['cat_name']);
$theme = htmlspecialchars($joke['theme_name']);
$geofocus = htmlspecialchars($joke['geofocus_name']);
$position = 200;
$post = substr($joketext, 0, $position);
echo "<li id=\"jump\">
<article class=\"entry\">
<header>
<h3 class=\"entry-title\"><a href=''>$aname</a></h3>
</header>
<div class=\"entry-content\">
<p>$post...</p>
</div>
<div class =\"entry-attributes\">
<p>> Category: $category</p>
<p> > Theme(s): $theme</p>
<p> > Region(s) of focus: $geofocus</p>
</div>
<footer class=\"entry-info\">
<abbr class=\"published\">$jokedate</abbr>
</footer>
</article>
</li>";
}
}
else
echo "Sorry, no results were found. Please change your search parameters and try again!";
?>
聽起來你需要對SQL查詢以及它們的工作方式進行一些研究。 如果你想要一個單獨的笑話的主題列表,你可以做一個這樣的簡單查詢:
SELECT DISTINCT(theme.name) FROM theme
INNER JOIN joketheme ON joketheme.theme_id = theme.id
INNER JOIN joke ON joke.id = joketheme.joke_id
WHERE joke.id = {insert joke id}
我們在這里做的是一個連接。 我們將三個表連接在一起,根據所有表中常見的ID匹配結果。 這些被稱為密鑰。 基本上,我們試圖從主題表中選擇與特定笑話主題相匹配的所有主題。
查詢的DISTINCT部分確保我們只獲得唯一的結果。
我建議您查看一些SQL教程,以了解使用查詢獲取不同數據集的一些不同方法。 知道這些事情真的很有用,可以為你節省大量的代碼。
另外,就可讀性而言,我建議在連接表的表名之間加下下划線(例如joke_theme),或采用其他類似的約定。 使讀取更容易,並告訴哪一個是連接表,哪個是常規表。
祝好運 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.