donglv5269 2018-12-05 17:58
浏览 95

搜索多个类别的产品

I have this search function with categories and when you click on this category you will search within that category, however I want to make it so that I'm able to search a keyword within multiple categories. This is my form:

        <form method="get" action="zoek.php">
            <input type="text" name="producttitle" placeholder="Productnaam">
            <input type="hidden" name="prodgroup" value="<?= (isset($_GET['prodgroup'])) ? $_GET['prodgroup'] : '' ?>">
            <input type=submit class="btn btn-primary" value="Zoek...">
        </form>

This is the code where you clicked on a category and searched a word

      <?php
    if (isset($_GET['prodgroup']) && $_GET['prodgroup'] != "" && isset($_GET['producttitle']) && $_GET['producttitle'] != "") {
        $category = $_GET['prodgroup'];
        $prodtit = $_GET['producttitle'];


        $query = $readconn->prepare("
SELECT S.StockItemID, S.StockItemName, S.Photo, S.UnitPrice, S.Photo, S.Brand
FROM stockitems S JOIN stockitemstockgroups SISG ON S.StockItemID = SISG.StockItemID
JOIN stockgroups SG ON SISG.StockGroupID = SG.StockGroupID
WHERE SG.StockGroupID = '" . $category . "'
AND S.StockItemName LIKE '%" . $prodtit . "%'
");

These are the categories with their ID's:

.--------------.---------------------. | StockGroupID | Col2 | :--------------+---------------------: | 1 | Novelty Items | :--------------+---------------------: | 2 | Clothing | :--------------+---------------------: | 3 | Mugs | :--------------+---------------------: | 4 | T-Shirts | :--------------+---------------------: | 5 | Airline Novelties | :--------------+---------------------: | 6 | Computing Novelties | :--------------+---------------------: | 7 | USB Novelties | :--------------+---------------------: | 8 | Furry Footwear | :--------------+---------------------: | 9 | Toys | :--------------+---------------------: | 10 | Packaging Materials | '--------------'---------------------'

This table has all the items and as you can see some items can be in multiple categories.

.-----------------------.--------------. | StockItemStockGroupID | StockGroupID | :-----------------------+--------------: | 4 | 1 | :-----------------------+--------------: | 1 | 2 | :-----------------------+--------------: | 2 | 2 | :-----------------------+--------------: | 3 | 3 | :-----------------------+--------------: | 5 | 4 | :-----------------------+--------------: | 6 | 7 | :-----------------------+--------------: | 8 | 7 | :-----------------------+--------------: | 7 | 8 | :-----------------------+--------------: | 9 | 9 | :-----------------------+--------------: | 10 | 2 | '-----------------------'--------------'

here is also another table that has all the names of the items so I can search based on keywords. But I don't think I need that here. So what I've tried to do is change the WHERE statement to a IN like this:

WHERE SG.StockGroupID IN (SELECT StockGroupID FROM stockitemstockgroups) however it still won't hold multiple categories. This is the URL

http://localhost/kbs/zoek.php?producttitle=pizza&prodgroup=1

While I think it's supposed to be like this:

http://localhost/kbs/zoek.php?producttitle=pizza&prodgroup=1&prodgroup=2

  • 写回答

1条回答 默认 最新

  • dsdsds12222 2018-12-05 18:14
    关注

    I think you should transform the where part of your query to somethng like

    WHERE SG.StockGroupID IN (" . $categories . ") ...
    

    where $categories should be a string like '1,2,4' created using a UI that allow the User to pick the categories he needs.

    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)