gpt4 book ai didi

php - 如何从 Codeigniter 中的 Controller 调用模型?

转载 作者:搜寻专家 更新时间:2023-10-31 21:40:28 25 4
gpt4 key购买 nike

我想要一个网页,主要内容由ajax View 使用。和一个菜单边栏。

我的应用程序 View 文件夹是

+pages
-home
templates
-header
-footer

我的主要页面 Controller 是:

<?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);

}

}

我的服务模型是:

<?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;
}
}
}

我的观点是:

<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ı &rarr;</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,没有问题。你能告诉我正确工作的方法吗?

最佳答案

只需使用一个专用的 Controller (ajax) 来处理您所有的ajax 调用..

通用 Controller

class Ajax_Controller extends CI_Controller {
public function index(){
// Add the logic which you want to share among all ajax calls
// like doing security check and all
}
}

扩展通用 Controller 以处理特定请求

class <SomeName>Ajax_Controller extends Ajax_Controller {
public function <some_action>(){
// write the request specific logic here.
}
}

关于php - 如何从 Codeigniter 中的 Controller 调用模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11754464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com