0

我正在建立一个漫画网站,它有两个子网站:漫画和艺术品。Comics 和 Artwork 存储在两个单独的表中。

我有一个搜索功能,允许用户搜索图像。

我想给他们一个选项来选择只搜索漫画、只搜索艺术品或两者兼而有之。

在此处输入图像描述

我有以下我认为应该可以工作的javascript :

    <script type="text/javascript">

        function search(searchString) {     
           var site = $("#site").val();
           $.get("./scripts/search.php", {_input : searchString, _site : site},
                function(returned_data) {
                    $("#output").html(returned_data);
                }
            );
        }

        function searchChoice(choice) {     
           $.get("./scripts/search.php", {_choice : choice}
           );
        }

</script>

以及以下 HTML:

    <!--Search filtering for comics, artwork, or both-->
<span class="search"><b>Search for: </b> </span>

<div class="btn-group" data-toggle="buttons-radio">
<span class="search">
    <button type="button" class="btn" id="comics" onclick="searchChoice(this.id)">Comics</button> 
    <button type="button" class="btn" id="artwork" onclick="searchChoice(this.id)">Artwork</button> 
    <button type="button" class="btn" id="all" onclick="searchChoice(this.id)">All</button> 
</span>
</div>

<br/>
<br/>

<!--Search functionality-->
<span class="search">
    <input type="text" onkeyup="search(this.value)" name="input" value="" />
    <input id="site" type="hidden" value="<?php echo $site; ?>">
</span>

<br />
<span id="output"><span class="sidebarimages">  </span></span>

我的问题是PHP查询两个表:

我正确地做 JOIN 吗?

$input = (isset($_GET['_input']) ? ($_GET['_input']) : 0); 
$choice = (isset($_GET['_choice']) ? ($_GET['_choice']) : "all");
$site = (isset($_GET['site']) ? ($_GET['site']) : null);


if ($choice == "artwork") {
    $sql = "SELECT id, title, thumb FROM artwork";
    $thumbpath = "./images/Artwork/ArtThumbnails/";
}
else if ($choice == "comics") {
    $sql = "SELECT id, title, thumb FROM comics";
    $thumbpath = "./images/Comics/ComicThumbnails/";
}
else {
    $sql = "SELECT id, title, thumb FROM comics 
            UNION 
            SELECT id, title, thumb FROM artwork";
    $thumbpath = "./images/AllThumbnails/";
}


$imgpaths = $mysqli->query($sql);
mysqli_close($mysqli);

谢谢!

4

2 回答 2

2

要使用 JOIN 功能,您需要表之间的关系(连接一个到另一个的外键)

于 2012-11-21T21:01:41.763 回答
1

不,您没有说明加入 2 个表的任何条件,同样在查看两个表的情况下,您可能需要考虑UNION改用,如下所示:

SELECT * FROM comics
UNION
SELECT * FROM artwork
于 2012-11-21T21:01:01.730 回答