I have a problem with passing data from Controller to View.
I am using Ajax to do this, you can see my Ajax code here:
$(document).ready(function(){
$('li.thang').click(function(){
var id_thang = $(this).attr('value');
$.ajax({
url: baseUrl+'/Home/getImage',
dataType: 'json',
type: 'POST',
data: {id_thang: id_thang},
}).done(function(result) {
console.log(result.get_list_image_thang);
})
});
});
I will get id_thang when clicking in HTML tags li > thang.
At Controller / Home.php, I will get an array data on this id_thang.
function getImage(){
$id_thang = $this->input->post('id_thang');
$input = array();
$input['order'] = array('id','ASC');
$get_image_thang = $this->Mmenushoatnao->get_info($id_thang);
ob_start();
}
All data is stored in array $get_image_thang.
Finally, I don't know how to pass this array to View show all data I selected.
In View/index.php I am trying a foreach loop through all data in this array and show in <html> tag. Like this:
<?php foreach($get_image_thang AS $item) ?>
<div class="col-md-4 col-sm-4 col-xs-4">
<?php echo $item->id; ?>
</div>
<?php endforeach ?>
Note: at View / index.php is demo code.
Problem is I don't know how to send $get_image_thang to this View.
Update 1:
I tried to put: console.log(result); to .done(function(result) event and receive result like this:
Problem is: I use row += result[i].id; or any property like id, name, image_list is not undefined.
Update 2:
Only two function to get info base on id. I write all code in core/MY_MODEL.php:
function get_info($id, $field = '')
{
if (!$id)
{
return FALSE;
}
$where = array();
$where[$this->key] = $id;
return $this->get_info_rule($where, $field);
}
function get_info_rule($where = array(), $field= '')
{
if($field)
{
$this->db->select($field);
}
$this->db->where($where);
$query = $this->db->get($this->table);
if ($query->num_rows())
{
return $query->row();
}
return FALSE;
}
At controller, I call get_info. Note:
Mmenushoatnao is a Model maps in database.
Update 3:
I only know write code in Controller to get data after click event.
But like your question mention. Must write all code in Ajax code.
Like this:
function getImage(){
$id_thang = $this->input->post('id_thang');
$input = array();
$input['order'] = array('id','ASC');
$get_image_thang = $this->Mmenushoatnao->get_info($id_thang);
ob_start();
?>
<?php foreach($get_image_thang as $item): ?>
<?php $image_list = json_decode($item->image_list); ?>
<?php foreach($image_list as $image): ?>
<div class="col-md-4 col-sm-4 col-xs-4">
<img src="<?php echo upload_url() ?>/img/hoatnao/hinhanh/<?php echo $image ?>" alt="Image" class="img-responsive">
</div>
<?php endforeach; ?>
<?php endforeach; ?>
<?php
$return = ob_get_clean();
$data['result']=$return;
echo json_encode($data);
}
Of course, this code is not working.
So, we need convert to Ajax code.

$this->Mmenushoatnao->get_info($id_thang);show thisget_info()method codeget_info()function.