我会从数据库中将一个变量传递给 Phil Sturgeon 模板库中的 title 函数参数。
我有这些文件:
控制器
class Blog extends CI_Controller {
public function post($id)
{
$this->load->model('blog_model');
$data['post'] = $this->blog_model->get_post($id);
$data['comments'] = $this->blog_model->get_post_comments($id);
if($data['post'])
{
$this->template
->title(?????????) <- HERE IS THE PROBLEM!
->build('blog/post', $data);
}
else
{
$this->flash_message->error('Post doesn't exist');
redirect('blog');
}
}
}
模型
class Blog_model extends CI_Model {
function get_post($id)
{
// Join with user's table
// to get the name of the author
$this->db->join('posts', 'users.id = posts.user_id')
// THIS IS VERY HUGLY, But It's an other problem!
->where('posts.id', $id);
return $this->db->get('users')->result_array();
}
}
看法
<?php echo print_r($post); ?>
<br><br>
<?php echo print_r($comments); ?>
{post}
<h1>{title}</h1>
<i>By {name}</i>
<div class="post_body">{content}</div>
{/post}
<h2>Comments</h2>
<?php if($comments): ?>
{comments}
<h2>{commenter}</h2>
<div>{content}</div>
{/comments}
<?php else: ?>
<p>No comment...</p>
<?php endif; ?>
现在,当我加载模板时,我会将帖子的标题传递给模板库中 title 函数的第一个参数(以便设置页面的标签,如帖子标题)
如果我链接一个页面(例如http://localhost/blog/index.php/blog/post/3
)print_r
视图中的函数打印这个结果
Array (
[0] => Array (
[id] => 3
[name] => Fra Ore
[password] => 123456
[email] => fra@ore.com
[title] => Very simple title!
[content] => bla bla bla
[user_id] => 1)
) 1
我必须在 title 函数中放什么?
我试了很多...
$this->template
->title($data[0][title])
->build('blog/post', $data);
但返回 2 通知
Use of undefined constant title - assumed 'title'
和
Message: Undefined offset: 0
在controllers/blog.php
想法?