I would pass a variable to the title function argument in Phil Sturgeon template library from database.
I have these files:
Controller
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');
}
}
}
Model
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();
}
}
View
<?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; ?>
Now when I load the template I would pass the title of the post to the first argument of the title function in the template library (in order to set the tag of the page like the post title)
If I link a page (ex http://localhost/blog/index.php/blog/post/3)
the print_r function in views print this result
Array (
[0] => Array (
[id] => 3
[name] => Fra Ore
[password] => 123456
[email] => [email protected]
[title] => Very simple title!
[content] => bla bla bla
[user_id] => 1)
) 1
What must I put inside the title function?
I tried a lot...
$this->template
->title($data[0][title])
->build('blog/post', $data);
but returns 2 Notice
Use of undefined constant title - assumed 'title'
and
Message: Undefined offset: 0
in controllers/blog.php
Ideas?