简体   繁体   English

使用PHP和MySQL搜索结果

[英]Search results with PHP and MySQL

I recently posted another thread related to a search engine I was working on based on "Build your own DB driven website etc" by Kevin Yank. 我最近发布了另一个与我正在研究的搜索引擎相关的线程,基于Kevin Yank的“构建自己的数据库驱动的网站等”。 The search engine is working, however there are a few bugs I would like to fix and don't know how to. 搜索引擎正在运行,但是我想修复一些错误并且不知道如何。 I'll focuson just one of them in this thread so that it doesn't get too confusing. 我将在这个帖子中只关注其中一个,这样就不会太混乱了。

In the database, there is one table for the jokes (called "joke") and another table for the theme (called "theme"). 在数据库中,有一个用于笑话的表(称为“笑话”)和另一个用于主题的表(称为“主题”)。 These two tables are related by another table called "joketheme". 这两个表与另一个名为“joketheme”的表相关联。 Each joke should be able to have more than 1 theme and I would like the results to list all the themes without repeating the entries. 每个笑话应该能够有超过1个主题,我希望结果列出所有主题而不重复输入。 So far, I haven't been able to achieve that. 到目前为止,我还没有实现这一目标。 In fact, I'm not sure how to even designate 2 themes for 1 joke within MySQL. 事实上,我不确定如何在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

Do you know what I need to change in the code (or in the MySQL) to list more than one theme in each result (if that entry has more than one theme?)? 您是否知道我需要在代码(或MySQL)中更改以在每个结果中列出多个主题(如果该条目具有多个主题?)?

Here is my code in the search results page. 这是搜索结果页面中的代码。 Thank you in advance for any help!: 预先感谢您的任何帮助!:

    <?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>');
}

?>

Search form: 搜索表单:

    <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>

Query: 查询:

    <?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%'";
    }
    ?>  

Results 结果

    <?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!";
     ?>

Sounds like you need to do some research into SQL queries and how they work. 听起来你需要对SQL查询以及它们的工作方式进行一些研究。 If you want a list of themes for an individual joke, you can do a simple query like this: 如果你想要一个单独的笑话的主题列表,你可以做一个这样的简单查询:

  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}

What we're doing here is called a join. 我们在这里做的是一个连接。 We're joining the three tables together, matching results based on ids that are common across all tables. 我们将三个表连接在一起,根据所有表中常见的ID匹配结果。 These are called keys. 这些被称为密钥。 Basically, we're trying to select all the themes from the theme table that match up with a specific joke's theme. 基本上,我们试图从主题表中选择与特定笑话主题相匹配的所有主题。

The DISTINCT part of the query ensures that we only get unique results. 查询的DISTINCT部分确保我们只获得唯一的结果。

I suggest you check out some SQL tutorials to find out about some different ways of getting different sets of data using queries. 我建议您查看一些SQL教程,以了解使用查询获取不同数据集的一些不同方法。 It's really useful to know these kinds of things and could save you a ton of that code up there. 知道这些事情真的很有用,可以为你节省大量的代码。

Also, as far as readability goes, I would suggest putting underscores between table names for join tables (eg joke_theme), or adopting some other similar convention. 另外,就可读性而言,我建议在连接表的表名之间加下下划线(例如joke_theme),或采用其他类似的约定。 Makes it easier to read and tell which one is a join table and which is a regular table. 使读取更容易,并告诉哪一个是连接表,哪个是常规表。

Good luck :) 祝好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM