【问题标题】:how to read a specific field from an array returned by the model in the controller itself如何从控制器本身的模型返回的数组中读取特定字段
【发布时间】:2016-08-09 06:50:22
【问题描述】:

我在控制器“thisTopic”中有一个函数,它具有以下内容:

$viewdata = array();
$viewdata['reply']   = $this->disc->get_all_replies($topic_id);
$viewdata['firstReply']= $this->disc->get_firstReply(); 

在模型中:

public function get_all_replies($topic_id){

    return $results =  $this->db->select('*')
                     ->from('disc_replies')
                     ->join('users', 'users.id = disc_replies.users_id')
                     ->where('topic_id',$topic_id)
                     ->get()->result();
}

public function get_firstReply($reply_id){

    return $results =  $this->db->select('*')
                     ->from('disc_firstReply')
                     ->join('users', 'users.id = disc_firstReply.users_id')
                     ->where('reply_id',$reply_id)
                     ->get()->result();
}

disc_replies 表具有以下列:

1. reply_id
2. topic_id
3. users_id
4. replied_on

$viewdata['reply'] 我想读取第一行的reply_id 字段并将其作为参数发送给get_firstReply() 函数。

这是我迄今为止尝试过的:

$viewdata['firstReply']= $this->disc->get_firstReply($viewdata['reply'][0]['reply_id']);

$viewdata['firstReply']= $this->disc->get_firstReply($viewdata['reply']['reply_id']);

但没有任何效果,非常感谢任何解决此问题的帮助。在此先感谢

【问题讨论】:

    标签: php html mysql codeigniter


    【解决方案1】:

    为了在 codeigniter 中使用这个$viewdata['reply'][0](0 指向数组),您必须将数组格式化为目标数组。

    这个

    ->get()->result();
    

    应该改变为

    ->get()->result_array();
    

    您可以使用print_r($viewdata['reply'])检查数组的格式


    确保$viewdata['reply'] 不为空

    【讨论】:

      猜你喜欢
      • 2018-12-13
      • 1970-01-01
      • 2013-09-25
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多