1

データベースから 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

アイデア?

4

3 に答える 3

0

あなたが言うように、コントローラーとモデルを変更しました:

コントローラ

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

モデル

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

つまり、タイトル テンプレート機能は正しい設定ですが、ビューのパーサーは機能しません。括弧内のコードを返します。

{title}
By {name}
{content}

オブジェクトを返すためにモデル関数をrow_array()からrow()に変更することを考えていますが、問題はパーサークラスに残っています。

ここを読みましたが、それが正しい方法かどうかわかりません... CIファイルを変更したくありません...

助けて!

于 2013-07-17T16:16:04.497 に答える
0

これを試して:

if($data['post'])
{
    $this->template
    ->title($data['post']['title'])
    ->build('blog/post', $data);
}
于 2013-07-17T07:29:54.073 に答える
0

ここに問題があると思います

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);
    }

そうでない場合は、ループを使用してタイトルを取得する必要があると思います..

于 2013-07-17T07:20:07.157 に答える