douju6542 2016-08-09 07:06
浏览 206

在mysql中使用多个where条件

i have a select field named categories[] with multiple select and i written one on change function so on change fuction it will go the controller as an array of id's now what i want is i want to select foods from mysql table with these categories

<select name="categories[]">
    <?php 
      foreach ($category as $c) {

    ?>
     <option value="<?php echo $c->category_id; ?>"><?php echo $c->category_name;?></option>
    <?php
       }

     ?>
</select>

controller

    public function get_foods(){

       $categories = $this->input->post(NULL,true);
       $sql = $this->subscription->get_foods($categories);
       $result = array(

                    'result' => $sql

       );
      echo json_encode($result);
    }

and model

    public function get_foods($id){

    $sql = "SELECT * FROM food_category fc
            LEFT JOIN food f ON fc.food_id=f.food_id
            WHERE f.food_status = 1 AND
            fc.category_id = $id
            ORDER BY f.food_id DESC";
    $query = $this->db->query($sql);
    return $query->result_array();

}

i want to fetch all the foods in these categories, so i think i should have to use some multiple where condition ?

  • 写回答

3条回答 默认 最新

  • dprntkxh703029 2016-08-09 07:09
    关注

    You just need to store category with each food and when you get the foods you can use a where condition :

    $this->db->where('category',$category);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable