简体   繁体   English

Codeigniter在另一个查询中使用查询结果的一部分

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

It was hard to come up with a title. 很难拿出一个标题。 I am using CodeIgniter with models/views/controller. 我正在将CodeIgniter与模型/视图/控制器一起使用。 I have the following tables in my MySQL database that are relevant: 我的MySQL数据库中有相关的下表:

在此处输入图片说明

In my model I have the following function: 在我的模型中,我具有以下功能:

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

In my controller I use the above function like 在我的控制器中,我使用上述功能,例如

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

In my view I am foreaching $bookshop. 我认为我正在开设$ bookshop。 My problem is, what is the best wayto show shopName, instead of showing shopId. 我的问题是,什么是显示shopName而不是显示shopId的最佳方法? Taking in regards that $bookshop should be as it is (except of shopid), because I am creating a HTML table with product data. 考虑到$ bookshop应该保持原样(shopid除外),因为我正在使用产品数据创建HTML表。

Try some like this: 试试这样:

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

get an overlook to active class of codeigniter for details of functions 忽略活跃的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();
}

Model: 模型:

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

Controller: 控制器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM