【问题标题】:How do I filter a MySQL query with a select option which results in the right product如何使用导致正确产品的选择选项过滤 MySQL 查询
【发布时间】:2013-03-18 15:36:59
【问题描述】:

我正在开发一个基于数据库的 CMS。 数据库结构如下(语言为荷兰语):

id、artikelnummer_fabrikant、breedte、diepte、hoogte、extrainfo、afmeting、kleur、merk、serie、producttype、omschrijving、prijsfinal、levertijd

通过下一段代码,我从数据库中获取所有信息并将其放入表中:

(对不起,我没有使用 来创建标签,这个文本编辑器将其视为标签而不是文本,我不知道如何使这项工作正确,欢迎任何提示!)

function get_content() {

echo "<p>";
echo "<table>";
echo "<tr>";
echo "<td> id </td>";
echo "<td> artikelnummer frabrikant </td>"
and so on
echo "</tr>";

$sql = "SELECT * FROM products";
$res = mysql_query($sql) or die (mysql_error());

if (mysql_num_rows($res) != 0) :
while ($row = mysql_fetch_assoc($res)) :
echo "<tr>";
echo "<td> . $row['id'] . </td>";
echo "<td> . $row['artikelnummer_fabrikant'] . </td>";
and so on
echo "</tr>";
endwhile;

echo "</table>";
echo "</p>";

endif;

现在我想使用标签选择和选项在此基础上构建一个过滤器,这样当我选择选项品牌(merk)“温莎”时,它将显示所有带有品牌“温莎”的产品以及所有信息,例如Brette, hoogte, diepte(宽度、高度、深度)。

我有一段代码在选项菜单中提供了所有品牌(merk):

function category_filter() {

echo "Merk: ";
echo "<select onchange=window.location = this.value>;";
$sql = "SELECT merk FROM products";
$res = mysql_query($sql) or die (mysql_error());

if (mysql_num_rows($res) !=0 ) :
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) :
echo "<option> . $row['merk'] . </option>";
endwhile;
endif;
echo "</select>";

当然我对数据库中的每个项目都有 1 个。

接下来我想做的是以某种方式组合这两个代码,当我在选项菜单品牌(merk)中选择品牌(merk)“Windsor”时,它将显示所有带有品牌“Windsor”的产品+ 该产品的所有其他信息。

我苦苦挣扎了一周,试图让这件事发生,但我无法让它按照我想要的方式进行:(。

我怎样才能做到这一点?

【问题讨论】:

  • 请使用格式代码按钮...我会这样做,但是您添加了很多html标签...
  • Please, don't use mysql_* functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解 prepared statements,并使用 PDOMySQLi - this article 将帮助您决定哪个。
  • 你为什么要写所有没有 的 HTML 标记?
  • 你好@AarolamaBluenk 我确实听说过这个,愚蠢的我仍然使用mysql。我将使用 mysqli 代替。 michi 我不知道如何在编辑器中使用它们,当我使用它们时,它们实际上被用作代码而不是文本。 neal 你将如何使用 mysqli 构建代码?
  • @michi 已编辑标签

标签: php mysql


【解决方案1】:

好的,所以我通过编写以下代码了解了如何做到这一点:

    echo    "<form class='merk' action='index.php'>";
        echo        "<select name=merk>";
        echo            "<option value=''>Kies een merk</option>";

        while ($row_merk    =   mysqli_fetch_array($merk_query_selected)) :

        $filter =   $row_merk['merk'];

            if($filter == $merk) :
                echo    "<option value=" . urlencode($filter) . " selected='selected'>" . $filter . "</option>";
            else :
                echo    "<option value=" . urlencode($filter) . ">" . $filter . "</option>";
            endif;

        endwhile;

        echo        "</select>";        
        echo        "<input class='input-merk' type='submit' value='Filter op merk'>";
        echo    "</form><br />";

$('form').each(function() {
        var that = $(this);
        $.post(that.attr('action'), that.serialize());
    });</script>

解码:

$merk                                       =   urldecode($_GET["merk"]);

然后通过运行这个while循环来获取内容:

while ($row_merk    =   mysqli_fetch_array($merk_query_all)) :
            echo    $table_row;
            echo        $table_cell, $row_merk['id'] . $table_div_end;
            echo        $table_cell, $row_merk['artikelnummer_fabrikant'] . $table_div_end;
            echo        $table_cell, $row_merk['breedte'] . $table_div_end;
            echo        $table_cell, $row_merk['diepte'] . $table_div_end;
            echo        $table_cell, $row_merk['hoogte'] . $table_div_end;
            echo        $table_cell, $row_merk['extrainfo'] . $table_div_end;
            echo        $table_cell, $row_merk['afmeting'] . $table_div_end;
            echo        $table_cell, $row_merk['kleur'] . $table_div_end;
            echo        $table_cell, $row_merk['merk'] . $table_div_end;
            echo        $table_cell, $row_merk['serie'] . $table_div_end;
            echo        $table_cell, $row_merk['producttype'] . $table_div_end;
            echo        $table_cell, $row_merk['omschrijving'] . $table_div_end;
            echo        $table_cell, $row_merk['prijsfinal'] . $table_div_end;
            echo        $table_cellEnd, $row_merk['levertijd'] . $table_div_end;
            echo    $table_row_end;
        endwhile;

现在我想做的是让它动态化,以便它可以同时过滤多个选择。

尽我所能,欢迎任何帮助!

[编辑] 了解它是如何工作的,但它仍然不是动态的。但是这个问题已经解决了,我会去网上看看如何使这个动态化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    • 2019-01-11
    • 1970-01-01
    • 2023-03-05
    • 2023-03-18
    相关资源
    最近更新 更多