【问题标题】:CodeIgniter: How to join two table columns and use like active record for querying both?CodeIgniter:如何连接两个表列并使用类似活动记录来查询两者?
【发布时间】:2013-04-05 04:15:38
【问题描述】:
public function search($query, $limit) {
    // output
    $output = "";
    // query
    $this->db->select("*");
    $this->db->from("products");
    $this->db->like("brand", $query);
    $this->db->or_like("model", $query);
    $this->db->limit($limit);
    $query = $this->db->get();
    // zero
    if ($query->num_rows() == 0) {
        $output .= "No results found";
        return $output;
    }
    // result
    foreach ($query->result_array() as $row) {
        // uri
        $uri2 = "{$this->base_url}specifications/{$row['brand']}-{$row['model']}";
        $uri2 = str_replace(" ", "-", $uri2);
        // price format
        $row['price'] = number_format($row['price']);
        // image url
        $image = "{$this->base_url}assets/images/products/{$row['category']}-{$row['brand']}-{$row['model']}/thumb.png";
        $image = str_replace(" ", "-", $image);
        $output .= "<ul class=\"product\">\n
            <a href=\"{$uri2}\">\n
                <li class=\"product-image\"><img src=\"{$image}\" height=\"108\" width=\"57\" alt=\"\"></li>\n
                <li class=\"product-brand\">{$row['brand']}</li>\n
                <li class=\"product-model\">{$row['model']}</li>\n
                <li class=\"product-price\">{$row['price']} <span class=\"green\">pkr</span></li>\n
            </a>\n
        </ul>\n";
    }
    // return
    return $output;
}

这里我做了三种查询:

1- 仅包含品牌的查询(我得到结果)
2- 仅包含模型的查询(我得到结果)
3- 包含两者的查询(我没有得到结果)

如何完成以上三项任务?

【问题讨论】:

    标签: codeigniter search activerecord


    【解决方案1】:

    您似乎正在执行某种搜索,因此您想要获得的结果来自控制器或视图,您只需这样做

    if ($brand_val != '' )
    {  //put your where query here }
    
    if ($model_val  != '' )
    {  //put your where query here }
    
    if ($brand_val != '' && $model_val  != ''   )
    {  //put your where query here }
    

    希望这能给你一些想法

    【讨论】:

      【解决方案2】:

      使用它来打印查询。

      echo $this->db->last_query();

      然后尝试在您的 phpmyadmin 中执行该查询

      【讨论】:

        猜你喜欢
        • 2018-09-19
        • 1970-01-01
        • 2020-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-05
        • 1970-01-01
        相关资源
        最近更新 更多