用PHP无法返回数组数据?

时间:2014-02-23 15:05:48

标签: php arrays codeigniter

我目前正在尝试简单地返回一个带有codeigniter活动记录的数组,之前我曾做过数千次。我很确定这将是一个简单的问题,但我似乎无法找到问题。需要另一只眼睛!

下面的代码是一切似乎都出错的地方,特别是在foreach中。当我删除foreach时,只需使用一个简单的where子句:$this->db->where( array('posts.category_string' => $category['category_string'], 'published' => 1) );它就像我期望的那样在视图中显示记录。

public function get_most_relevant($user){

    $categories = $this->get_users_most_common_categories($user);

    $this->db->order_by('posts.date_created','DESC');
    $this->db->limit(25);
    $this->db->select('*');
    $this->db->from('posts');
    $this->db->select('posts.image_name as post_image');
    $this->db->select('posts.random_string as post_string');
    $this->db->select('posts.date_created as post_creation');
    $this->db->join('users', 'posts.user_string = users.random_string','left'); 
    foreach($categories->result_array() as $category){
        $i = 0;
        if($i == 0){
            $this->db->where( array('posts.category_string' => $category['category_string'], 'published' => 1) );
        }else{
            $this->db->or_where( array('posts.category_string' => $category['category_string'], 'published' => 1) );
        }   
        $i++;
    }

    $posts = $this->db->get();

    return $posts;

}

我还在下面的函数中使用了print_r()函数,以确保返回的记录是

public function get_users_most_common_categories($user){

    $this->db->order_by('category_occurence', 'DESC');
    $this->db->limit(25);
    $this->db->group_by('category_string');
    $this->db->select('*');
    $this->db->select('COUNT(category_string) AS category_occurence');
    $this->db->from('views');       
    $this->db->where('user_string', $user);
    $this->db->or_where('ip_address', $user);

    $categories = $this->db->get();

    return $categories;

}

来自get_users_most_common_categories()函数的数组:

Array
(
[0] => Array
    (
        [id] => 13
        [post_string] => zmn2GxUNeD1V
        [user_string] => jpgYW9kXqnJ9
        [category_string] => cCU8oEQHYLWP
        [ip_address] => 0.0.0.0
        [date_created] => 2014-02-22 23:56:42
        [category_occurence] => 3
    )

[1] => Array
    (
        [id] => 14
        [post_string] => 9qlnMxRg
        [user_string] => jpgYW9kXqnJ9
        [category_string] => 8RfRDWrG5QB7
        [ip_address] => 0.0.0.0
        [date_created] => 2014-02-22 23:56:44
        [category_occurence] => 1
    )

)

希望有所帮助,感谢您的时间!

0 个答案:

没有答案