如何将查询附加到以前选择的查询?

时间:2017-03-23 05:40:07

标签: php mysql codeigniter-3

public function gettopfilteredcourse($filtertype, $topfiltername, $filterid){
    $sql="SELECT * FROM course c
          INNER JOIN category s
          ON c.categoryId=s.categoryId
          INNER JOIN users u
          ON c.userId=u.userId";

  if($topfiltername == 'trash'){
     $sql .= " WHERE isCompleted='3'";
  } else if($topfiltername == 'forapproval'){
     $sql .= " WHERE isCompleted='1'";
  } else if($topfiltername == 'drafts'){
     $sql .= " WHERE isDraft='1'";
  } else {
     $sql .= " WHERE isCompleted='2'";
  }

  if($filtertype == 'free'){
     $sql .= " AND c.fee=''";
  } else if($filtertype == 'paid'){
     $sql .= " AND c.fee!=''";
  } else {
     $sql .= " AND c.categoryId='$filterid' ";
  } 
   $query=$this->db->query($sql);
   return $query;
}

在第一个if条件$topfiltername == 'forapproval'被选中时,它正确执行。如果条件为$filtertype == 'paid',则为第二个,我希望将其附加到$topfiltername == 'forapproval',但它始终附加第一条条件if语句$sql .= " WHERE isCompleted='2'";。怎么办请帮助我。

filter

1 个答案:

答案 0 :(得分:0)

如果您正在使用codeigniter.use数据库库。 codeigniter.com/userguide3/database/query_builder.html