繁体   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