我将数据从Controller传递到View时遇到问题.
我使用Ajax来执行此操作,您可以在此处查看我的Ajax代码:
$(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);
})
});
});
点击HTML标签时我会得到id_thang li>胜.
在Controller / Home.PHP中,我将获得有关此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();
}
所有数据都存储在数组$get_image_thang中.
最后,我不知道如何将此数组传递给View显示我选择的所有数据.
在View / index.PHP中,我正在尝试使用此数组中的所有数据进行foreach循环,并在< html>中显示标签.像这样:
<?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 ?>
问题是我不知道如何将$get_image_thang发送到此视图.
更新1:
我试着把:console.log(结果);到.done(函数(结果)事件并收到如下结果:
问题是:我使用row = result [i] .id;或任何属性,如id,name,image_list未定义.
更新2:
只有两个函数来获取id的信息.我在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;
}
在控制器,我调用get_info.注意:
Mmenushoatnao是数据库中的模型映射.
更新3:
我只知道在Controller中编写代码以获取点击事件后的数据.
像这样:
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);
}
当然,这段代码不起作用.
所以,我们需要转换为Ajax代码.
解决方法:
试试这个
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);
echo json_encode($get_image_thang);
}
现在在ajax中(假设你从get_info()方法返回对象)
//....
.done(function(result) {
var row = '';
for (var i = 0; i < Object.keys(result).length; i++) {
row += result[i].id;
}
$('#res').html(row);
})
<div id="res" class="col-md-4 col-sm-4 col-xs-4">
</div>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。