Php 在同一列上应用where条件两次或三次

Php 在同一列上应用where条件两次或三次,php,jquery,ajax,Php,Jquery,Ajax,我有一个目录页面,通过ajax显示产品。 Ajax调用的代码如下: function updateProducts(opts){ $.ajax({ type: "POST", url: "func.php", dataType : 'json', cache: false, data: {filterOpts: opts}, success: function(records){ $('#slide

我有一个目录页面,通过ajax显示产品。 Ajax调用的代码如下:

function updateProducts(opts){

    $.ajax({
      type: "POST",
      url: "func.php",
      dataType : 'json',
      cache: false,
      data: {filterOpts: opts},
      success: function(records){
        $('#slider').html(makeProdiv(records));

      }
    });
  }
func.php的代码如下:

$pdo = new PDO('mysql:host=localhost;dbname=filter', 'root', '');

  $select = 'SELECT id, pname, prate, pdesc';
  $from = ' FROM product';
  $where = ' WHERE TRUE';

  $opts = isset($_POST['filterOpts'])? $_POST['filterOpts'] : array('');

  if (in_array("Shoes", $opts)) { $where .= " AND ptype = 'Shoes'"; }
  if (in_array("Belt", $opts))  { $where .= " AND ptype = 'Belt'"; }
  if (in_array("lt1th", $opts)) { $where .= " AND prate < 1000"; }
  if (in_array("mr1th", $opts)) { $where .= " AND prate > 1000"; }
  if (in_array("lth", $opts))   { $where .= " order by prate asc"; }
  if (in_array("htl", $opts))   { $where .= " order by prate desc"; }

  $sql = $select . $from . $where;
  $statement = $pdo->prepare($sql);
  $statement->execute();
  $results = $statement->fetchAll(PDO::FETCH_ASSOC);
  $json = json_encode($results);
  echo($json);
$allptype = array('Belt','Shoes');
$selectedptype = array();
  foreach($allptype as $ptype){
    if(in_array($ptype,$opts)){
        $selectedptype[] = $ptype;
    }
  }
  if(count($selectedptype)){
    $where .= " AND ptype IN ('".implode("', '", $selectedptype)."')";
  }
$pdo=newpdo('mysql:host=localhost;dbname=filter','root','');
$select='select id、pname、prate、pdesc';
$from=‘from product’;
$where='where TRUE';
$opts=isset($\u POST['filterOpts'])$_POST['filterOpts']:数组(“”);
if(在_数组中($Shoes',$opts)){$where.=“和ptype='Shoes'”;}
if(在_数组(“Belt”,$opts)中){$where.=“和ptype='Belt'”;}
if(在数组(“lt1th”,“lt1th”,“opts”){$where.=“和prate<1000”}
if(在_数组中(“mr1th”,$opts)){$where.=“AND prate>1000”;}
if(在_数组(“lth”,$opts)中){$where.=“order by prate asc”;}
if(在数组(“htl”,$opts)中){$where.=“orderbyprate desc”;}
$sql=$select$从$哪里
$statement=$pdo->prepare($sql);
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($results);
echo($json);
我面临的问题是:

  • 当我在过滤器中同时选择皮带和鞋子时,不会显示任何结果,因为在选择这两个选项时,查询结果如下所示:
  • 从产品中选择id、pname、prate、pdesc,其中TRUE和ptype='Shoes'和ptype='Belt'

    请让我知道如何实现这一点,因为单一产品检查工作正常。

    如果(在_数组中(“Shoes”,$opts)){$where.=“和ptype='Shoes'”;} if(在_数组(“Belt”,$opts)中){$where.=“和ptype='Belt'”;}

    你为什么不用“或”来代替? 因此,完整的sql将是

    “从产品中选择id、pname、prate、pdesc,其中TRUE和ptype=‘Shoes’和ptype=‘Belt’”
    这就是您想要的吗?

    WHERE
    子句中删除
    TRUE

    您的
    ptype
    值在任何时候都不相同(如果是
    shoes
    belt

    所以你需要像这样改变

    $pdo = new PDO('mysql:host=localhost;dbname=filter', 'root', '');
    
      $select = 'SELECT id, pname, prate, pdesc';
      $from   = ' FROM product';
      $where  = ' WHERE';
    
      $opts = isset($_POST['filterOpts'])? $_POST['filterOpts'] : array('');
    
      if (in_array("Shoes", $opts) && in_array("Belt", $opts)) { 
         $where .= " ptype = 'Shoes' OR ptype = 'Belt'"; 
      }
      else if (in_array("Shoes", $opts)) { 
         $where .= " ptype = 'Shoes'"; 
      }
      else if (in_array("Belt", $opts))  { 
         $where .= " ptype = 'Belt'"; 
      }
    
      $sql = $select . $from . $where;
      $statement = $pdo->prepare($sql);
      $statement->execute();
      $results = $statement->fetchAll(PDO::FETCH_ASSOC);
      $json = json_encode($results);
      echo($json);
    

    嘿,谢谢你的回复

    我找到了答案,实现了以下目标:

    $pdo = new PDO('mysql:host=localhost;dbname=filter', 'root', '');
    
      $select = 'SELECT id, pname, prate, pdesc';
      $from = ' FROM product';
      $where = ' WHERE TRUE';
    
      $opts = isset($_POST['filterOpts'])? $_POST['filterOpts'] : array('');
    
      if (in_array("Shoes", $opts)) { $where .= " AND ptype = 'Shoes'"; }
      if (in_array("Belt", $opts))  { $where .= " AND ptype = 'Belt'"; }
      if (in_array("lt1th", $opts)) { $where .= " AND prate < 1000"; }
      if (in_array("mr1th", $opts)) { $where .= " AND prate > 1000"; }
      if (in_array("lth", $opts))   { $where .= " order by prate asc"; }
      if (in_array("htl", $opts))   { $where .= " order by prate desc"; }
    
      $sql = $select . $from . $where;
      $statement = $pdo->prepare($sql);
      $statement->execute();
      $results = $statement->fetchAll(PDO::FETCH_ASSOC);
      $json = json_encode($results);
      echo($json);
    
    $allptype = array('Belt','Shoes');
    $selectedptype = array();
      foreach($allptype as $ptype){
        if(in_array($ptype,$opts)){
            $selectedptype[] = $ptype;
        }
      }
      if(count($selectedptype)){
        $where .= " AND ptype IN ('".implode("', '", $selectedptype)."')";
      }
    

    对我来说,这似乎太棒了。如果sombody有其他方法,请随时发布。

    是否有其他理由检查
    WHERE TRUE
    ?如果没有,这里就不需要这样做。您可以直接为
    Shoes
    belt
    添加
    WHERE
    子句,但对“WHERE TRUE”没有任何特定的内容。您的意思是希望得到
    Shoes
    belt
    的结果。对吗?请尝试使用IN子句,即“和ptypein”(“'.intlode(“'”,$opts)。”)“。请记住转义用户输入。@DavidLin,在
    $opts
    中,我还有另一个值。鞋子和皮带是其中之一。它还包括
    颜色
    。因此,在这种情况下,您的方法将失败。它是fyn@Ranjith。。请查看在运行时生成的查询。请让我知道如何在运行时合并此值。再次,这不是很好的方法。”ch..看看如果我在
    $opts
    中有3种颜色,我必须应用过滤器,然后我必须对所有3种颜色进行组合,一次有3种颜色,一次有2种颜色,然后一次有1种颜色..这将导致代码过长..难道没有其他选择吗approach@Gags:您的意思是,
    $opts
    将有2种或更多的可能性,对吗?是的Ranjith.它可以有任何值..你明白了吗right@Gags:查看您的数组包含一个字符串,如
    shoes
    Belt
    ,等等。然后您检查该字符串是否存在于数组中,您添加了
    WHERE
    子句,如
    $WHERE.=“ptype='shoes'”,
    WHERE.=“ptype='Belt”“
    。。等等,好的。这个
    ptype='Shoes'
    是您手动添加的,不是来自数据库。我认为没有其他方法可以实现这些目标。您只需要检查手册。是的,我希望当我同时检查这两项时,应显示皮带和鞋子的结果,并在运行时生成查询是的。你有最好的方法。还有一件事,
    foreach(
    迭代不需要一个。
    $opts
    已经是一个数组了。你可以直接将
    $opts
    传递到
    内爆
    。比如
    $where.=”和ptype-IN(“”。内爆(“,”,$opts)。“)”
    No Ranjith..我不能直接使用
    $opts
    ,因为
    $opts
    有产品类型、产品颜色、产品价格等,如果这样做,查询将失败..如果我错了,请纠正我here@ranjith…你知道通过ajax显示产品和过滤产品的其他方法吗。。