Codeigniter 将数据从模型传递到控制器解析器
我即将在我的 codeigniter 项目中实现解析器类,并且希望获得一些将数据从我的模型传递到解析器数组的指导。这是更好更有效的方法。
我的模型获取数据并将其返回给控制器。如何将其放入控制器中的数组中?
模型:
function getSeo($data){
$this->db->select('seo_title, seo_description, seo_keywords');
$this->db->from('content');
$this->db->where($data);
$query = $this->db->get();
$data = array();
foreach ($query->result() as $row) {
$data[] = array(
'seo_title' => $row->seo_title,
'seo_description' => $row->seo_description,
'seo_keywords' => $row->seo_keywords
);
}
return $data;
}
控制器:
$viewdata['pageseo'] = $this->Content_model->getSeo($data);
$viewdata = array(
'seo_title' => 'My seo Title',
'seo_description' => 'My seo description',
'seo_keywords' => 'My seo keywords',
);
将模型中的数据获取到“$viewdata”数组的最佳方法是什么,是如何完成的???
I am about to implement the parser class to my codeigniter project and would like some guidance in passing the data from my model to the parser array. Which is the better more efficient way to do it.
My model gets the data and returns it to the controller. How can I get it into the array in the controller?
Model:
function getSeo($data){
$this->db->select('seo_title, seo_description, seo_keywords');
$this->db->from('content');
$this->db->where($data);
$query = $this->db->get();
$data = array();
foreach ($query->result() as $row) {
$data[] = array(
'seo_title' => $row->seo_title,
'seo_description' => $row->seo_description,
'seo_keywords' => $row->seo_keywords
);
}
return $data;
}
Controller:
$viewdata['pageseo'] = $this->Content_model->getSeo($data);
$viewdata = array(
'seo_title' => 'My seo Title',
'seo_description' => 'My seo description',
'seo_keywords' => 'My seo keywords',
);
What is the best way to get the data from my model into the '$viewdata' array, how is it done????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于模型中的 getSeo 函数返回一个数组,因此控制器也会将此信息存储到您的 $viewdata 数组中。
如果您尝试
print_r($viewdata)
您会发现结构符合预期。这是$viewdata['seo_title'] => “我的 seo 标题”
等等...
Since the getSeo function from your model returns an array, the Controller will store this information to your $viewdata array as well.
If you try a
print_r($viewdata)
you'll see that the structure is as expected. Which is$viewdata['seo_title'] => 'My seo Title'
and so on...
有一个名为
result_array()
的函数,用于将结果集获取为数组,下面的链接可能会对您有所帮助。这是核心库函数。请参考,
There is function called
result_array()
which is used to get the result set as array and the below link may help you. This is the core library function.Plz refer,