【问题标题】:Like Query Is Not Working properly像查询不能正常工作
【发布时间】:2015-06-23 10:28:20
【问题描述】:

在代码点火器模型中,我使用类似查询来获取所有名称为 rice 的产品,但使用 get_where('name') 时控制器不起作用。

  public function fetchdeal_products($id) {
        $this->db->select('*');
        $this->db->from('products');
        $q = $this->db->like("name", $id);

        if ($q->num_rows() > 0) {
        foreach (($q->result()) as $row) {
        $data[] = $row;
        }

        return $data;
         }    
        }

我正在使用类似查询来获取所有名称为 rice 的产品,但使用 get_where('name') 时控制器不起作用。 //控制器

        function ajaxdealcategorydata($id = NULL) {
        $this->sma->checkPermissions('index');
        $id = $this->input->get('id');

        $subcategories = $this->pos_model->getdealcategory($id);
        $scats = '';
        foreach ($subcategories as $category) {
        $scats .= "<button id=\"subcategory-" . $category->id . "\"    type=\"button\" value='" . $category->id . "' class=\"btn-prni subcategory\" ><img src=\"assets/uploads/thumbs/" . ($category->image ? $category->image : 'no_image.png') . "\" style='width:" . $this->Settings->twidth . "px;height:" . $this->Settings->theight . "px;' class='img-rounded img-thumbnail' /><span>" . $category->name . "</span></button>";
  }

$products = $this->ajaxdealproducts($id);

if (!($tcp = $this->pos_model->products_count($id))) {
    $tcp = 0;
}

echo json_encode(array('products' => $products, 'subcategories' => $scats, 'tcp' => $tcp));

}

我正在使用类似查询来获取所有名称为 rice 的产品,但使用 get_where('name') 时控制器不起作用。

   function ajaxdealproducts() {
     $this->sma->checkPermissions('index');
     if ($this->input->get('id') ) {
    $id = $this->input->get('id');
    } else {
    $category_id = $this->pos_settings->default_category;
    }
    if ($this->input->get('subcategory_id')) {
    $subcategory_id = $this->input->get('subcategory_id');
    } else {
    $subcategory_id = NULL;
    }
    if ($this->input->get('per_page') == 'n') {
    $page = 0;
    } else {
    $page = $this->input->get('per_page');
}

     $this->load->library("pagination");

     $config = array();
     $config["base_url"] = base_url() . "pos/ajaxdealproducts";
     $config["total_rows"] = $subcategory_id ? $this->pos_model-    >products_count($id, $subcategory_id) : $this->pos_model->products_count($id);
     $config["per_page"] = $this->pos_settings->pro_limit;
     $config['prev_link'] = FALSE;
     $config['next_link'] = FALSE;
     $config['display_pages'] = FALSE;
     $config['first_link'] = FALSE;
     $config['last_link'] = FALSE;

     $this->pagination->initialize($config);

    $products = $this->pos_model->fetchdeal_products($id, $config["per_page"], $page);
    $pro = 1;
    $prods = '<div>';

    foreach ($products as $product) {
    $count = $product->id;
    if ($count < 10) {
        $count = "0" . ($count / 100) * 100;
    }
    if ($category_id < 10) {
        $category_id = "0" . ($category_id / 100) * 100;
    }

    $prods .= "<button id=\"product-" . $category_id . $count . "\" type=\"button\" value='" . $product->code . "' title=\"" . $product->name . "\" class=\"btn-prni btn-" . $this->pos_settings->product_button_color . " product pos-tip\" data-container=\"body\"><img src=\"" . base_url() . "assets/uploads/thumbs/" . $product->image . "\" alt=\"" . $product->name . "\" style='width:" . $this->Settings->twidth . "px;height:" . $this->Settings->theight . "px;' class='img-rounded' /><span>" . character_limiter($product->name, 15) . "</span></button>";

    $pro++;
}

$prods .= "</div>";

if ($this->input->get('per_page')) {
    echo $prods;
} else {
    return $prods;
}

}

【问题讨论】:

    标签: codeigniter codeigniter-2 codeigniter-datamapper


    【解决方案1】:

    数据库LIKE 函数只在SQL 语句中创建一个LIKE。之后你仍然需要调用GET 方法;像这样;

    $this->db->like('name', $id);
    $q = $this->db->get();
    

    【讨论】:

      【解决方案2】:

      你使用 $this->db_like() 不正确。

      $this->db->like('title', 'match'); 
      // Produces: WHERE title LIKE '%match%'
      

      (来自文档)

      在你的情况下,用法是

      $this->db->like('name', 'rice')
      

      More info

      【讨论】:

        【解决方案3】:
        public function fetchdeal_products($id) 
        {
            $query = $this->db->query("Select products.* From products Where (products.name Like '%$id%')");
            $result = $query->result_array();
            return $result;
        }
        

        【讨论】:

          【解决方案4】:

          你必须像下面这样写。

          public function fetchdeal_products($id) {
              $this->db->like("name", $id);
          
              $q = $this->db->get('products');
              if ($q->num_rows() > 0) {
                  foreach (($q->result()) as $row) {
                      $data[] = $row;
                  }
          
                  return $data;
              }    
          }
          

          【讨论】:

            猜你喜欢
            • 2015-09-29
            • 2017-06-06
            • 1970-01-01
            • 2014-03-30
            • 2012-04-10
            • 2020-04-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多