1
votes

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?

3

3 Answers

0
votes

i think problem is here

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')->row_array();
                                //--^^^^^^^^^---- here since is think this returns just a single row.
  }
}

 if($data['post'])
    {
        $this->template
        ->title($data['post']['title'])  //<--- HERE
        ->build('blog/post', $data);
    }

if not then i guess you need to use loop to get the title there..

0
votes

Try this:

if($data['post'])
{
    $this->template
    ->title($data['post']['title'])
    ->build('blog/post', $data);
}
0
votes

I modified the controller and the model like you say:

Controller

if($data['post'])
{
    $this->template
    ->title($data['post']['title'])
    ->build('blog/post', $data);
}

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')->row_array();
  }
}

So, the title template function is correct setting but the parser in the view don't work. It return the code in brackets:

{title}
By {name}
{content}

I think to change the model function from row_array() to row() in order to return a objet but the problem remain with the parser class.

I read here but I'm not sure that is the correct way... I do not want to modify a CI file...

Help me!