像查询不能正常工作

时间:2015-06-23 10:28:20

标签: codeigniter codeigniter-2 codeigniter-datamapper

在Code igniter模型中,我使用类似查询来获取所有具有名字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(&#39; name&#39;)时不起作用控制器,它工作正常。

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

}

4 个答案:

答案 0 :(得分:1)

数据库GET函数仅在SQL语句中创建LIKE。您之后仍然需要致电$this->db->like('name', $id); $q = $this->db->get(); 方法;像这样;

Update tblTempChek Set TmpCheckIn='15:50:03' 
Where TempID IN (
    Select MAX(TempID) 
    From tblTempChek 
    Where convert(date, TmpDate)='2015-06-23' AND UserID='1'
)

答案 1 :(得分:0)

您正在错误地使用$ this-&gt; db_like()。

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

(来自文档)

在您的情况下,用法是

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

More info

答案 2 :(得分:0)

public function fetchdeal_products($id) 
{
    $query = $this->db->query("Select products.* From products Where (products.name Like '%$id%')");
    $result = $query->result_array();
    return $result;
}

答案 3 :(得分:0)

你必须把它写成吼叫。

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