如何从Codeigniter中的控制器调用模型? [英] How to call model from controller in Codeigniter?
本文介绍了如何从Codeigniter中的控制器调用模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个网页,其主要内容被ajax视图使用。和一个菜单边栏。
I want a webpage , which main content is used by ajax view. and a menu sidebar.
我的应用程式资料检视资料夹是
my application views folder is
+pages
-home
templates
-header
-footer
页面控制器是:
<?php
class Pages extends CI_Controller {
public function view($page = 'home')
{
$this->load->model('services_model');
$data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
我的service_model是:
My service_model is:
<?php
class Services_model extends CI_Model {
function getAll() {
$q = $this->db->get('services');
if($q->num_rows() > 0){
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
}
/ p>
And my view is :
<ul class="blog-medium">
<?php foreach($records as $row);?>
<li>
<div class="blog-medium-text">
<h1><a href="./post.html"><?php echo $row->title; ?></a></h1>
<p class="blog-medium-excerpt">
<?php echo $row->content; ?> <br />
<a href="./post.html" class="read_more">Devamı →</a></p>
</div>
<div class="blog-medium-text"><p class="blog-info">
<img src="./images/icon-time.png" alt="" />March 14, 2012
<img src="./images/sep.gif" alt="" /><img src="./images/icon-comment.png" alt="" />0 Yorum</p>
</div></li>
<?php endforeach;?>
所以我的问题是在代码中实现service_model,没有问题。
so my problem is in implementing service_model in the code., there is no problem . Can you show me a way to work correctly?
推荐答案
只需使用一个专用的控制器(ajax)对于所有的ajax调用..
Just use one dedicated controller(ajax) for all your ajax calls..
在ajax控制器中加载该模型并使用
Load that model in ajax controller and use
这篇关于如何从Codeigniter中的控制器调用模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文