PHP为什么我的过滤无法正常工作

时间:2019-05-09 15:38:57

标签: php html sql

因此,我必须创建一个网上商店,一个人可以在其中过滤产品。 我添加了[pod 300€]和[vsi izdelki]按钮。但是现在当我 单击按钮[pod 300€],它会显示价格低于300€的商品,但同时还会显示下方的所有商品。我如何解决该问题 点击[pod 300€],它仅显示300€以下的产品。

<?php 
include "header.php";

$connect = mysqli_connect("localhost", "root", "", "registration");

if(isset($_POST["add_to_cart"]))
{
    if(isset($_SESSION["shopping_cart"]))
    {
        $item_array_id = array_column($_SESSION["shopping_cart"], "item_id");
        if(!in_array($_GET["id"], $item_array_id))
        {
            $count = count($_SESSION["shopping_cart"]);
            $item_array = array(
                'item_id'           =>  $_GET["id"],
                'item_name'         =>  $_POST["hidden_name"],
                'item_price'        =>  $_POST["hidden_price"],
                'item_quantity'     =>  $_POST["quantity"]
            );
            $_SESSION["shopping_cart"][$count] = $item_array;
        }
        else
        {
            echo '<script>alert("Izdelek je že bil dodan")</script>';
        }
    }
    else
    {
        $item_array = array(
            'item_id'           =>  $_GET["id"],
            'item_name'         =>  $_POST["hidden_name"],
            'item_price'        =>  $_POST["hidden_price"],
            'item_quantity'     =>  $_POST["quantity"]
        );
        $_SESSION["shopping_cart"][0] = $item_array;
    }
}

if(isset($_GET["action"]))
{
    if($_GET["action"] == "delete")
    {
        foreach($_SESSION["shopping_cart"] as $keys => $values)
        {
            if($values["item_id"] == $_GET["id"])
            {
                unset($_SESSION["shopping_cart"][$keys]);
                echo '<script>alert("Izdelek odstranjen")</script>';
                echo '<script>window.location="kosarica.php"</script>';
            }
        }
    }
}

?>
<!DOCTYPE html>
<html>
    <head>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    </head>
    <body>
        <br />
        <div class="container">
            <br />
            <br />
            <br />
            <br /><br />
            <?php
            $connect = mysqli_connect("localhost", "root", "", "registration");
            if(isset($_POST["manj300"])){
            $query =mysqli_query($connect, "SELECT * FROM tbl_product WHERE price<=300");   

            while($row=mysqli_fetch_array($query)){ 
            ?>
                <div class="col-md-4">
                <form method="post" action="produkti.php?action=add&id=<?php echo $row["id"]; ?>">
                    <div style="border:1px solid #333; background-color:#f1f1f1; border-radius:5px; padding:16px;" align="center">
                        <img src="slike/<?php echo $row["image"]; ?>" class="img-responsive" /><br />

                        <h4 class="text-info"><?php echo $row["name"]; ?></h4>

                        <h4 class="text-danger">€ <?php echo $row["price"]; ?></h4>
                        <input type="text" name="quantity" value="1" class="form-control" />

                        <input type="hidden" name="hidden_name" value="<?php echo $row["name"]; ?>" />

                        <input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>" />

                        <input type="submit" name="add_to_cart" style="margin-top:5px;" class="btn btn-success" value="Dodaj v košarico" />

                    </div>
                </form>
            </div>
            <?php   
            }
            }
            ?>
            <?php
            $connect = mysqli_connect("localhost", "root", "", "registration");
            if(isset($_POST["vsi_izdelki"])){
            $query =mysqli_query($connect, "SELECT * FROM tbl_product");    

            while($row=mysqli_fetch_array($query)){ 
            ?>
                <div class="col-md-4">
                <form method="post" action="produkti.php?action=add&id=<?php echo $row["id"]; ?>">
                    <div style="border:1px solid #333; background-color:#f1f1f1; border-radius:5px; padding:16px;" align="center">
                        <img src="slike/<?php echo $row["image"]; ?>" class="img-responsive" /><br />

                        <h4 class="text-info"><?php echo $row["name"]; ?></h4>

                        <h4 class="text-danger">€ <?php echo $row["price"]; ?></h4>

                    </div>
                </form>
            </div>
            <?php   
            }
            }
            ?>
            <form method="post" action="produkti.php">

            <input type="submit" name="manj300" style="margin-top:5px;" class="btn btn-success" value="pod 300€" />
            <input type="submit" name="vsi_izdelki" style="margin-top:5px;" class="btn btn-success" value="vsi izdelki" />


            </form>


            <?php
                $query = "SELECT * FROM tbl_product ORDER BY id ASC";
                $result = mysqli_query($connect, $query);
                if(mysqli_num_rows($result) > 0)
                {
                    while($row = mysqli_fetch_array($result))
                    {
                ?>
            <div class="col-md-4">
                <form method="post" action="produkti.php?action=add&id=<?php echo $row["id"]; ?>">
                    <div style="border:1px solid #333; background-color:#f1f1f1; border-radius:5px; padding:16px;" align="center">
                        <img src="slike/<?php echo $row["image"]; ?>" class="img-responsive" /><br />

                        <h4 class="text-info"><?php echo $row["name"]; ?></h4>

                        <h4 class="text-danger">€ <?php echo $row["price"]; ?></h4>

                        <input type="text" name="quantity" value="1" class="form-control" />

                        <input type="hidden" name="hidden_name" value="<?php echo $row["name"]; ?>" />

                        <input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>" />

                        <input type="submit" name="add_to_cart" style="margin-top:5px;" class="btn btn-success" value="Dodaj v košarico" />

                    </div>
                </form>
            </div>
            <?php
                    }
                }
            ?>
            <div style="clear:both"></div>
            <br />

        </div>
    </div>
    <br />
    </body>
</html>

<?php
   include "footer.php";
?>

the result now

1 个答案:

答案 0 :(得分:1)

问题是,如果获得过滤器,则会使用正确的过滤器进行db-request,但是之后,您仍将使用默认的db-request来获取所有产品。

不要发出多个数据库请求,而只需发出一个。您可以根据从客户端获得的过滤器来更改查询。

替代方法1-动态构建查询

类似这样的东西:

$whereConditions = [];
$where           = '';

if (isset($_POST["manj300"])) {
    // Add this filter
    $whereConditions[] = 'price<=300';   
}

// Here you can add more conditions, just like the above if-statement

if ($whereConditions) {
    // We have a condition, implode and add WHERE
    $where = 'WHERE ' . implode(' ', $whereConditions);   
}

// Now put the where conditions in your query
$query = "SELECT * FROM tbl_product {$where} ORDER BY id ASC";
$result = mysqli_query($connect, $query);

if(mysqli_num_rows($result) > 0)
{
    while($row = mysqli_fetch_array($result))
    {
        // Your current code
    }
}
?>

此方法的好处是您可以轻松地在同一查询中添加更多条件/过滤器。

缺点是代码变得更难阅读。

替代方法2-选择预定义的查询

您可以定义多个查询,然后选择要使用的查询:

// This is the "get all" query
$query = "SELECT * FROM tbl_product ORDER BY id ASC";

if (isset($_POST["manj300"])) {
    // We have a filter, let's override the default query
    $query = "SELECT * FROM tbl_product price<=300 ORDER BY id ASC";
}

$result = mysqli_query($connect, $query);

if(mysqli_num_rows($result) > 0)
{
    while($row = mysqli_fetch_array($result))
    {
        // Your current code
    }
}
?>

此方法的好处是它非常干净,易于阅读和遵循。

缺点是您只能同时启用一个过滤器。