[英]Codeigniter pass array from controller to model, But not returning a matching result
[英]Codeigniter 2 not returning from model as object only as result array
因此,我使用Codeigniter已有一段时间了,在我的模型中运行查询并将其返回到控制器后再将其传递到我的视图中时,我从未遇到任何问题,在该视图中我可以将其作为对象进行访问,如下所示:
模型(somemodel)
function getdata()
{
$query = $this->db->get('sometable');
return $query;
}
控制器(somecontroller)
function controldata()
{
$this->load->model('somemodel');
$data['dbdata'] = $this->somemodel->getdata();
$this->load->view('someview',$data);
}
查看(someview)
<?php
foreach($dbdata->result() as $row) {
echo $row->id;
echo $row->name;
echo $row->whatever;
echo "<br />";
}
?>
但是现在无论出于什么原因,在2.0中,它都将$ dbdata-> result_array()用作$ row。
基本上,它以数组形式返回结果。
这没有任何意义,新版本中是否有所更改?
谢谢,
所以这是我的代码:
型号(admin_model)
function show_allproducts($sort,$order,$limit,$offset)
{
$this->db->select('products.id,productcategories.categoryname,products.name,products.internetspecial,products.added,products.modified');
$this->db->from('products');
$this->db->join('productcategories','products.productcategories_id = productcategories.id');
$this->db->order_by($sort,$order);
$this->db->limit($limit,$offset);
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query;
}
else
{
return FALSE;
}
}
控制器(管理员)
function show_allproducts()
{
$data['title'] = 'All Products';
$this->load->library('pagination');
$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/admin/show_allproducts';
$config['total_rows'] = $this->db->get('products')->num_rows();
$config['per_page'] = 10;
$config['num_links'] = 5;
$config['full_tag_open'] = '<div class="paggination right">';
$config['full_tag_close'] = '</div>';
$config['prev_link'] = '< prev';
$config['next_link'] = 'next >';
$config['cur_tag_open'] = '<a class="active">';
$config['cur_tag_close'] = '</a>';
$this->pagination->initialize($config);
$this->load->model('admin_model');
$data['tabledata'] = $this->admin_model->show_allproducts('name','ASC',$config['per_page'],$this->uri->segment(3));
$data['nav'] = 'admin/pagesections/nav';
$data['maincontent'] = 'admin/pages/show_allproducts';
$this->load->view('admin/template',$data);
}
VIEW(show_all_products)
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<?php if($tabledata) { ?>
<tr>
<th>ID</th>
<th>CATEGORY</th>
<th>NAME</th>
<th>ADDED</th>
</tr>
<?php foreach($tabledata->result() as $row) { ?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->categoryname; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->added;) ?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td>NO DATA</td>
</tr>
<?php } ?>
</table>
除非您将$ dbdata-> result_array()指定为$ row ,否则不会将结果作为数组返回。 您发布的相同代码必须在CI 2.0中有效。
如果不是这样,可能是$ this-> db-> get('sometable')的问题 。
试试$ this-> db-> query('select * from sometable') 。 但据我所知,两者都是相同的...
好的,伙计们,不要撒谎,我发现了这个令人毛骨悚然的问题。
查看第四个td,其中$ row->增加了; 在它的末尾有一个紧密的括号,造成了四天的混乱。
致以最诚挚的问候。 有时候,我猜多少眼睛都没关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.