簡體   English   中英

Codeigniter在另一個查詢中使用查詢結果的一部分

[英]codeigniter use part of query result in another query

很難拿出一個標題。 我正在將CodeIgniter與模型/視圖/控制器一起使用。 我的MySQL數據庫中有相關的下表:

在此處輸入圖片說明

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

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();

我認為我正在開設$ bookshop。 我的問題是,什么是顯示shopName而不是顯示shopId的最佳方法? 考慮到$ bookshop應該保持原樣(shopid除外),因為我正在使用產品數據創建HTML表。

試試這樣:

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

忽略活躍的Codeigniter 以獲取功能的詳細信息

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

模型:

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

}

視圖:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM