2013-07-30 77 views
0

很難想出一個標題。我在模型/視圖/控制器中使用CodeIgniter。我在我的MySQL數據庫下面的表格是相關的:codeigniter在另一個查詢中使用查詢結果的一部分

enter image description here

在我的模型,我有以下功能:

function get_shoptable() { 
    $this->db->from('productshop')->where('productId', $this->productId); 
    $query = $this->db->get(); 
    return $query->result(); 
} 

在我控制我用上面的函數一樣

$data['bookshop'] = $this->Product_model->get_shoptable(); 

在我看來,我正在推銷$書店。我的問題是,顯示shopName的最佳方式是什麼,而不是顯示shopId。考慮到$書店應該保持原樣(shopid除外),因爲我正在創建一個包含產品數據的HTML表格。

+0

只是不使用shopId從結果set.either使用SELECT語句來選擇特定的列 –

+0

感謝您的評論。你的回答並沒有真正幫助我,也許我應該明確我的問題。我可以做一個查詢,從producthop獲取所有數據(限制productId)並從商店獲取所需的shopName? – Orhan

+0

@或者,檢查我的答案,如果你想我可以擴展它所有的例子。 –

回答

3

嘗試一些類似這樣的:

function get_shoptable() { 
    $this->db->from('productshop') 
    ->join('shop', 'productshop.shopId = shop.shopId') 
    ->where('productshop.productId', $this->productId); 
    $query = $this->db->get(); 
    return $query->result(); 
} 
+0

非常酷,謝謝,工作!但是如果你不選擇shopName,那該如何工作呢?在探查我看到查詢樣訂做: SELECT * FROM ('productshop') JOIN'shop' ON'productshop'.'shopId' ='shop'.'shopId' 其中'productshop'.' productId' = 116774 – Orhan

+0

接受答案,如果它的工作! :d –

1

得到一個俯瞰到笨的active class的功能細節

function get_shoptable() 
{ 
    $this->db->from('productshop') 
    $this->db->join('shop', 'productshop.shopId = shop.shopId') 
    $this->db->where('productshop.productId', $this->productId); 
    $query = $this->db->get(); 
    return $query->result(); 
} 
1

型號:

function get_products() { 
    $this->db->select('productshop.productUrl, productshop.price, productshop.deliveryTime, productshop.shippingCast, productshop.inventory, productshop.productId, productshop.shopId, shop.shopName'); 
    $this->db->from('productshop'); 
    $this->db->join('shop', 'productshop.shopId = shop.shopId'); 
    $this->db->where('productshop.productId', $this->productId); 
    return $this->db->get()->result_array(); 
} 

控制器:

function products() { 
    $data['products'] = $this->model_name->get_product(); 
    $this->load->view('products', $data); 

} 

VIEW:

<?php foreach($products as $p): ?> 
<h1><?php echo $p['productUrl']; ?></h1> 
<h1><?php echo $p['shopName']; ?></h1> 
<?php endforeach(); ?> 
相關問題