如何在视图或另一个控制器中调用控制器方法?
我有主控制器,用于打印主页。
<?
class Main extends Controller {
function Main()
{
parent::Controller();
$this -> load -> helper('date');
}
function index()
{
$this -> load -> view('header');
$this -> load -> view('main');
$this -> load -> view('footer');
}
}
?>
我有文章控制器,打印最后 6 篇文章。
<?php
class Articles extends Controller
{
function Articles()
{
parent::Controller();
}
function top()
{
$this -> db -> limit(0, 6);
$query = $this -> db -> get('articles');
$data['articles'] = $query -> result();
$this -> load -> view('articles-top', $data);
}
?>
标题视图如下所示:
<html>
<head>
...
</head>
<body>
<div id="last-articles">
<!-- Here I want print last 6 articles -->
</div>
<div id="content">
如何在标题视图中打印最后一篇文章?
I have main controller, that print main page.
<?
class Main extends Controller {
function Main()
{
parent::Controller();
$this -> load -> helper('date');
}
function index()
{
$this -> load -> view('header');
$this -> load -> view('main');
$this -> load -> view('footer');
}
}
?>
And I have controller of articles, that print 6 last articles.
<?php
class Articles extends Controller
{
function Articles()
{
parent::Controller();
}
function top()
{
$this -> db -> limit(0, 6);
$query = $this -> db -> get('articles');
$data['articles'] = $query -> result();
$this -> load -> view('articles-top', $data);
}
?>
Header view looks like this:
<html>
<head>
...
</head>
<body>
<div id="last-articles">
<!-- Here I want print last 6 articles -->
</div>
<div id="content">
How can I print last articles in header view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$data 数组中的数组键成为视图中的变量。
Code Igniter 用户指南非常有用。
在你看来,你可以做这样的事情:
The array keys in your $data array become variables in the view.
The Code Igniter User Guide is very useful.
You can do something like this in your view:
你都做错了。
您的控制器需要具有这样命名的函数...
您的视图可以使用 $data 变量。
希望这有帮助,但我会推荐这个网站..
http://kerkness.ca/wiki/doku.php
这对我来说非常宝贵。
祝你好运!
You are doing it all wrong.
Your controllers need to have functions named like this...
Your view can use the $data variable.
Hope this helps, but I would suggest this site..
http://kerkness.ca/wiki/doku.php
It's been invaluable to me.
good luck!