【问题标题】:Undefined offset in CodeIgniter templateCodeIgniter 模板中未定义的偏移量
【发布时间】:2013-07-17 07:09:39
【问题描述】:

我会从数据库中将一个变量传递给 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

想法?

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    我认为问题就在这里

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

    如果不是,那么我想你需要使用循环来获取标题..

    【讨论】:

    • 嗨 bipen,如果我在模型中设置 row_array() 并在控件中设置 $data['post']['title'] 就像你说的标题有效但 CI 解析器没有工作。
    【解决方案2】:

    试试这个:

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

    【讨论】:

      【解决方案3】:

      我按照你说的修改了控制器和模型:

      控制器

      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() 以返回一个对象,但问题仍然存在于解析器类。

      我阅读了here,但我不确定这是正确的方法...我不想修改 CI 文件...

      帮帮我!

      【讨论】:

        猜你喜欢
        • 2016-01-07
        • 1970-01-01
        • 2020-06-10
        • 1970-01-01
        • 1970-01-01
        • 2013-08-18
        • 2017-07-25
        • 2011-03-13
        相关资源
        最近更新 更多