php - 未定义索引 : in PHP codeigniter while sending variable

标签 php mysql codeigniter

我的 Controller :

public function thread($page = 'empty')
{

    $data['user_id'] = $this->tank_auth->get_user_id();
    $data['username'] = $this->tank_auth->get_username();
    //User data variable grab
    //default template load data
    $data['thread'] = $this->thread_model->get_thread($page);
    $data['title'] = ucfirst($page); // Capitalize the first letter
    $data['replies'] = $this->thread_model->get_reply($data['thread']['thread_id']);
    $this->thread_model->view_increment($data);
    $this->load->view('templates/head', $data);
    $this->load->view('main/wrapper-start', $data);
    $this->load->view('templates/leftbar', $data);
    $this->load->view('main/thread', $data);
    $this->load->view('main/wrapper-end', $data);
    $this->load->view('templates/footer', $data);

}

错误出现在 $data['replies'] 行:

Severity: Notice

Message: Undefined index: thread_id

Filename: controllers/main.php

Line Number: 40

这是我的模型代码,回复是指:$this->thread_model->get_reply

public function get_reply($thread_id)
{

    $query = $this->db->query("SELECT r.reply_id FROM reply_thread rt
    INNER JOIN replies r
    ON rt.reply_id = r.reply_id
    WHERE thread_id = ". $thread_id);
    $replies = $query->result_array();
    $replyarray = array();
    foreach ($replies as $reply)
    {
        $id = $reply['reply_id'];
        $query = $this->db->query("SELECT * FROM replies WHERE reply_id='$id'");
        $thisRow = $query->row_array();
        $replyarray[] = $thisRow;
    }

    return $replyarray;
}

知道为什么会弹出此错误吗?除了错误之外,页面加载完全正常...所有信息都完好无损,可通过查询获得。

这是我的 get_thread 模型

public function get_thread($page)
{
    $slug = $page;
    $query = $this->db->get_where('thread', array('slug' => $slug));
    return $query->row_array();
}

使用 print_r($data['thread']); 我得到这个数组:

Array ( [thread_id] => 233 [owner_id] => 8 [subject] => Repercussions of D3 addiction [body] => 1. signs of unhygienic practices 2.become irritable when forced to stop playing 3. carpal tunnel 4. loss of interest in pursuing a significant other 5. sleep deprivation 6. malnutrition There's nothing positive about this. [timestamp] => 2012-05-08 19:03:02 [replystamp] => 2012-05-09 12:38:32 [last_post_id] => 3 [slug] => 233-repercussions-of-d3-addiction [replies_num] => 2 [views] => 21 )

最佳答案

我想你可以试试这个:

$threadinfo = $this->thread_model->get_thread($page);
$data['thread'] = $threadinfo;
$data['replies'] = $this->thread_model->get_reply($threadinfo->thread_id);

关于php - 未定义索引 : in PHP codeigniter while sending variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10606026/

相关文章:

javascript - 语法错误 : missing } after property list javascript

mysql - 限制一个表可以有的记录数

php - 通过FB、Twitter、Linkedin等跟踪用户。PHP、Mysql

php - 查询验证遇到困难

php - 使用子查询和Having优化SQL

php - 将变量数组数据插入MYSQL

php - SQL 子查询或 JOIN

php - 使用 mysqldump 排除表的语法

mysql:查询中的未知列错误

codeigniter - Lighttpd 重写 Codeigniter 在某些 URL 上崩溃