dongyue0263 2013-12-17 19:53
浏览 67
已采纳

从Model到Controller codeigniter返回数组

I'm trying to query two values from database, with 2 different IDs and I need to get all the results on one array. This is part of my controller.

function account()
{
    $user_id = $this->session->userdata('user_id');
    $this->load->model("Site_model");
    $q = $this->Site_model->get_all_notify($user_id);
}

and here is the Model:

function get_user_data_by_id($id) {
    $this->db->where("id",$id);
    $q=$this->db->get("users");
    return  $q->result();
}

function get_all_notify($user_id) {
    $this->db->where("wanted_id",$user_id);
    $this->db->where("requests_id",1);
    $q=$this->db->get("intrested");
    foreach($q->result() as $row) {
        $user=$this->get_user_data_by_id($row->users_id);
    }
    return $user;
}

But when I print_r $q from controller I only get 1 row... but when I print_r $user I get all the results! Even with foreach nothing seems to work!

  • 写回答

1条回答 默认 最新

  • douluo1330 2013-12-17 19:56
    关注

    You need to make array and store the users data in that,in current code the last iteration data from foreach will be return

    $user=array();
    foreach($q->result() as $row)
    {
            $user[]=$this->get_user_data_by_id($row->users_id);
    }
    return $user;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行