データベースから 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
タイトル関数の中に何を入れる必要がありますか?
いろいろ試した...
$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
アイデア?