dongyu2047
2016-08-01 09:39
浏览 18
已采纳

在Codeigniter中针对id获取所有记录

When I retrieve the data from database the output is following enter image description here

I want to fetch records against these id's but when I try in my controller with for each it gives me only 1 record back Here is my code

//fetch all the posts relative to user in session
    $post_id['id'] = $this->message_model->get_post($id);
    echo '<pre>'; print_r($post_id['id']);die;
     foreach ($post_id['id'] as $key) {

        $post_id    =     $key['post_id'];
        $results['post_list'] = $this->Model_Frontend_Posts->get($post_id);
    }

when i print $results['post_list'] it gives me only one record

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanshan2988 2016-08-01 09:42
    已采纳

    Put it in an array so that it doesn't overrides on every loop. I guess this should solve it -

    $results['post_list'][] = $this->Model_Frontend_Posts->get($post_id);
    

    instead of

    $results['post_list'] = $this->Model_Frontend_Posts->get($post_id);
    
    打赏 评论
  • dongtao4787 2016-08-01 09:59
    $post_id['id'] = $this->message_model->get_post($id);
     $array = array_column($post_id['id'], 'post_id'));
         foreach ($array as $value) {
            $results['post_list'] = $this->Model_Frontend_Posts->get($value);
        }
    

    Better to use array_column from the reference http://php.net/manual/en/function.array-column.php

    打赏 评论

相关推荐 更多相似问题