如何使用 GET 方法创建 php 的电子商务过滤器

How to create a ecommerce filter with php using the GET method

我知道有更好的方法来做到这一点,但我确实需要使用 PHP 创建一个多参数过滤器,只是为了从 json 中过滤项目。

类别按性别、项目类型、颜色等分隔...我希望能够 select 多个类别,如果只设置性别以显示所有产品,请使用$_GET 方法并能够使用 IF 进行过滤。

问题是我的业余编码技能没有太大帮助,而且我也不确定没有 AJAX 是否有更好的方法来做到这一点。

这是我过滤器的一部分:

<ul class="category-menu" name="categoria">         
    <li><a href="page.php?genid=<?php echo $genid;?>&cat=remeras&col=<?php echo ($_GET["col"]);?>&mar=<?php echo ($_GET["mar"]);?>" name="campera" >Remeras</a></li>
    <li><a href="page.php?genid=<?php echo $genid;?>&cat=camperas&col=<?php echo $col;?>&mar=<?php echo $mar;?>" name="campera">Camperas</a></li>
    <li><a href="page.php?genid=<?php echo $genid;?>&cat=pantalones&col=<?php echo $col;?>&mar=<?php echo $mar;?>" name="pantalones">Pantalones</a></li>
    <li><a href="page.php?genid=<?php echo $genid;?>&cat=shorts&col=<?php echo $col;?>&mar=<?php echo $mar;?>" name="short">Shorts</a></li>
    <li><a href="page.php?genid=<?php echo $genid;?>&cat=vestidos&col=<?php echo $col;?>&mar=<?php echo $mar;?>" name="vestido">Vestidos</a></li>
</ul>

我的 ifs 是这样的:


<?php   

}elseif ( !empty($_GET["genid"]) && !empty($_GET["cat"]) && $datosArray[$i]["sex_id"] == $_GET["genid"] && $datosArray[$i]["categoria"] == $_GET["cat"]){

?>

<!-- Individual Product full Code -->  
...
<!-- /Individual Product full Code -->  

<?php   

    }elseif (!empty($_GET["genid"]) && $datosArray[$i]["sex_id"] == $_GET["genid"]){
?>

<!-- Individual Product full Code -->  
 ... 
<!-- /Individual Product full Code --> 

<?php 
    }} ?> 

现在它唯一识别的 "filter" 是性别,即使 $_GET 已正确设置和显示,它也会显示所有产品。

提前谢谢大家。

您的代码显示结果的方式有点混乱。所以我的意思是你想过滤一个产品,从那里过滤类型,并根据 ajax 的要求遵循特定的细节,如颜色、尺寸等,是的。但我建议将这些参数保留在数据库中,并由此制作过滤器。如果你只基于 json 使用 json_decode ($data, true) 使它成为一个数组,这样你就可以更简单地做过滤器。我还建议使用模板引擎(例如 twig)将 php 代码与 html 分开。那对你来说会更好

如果我把你的代码变成伪代码,它看起来像这样。

如果我们有以下内容:a genid, cat
genid$datosArray[$i]["sex_id"]
相同 datosArray[$i]["categoria"]$_GET["cat"]
相同 然后显示产品

否则,如果我们有 genid
genid$datosArray[$i]["sex_id"]
相同 然后显示产品

如果这是您打算发生的情况,那么您可能需要 var_dumpprint_r 所有变量,并确保没有意外发生。


您谈到想要使用多个类别。我现在可以想到两种方法。第一种是使用逗号分隔的类别列表 href="...cat=pantelones,vestidos,shorts,然后将其转换为数组 $cats = explode(',', $_GET['cat'])。然后,您将使用 in_array('pantelones', $cats).

检查特定类别

第二种是创建一个HTML表单,使用复选框来select多个类别,然后如果你简单地在复选框名称的末尾添加括号,那么当用户提交表单 PHP 会自动将 $_GET['cat'] 转换为数组 See this SO question for more info about what I mean.


我会做更多这样的事情。

function display_product($productData){
    /* Add code here to display your product */
}

foreach($datosArray as $productData){

    /* Always display the product if the gender ID was not set */
    if ( empty($productData['genid']) ){
        display_product($productData);
    }

    /* Otherwise only display the product if it matches what is in $_GET[] */
    if (
        isset($_GET["genid"]) && $_GET["genid"] == $productData['sex_id']
        && isset($_GET["cat"]) && $_GET["cat"] == $productData['categoria']
        && isset($_GET["mar"]) && $_GET["mar"] == $productData['Marca']
    ){
        display_product($productData);
    }

}

我知道您已经说过您知道哪里有更好的数据过滤方法,我只想补充一点,一旦涉及到 add/edit/delete 数据,数据库就会变得非常有用。他们还可以更快地过滤出您想要的数据。


您引入了 XSS 漏洞。你应该总是在输出它们之前转义你的变量,就像这样。 (我喜欢使用 <?=,它只是 <?php echo 的 shorthand。)

<ul class="category-menu" name="categoria">         
    <li><a href="page.php?genid=<?= (int)$genid ?>&cat=remeras&col=<?= htmlspecialchars($_GET["col"]) ?>&mar=<?= htmlspecialchars($_GET["mar"]) ?>" name="campera" >Remeras</a></li>
    <li><a href="page.php?genid=<?= (int)$genid ?>&cat=camperas&col=<?= htmlspecialchars($col) ?>&mar=<?= htmlspecialchars($mar) ?>" name="campera">Camperas</a></li>
    <li><a href="page.php?genid=<?= (int)$genid ?>&cat=pantalones&col=<?= htmlspecialchars($col) ?>&mar=<?= htmlspecialchars($mar) ?>" name="pantalones">Pantalones</a></li>
    <li><a href="page.php?genid=<?= (int)$genid ?>&cat=shorts&col=<?= htmlspecialchars($col) ?>&mar=<?= htmlspecialchars($mar) ?>" name="short">Shorts</a></li>
    <li><a href="page.php?genid=<?= (int)$genid ?>&cat=vestidos&col=<?= htmlspecialchars($col) ?>&mar=<?= htmlspecialchars($mar) ?>" name="vestido">Vestidos</a></li>
</ul>

PHP 提供了一个 http_build_query($arrayOrObject) 功能,它本身就很有用。检查 docs.

我给自己做了一个我通常用于此类事情的函数的小包装器。

/**
 * Build query parameters string from given arrays ($_GET by default)
 * @param array $newGet
 * @param null|array $currentGet
 * @return string
 */
function new_build_query(array $newGet = [], ?array $currentGet = null)
{

    if (!isset($currentGet))
        $currentGet = $_GET;

    $newGet = array_merge($currentGet, $newGet);

    ksort($newGet);

    return http_build_query($newGet);
}

鉴于当前 URL 是 https://example.com/?page=2他可以这样使用:

$params = new_build_query([
    'sort' => 'asc',
    'filter' => 'female',
]);
// $params === 'filter=female&page=2&sort=asc'
$redirectUrl = "https://example.com/?{$params}";