0

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:

enter image description here

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.

3
  • just don't use the shopId from the result set.either use select statement to choose specific columns Commented Jul 30, 2013 at 12:41
  • Thanks for your comment. Your answer does not really help me, maybe I should specificy my question. Can I make a query which gets all the data from productshop (with the restriction of the productId) and gets the needed shopName from shop? Commented Jul 30, 2013 at 12:44
  • @Orhan, Check my answer, If you want I can extend it more all the example. Commented Jul 30, 2013 at 12:55

3 Answers 3

3

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Very cool, thanks, that worked! But how can that work if you do not select shopName? In the profiler I see the query build like: SELECT * FROM (productshop) JOIN shop ON productshop.shopId = shop.shopId WHERE productshop.productId = 116774
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.