【问题标题】:How to create a ecommerce filter with php using the GET method如何使用 GET 方法使用 php 创建电子商务过滤器
【发布时间】:2019-12-05 19:00:19
【问题描述】:

我知道有更好的方法可以做到这一点,但我确实需要使用 PHP 创建一个多参数过滤器,以便通过 json 过滤项目。

类别按性别、项目类型、颜色等分隔...我希望能够选择多个类别,如果仅将性别设置为显示所有产品,则使用 $_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 
    }} ?> 

现在它识别的唯一“过滤器”是性别过滤器,它显示所有产品,即使 $_GET 已正确设置和显示。

提前谢谢大家。

【问题讨论】:

    标签: php html json get e-commerce


    【解决方案1】:

    如果我把你的代码变成伪代码,它会是这样的。

    如果我们有以下内容: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表单,使用一个复选框来选择多个类别,然后如果你只是在复选框名称的末尾加上括号,那么当用户提交表单时,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);
        }
    
    }
    

    我知道你已经说过你知道有更好的过滤数据的方法,我只想补充一点,一旦需要添加/编辑/删除数据,数据库就会变得非常有用。它们还可以更快地过滤掉您想要的数据。


    您引入了 XSS 漏洞。您应该始终在输出变量之前对其进行转义,就像这样。 (我喜欢使用&lt;?=,它只是&lt;?php echo 的简写。)

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

    【讨论】:

      【解决方案2】:

      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=2his 可以这样使用:

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

      【讨论】:

        【解决方案3】:

        您的代码在显示结果的方式上有点混乱。所以我的意思是你想过滤一个产品,从那里根据 ajax 的要求来筛选类型和以下具体细节,如颜色、尺寸等是可能的。但我建议将这些参数保留在数据库中,并由此制作过滤器。如果您仅基于 json 使用 json_decode ($ data, true) 使其成为一个数组,以便您可以更简单地执行过滤器。我还建议使用模板引擎(例如树枝)将 php 代码与 html 分开。那对你会更好

        【讨论】:

          猜你喜欢
          • 2022-12-13
          • 1970-01-01
          • 2019-07-09
          • 2020-02-12
          • 2017-08-25
          • 1970-01-01
          • 2013-10-18
          • 2019-03-15
          • 2017-09-07
          相关资源
          最近更新 更多