doukan6564 2012-11-21 20:56
浏览 32
已采纳

选择使用jquery使用单选按钮查询多个表

I'm building a comics website, which has two subsites: Comics and Artwork. Comics and Artwork are stored in two separate tables.

I have a search function that allows the user to search for an image.

I'd like to give them an option to choose to search for only comics, only artwork, or both.

enter image description here

I have the following javascript which I believe should be working:

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

And the following 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>

My question is here with the PHP in querying TWO tables:

Am I doing the JOIN correctly?

$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);

Thanks!

  • 写回答

2条回答 默认 最新

  • dtr32221 2012-11-21 21:01
    关注

    No, you're not stating any conditions for joining the 2 tables, also in the case of looking in both tables, you may want to consider using UNION instead, as in:

    SELECT * FROM comics
    UNION
    SELECT * FROM artwork
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用