第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP 為什么我的過濾不能正常工作

PHP 為什么我的過濾不能正常工作

PHP
胡子哥哥 2021-06-08 17:42:50
所以,我必須做一個網(wǎng)上商店,一個人可以在那里過濾產(chǎn)品。我添加了按鈕 [pod 300€] 和 [vsi izdelki]。但是現(xiàn)在當(dāng)我點(diǎn)擊按鈕 [pod 300€] 時(shí),它會顯示低于 300€ 的產(chǎn)品,但也會顯示下面的所有產(chǎn)品。如何解決這個問題,當(dāng)我點(diǎn)擊 [pod 300€] 時(shí),它只顯示 300€ 以下的產(chǎn)品。<?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>';            }        }    }}
查看完整描述

1 回答

?
慕妹3146593

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個贊

問題是,如果你得到一個過濾器,你會用正確的過濾器發(fā)出一個 db-request,但是之后,你仍然在創(chuàng)建默認(rèn)的 db-request 來獲取所有產(chǎn)品。


與其發(fā)出多個數(shù)據(jù)庫請求,不如創(chuàng)建一個。您可以根據(jù)從客戶端獲得的過濾器更改查詢。


替代方案 #1 - 動態(tài)構(gòu)建查詢

像這樣的東西:


$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

    }

}

?>

這種方法的好處是您可以輕松地在同一查詢中添加更多條件/過濾器。


缺點(diǎn)是代碼變得有點(diǎn)難以閱讀。


替代方案 #2 - 選擇一個預(yù)定義的查詢

您可以定義多個查詢并選擇要使用的查詢:


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

    }

}

?>

這種方法的好處是它非常干凈,易于閱讀和遵循。


缺點(diǎn)是您只能同時(shí)啟用一個過濾器。


查看完整回答
反對 回復(fù) 2021-06-13
  • 1 回答
  • 0 關(guān)注
  • 151 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號