简体   繁体   中英

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. I have the following tables in my MySQL database that are relevant:

在此处输入图片说明

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. My problem is, what is the best wayto show shopName, instead of showing shopId. Taking in regards that $bookshop should be as it is (except of shopid), because I am creating a HTML table with product data.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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