duanpi7578 2017-06-23 02:22
浏览 34
已采纳

制作对象并在codeigniter视图上传递它

i have a query from my model. Now in my controller, the query is inside the loop, now i have two results of objects from my query. How can i make it only one object to be passed in my view?

Here is my code:

public function graph_diagnose() {
       $diagnosis = $this->input->post('selectdiagnosis');
       for ($i = 0; $i < count($diagnosis); $i++) {
            $diagname = $this->Adminreport_model->getDiagName($diagnosis[$i]);
            echo json_encode($diagname);
       }
    }

now i have a result from my query that is stored in $diagname and it consist of two rows inside every query.

Here is the result of echo json_encode:

 array(1) {
  [0]=>
  object(stdClass)#23 (2) {
    ["diagnosis"]=>
    string(8) "Headache"
    ["diagnosis_id"]=>
    string(1) "1"
  }
}
array(1) {
  [0]=>
  object(stdClass)#24 (2) {
    ["diagnosis"]=>
    string(10) "Sakit Kaau"
    ["diagnosis_id"]=>
    string(1) "2"
  }
}

now what i want is to make it as one object to be json encode. where i want an output like this:

 array(1) {
  [0]=>
  object(stdClass)#23 (2) {
    ["diagnosis"]=>
    string(8) "Headache"
    ["diagnosis_id"]=>
    string(1) "1"
  }
  [1]=>
  object(stdClass)#23 (2) {
    ["diagnosis"]=>
    string(10) "Sakit Kaau"
    ["diagnosis_id"]=>
    string(1) "2"
  }
}
  • 写回答

1条回答 默认 最新

  • dqce48404 2017-06-23 02:37
    关注

    Define an array to store the result:

    $diagnames = [];
    

    Inside the for loop:

    $result = $this->Adminreport_model->getDiagName($diagnosis[$i]);
    $diagnames = array_merge($diagnames, $result);
    

    But I am wondering why your getDiagName() method is returning an array?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化