Reputation: 53
I have a problem. I want to display Item details, with notes, one Item data has several images that will appear. I display data in the following way:
I have a model and method like this:
class Items_model extends CI_Model
{
public function dataItemsDetail($id)
{
$this->db->select('
`items`.`id`, `items`.`item_code`, `items`.`item_name`, `category`.`name` AS
`category`, `items`.`price_unit`, `items`.`rating`, `images`.`image_name`,
`items`.`description`
');
$this->db->where('items.id', $id);
$this->db->join('items', 'items.category_id = category.id', 'right');
$this->db->join('images', 'images.item_code = items.item_code', 'left');
$this->db->from('category');
$query = $this->db->get();
return $query->row_array();
}
public function imagesItem($id)
{
$this->db->select('
`items`.`id`, `items`.`item_name`, `images`.`image_name`');
$this->db->where('items.id', $id);
$this->db->join('images', 'images.item_code = items.id');
$this->db->from('items');
$query = $this->db->get();
return $query->result_array();
}
}
This my controller:
public function itemsDetail($id)
{
$data['title'] = 'Items';
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
$data['itemDetail'] = $this->Items_model->dataItemsDetail($id);
$data['imagesItem'] = $this->Items_model->imagesItem($id);
$this->load->view('templates/header', $data);
$this->load->view('master/itemDetail', $data);
$this->load->view('templates/footer');
}
And this my view:
<div class="form-group row">
<label for="item_code" class="col-sm-2 col-form-label">Item Code</label>
<div class="col-sm-3">
<input type="text" class="form-control" value="<?= $itemDetail['item_code']; ?>" readonly>
</div>
</div>
<div class="form-group row">
<label for="item_name" class="col-sm-2 col-form-label">Item Name</label>
<div class="col-sm-6">
<input type="text" class="form-control" value="<?= $itemDetail['item_name']; ?>" readonly>
</div>
</div>
<div class="form-group row">
<label for="images" class="col-sm-2 col-form-label">Images</label>
<div class="col-sm-7">
<?php $i = 1; ?>
<?php foreach ($imagesItem as $imagesItem) : ?>
<?= $imagesItem['image_name']; ?>
<?php $i++; ?>
<?php endforeach; ?>
</div>
</div>
I want to show, item data with one row only, and i want to show multiple image using foreach in view. But that's is, error, the data cannot be show.
and i want to shwon in this view
please help me.. Thanks..
Upvotes: 3
Views: 49
Reputation: 7997
You have to restructure the item's query a little bit. The first query will return all "single" information you need for your form, the second query is (already) returning the related images:
change this in your controller:
public function dataItemsDetail($id)
{
$this->db->select('items.*, category.name AS category');
$this->db->where('items.id', $id);
$this->db->join('items', 'items.category_id = category.id', 'right');
$this->db->from('category');
$query = $this->db->get();
return $query->row_array();
}
public function imagesItem($id)
{
$this->db->select('items.id, items.item_name, images.image_name');
$this->db->where('items.id', $id);
$this->db->join('images', 'images.item_code = items.id');
$this->db->from('items');
$query = $this->db->get();
return $query->result_array();
}
note: the escaping is done by Codeigniter for you, do not use backticks ` in your function
tip: you can echo out $this->db->last_query();
and test the generated SQL in your phpMyAdmin
and in your view:
<div class="col-sm-7">
<?php foreach ($imagesItem as $img) : ?>
<?= $img['image_name']; ?>
<?php endforeach; ?>
</div>
Upvotes: 2