2013-05-05 36 views
0

我需要加入一個表格並從該表格中扣除position和pricebycat1,但我無法弄清codeigniter上的函數。在get_where codeigniter上加入表格

這是我的功能檢索產品。

function get_product($id, $related=true) 
{ 
    $result = $this->db->get_where('products',array('id'=>$id))->row(); 
    // get position, pricebycat1, join category_products 'category_products.product_id=products.id' 
    if(!$result) { 
     return false; 
    } 
    $related = json_decode($result->related_products); 
    if(!empty($related)) { 
     //build the where 
     $where = false; 
     foreach($related as $r) { 
      if(!$where) { 
        $this->db->where('id', $r); 
      } else { 
        $this->db->or_where('id', $r); 
      } 
      $where = true; 
     } 
     $result->related_products = $this->db->get('products')->result(); 
    } else { 
     $result->related_products = array(); 
    } 
    $result->categories = $this->get_product_categories($result->id); 
    // group discount? 
    if($this->group_discount_formula) { 
     eval('$result->price=$result->price'.$this->group_discount_formula.';'); 
    } 
    return $result; 
} 

任何幫助表示讚賞。

+0

請更清楚您的問題。 – Aurel 2013-05-05 12:20:38

+0

是的,這真的很糟糕的代碼,真的很難找出你想要達到的目標。讓我們知道你的數據庫結構是什麼以及你如何將這些產品連接在一起,因爲必須有更好的方法。 WTF與'eval()'也是?? – 2013-05-05 23:22:54

回答

0

您只能使用get_where()不能加入其他表格。 嘗試使用此方法提取數據

$this->db->select('*.products, category_products.position, category_products.price'); 
$this->db->from('products'); 
//join LEFT by default 
$this->db->join('category_products', 'category_products.product_id=products.id'); 
$this->db->where('products.id = '.$id); 
$this->db->row(); 
+0

不正確 - 你可以在'get_where()'之前做'join()'就好了。只是不確定這是否適合OP。 – 2013-05-05 23:23:28

+0

看看如何在CI中實現'get_where()'功能。它不會工作。你能舉一個例子嗎? – 2013-05-06 07:01:44

+0

正如我所說的,在'get_where()'之前加'join()'。不,你不能單獨使用get_where來連接,但是你可以在get_where之前鏈接活動記錄。 – 2013-05-06 08:40:34