如何在codeigniter中将json数据从模型传递到控制器

时间:2014-09-26 11:11:59

标签: json codeigniter

我正在尝试将数据从模型传递到控制器,以便将其通过json传递到ajax页面,我没有正确的做法,到目前为止,我已尝试过以下代码。

在我的控制器中;

 public function grid_view_ajax_function()
        {       
            $contacts_details="";
            $this->load->model('contact_model');
            $contacts_details= $this->contact_model->get_all_contacts();

            $data['id']                =$contacts_details->contact_id; /getting error  here
            $data['first_name']        =$contacts_details->first_name; /"
            $data['last_name']         =$contacts_details->last_name; /"
            $data['title']             =$contacts_details->title; /" 
            $data['city']              =$contacts_details->city; /"
            $data['country']           =$contacts_details->country_id;/"
            $data['city']              =$contacts_details->city;/"
            $data['contact_mobile']    =$contacts_details->contact_mobile;
            $data['unique_id']         =$contacts_details->unique_id;

            echo json_encode(array('id'=>$data['id'],'first_name'=>$data['first_name'],'last_name'=>$data['last_name'],'title'=>$data['title'],'city'=>$data['city'],'coutry'=>$data['contact_mobile'],'unique_id'=>$data['unique_id'] ));

        }

我的模特

public function get_all_contacts() {
        $this->db->select("c.id contact_id, c.salutation salutation, c.honorfic honorfic, c.first_name first_name, c.middle_name middle_name, c.last_name last_name, c.email_primary email_primary, c.email_secondry email_secondry, c.contact_mobile contact_mobile, c.contact_home contact_home, DATE_FORMAT(c.dob, '%e %M %Y') dob, c.image image, c.marital_status marital_status, c.spouse_name spouse_name, c.title title, c.email_work email_work, c.contact_work contact_work, c.assistant_name assistant_name, c.assistant_email assistant_email, c.address address, c.city city, c.state state, c.country_id country_id, c.postbox postbox, c.biography biography, c.created_by created_by, c.modified_by modified_by, c.created_on created_on, c.modified_on modified_on", false);
        $this->db->from("contact c");
        $this->db->where("c.flag_delete", '0');     
        $obj_result  = $this->db->get();
        $row         = $obj_result->result_array(); 


        return $row;
    }

我收到错误"试图获取非对象的属性" 谢谢。

2 个答案:

答案 0 :(得分:1)

$obj_result->result_array()更改为$obj_result->result()并尝试

         $contacts_details_list= $this->contact_model->get_all_contacts();
         $result = array();
         foreach($contacts_details_list as $contacts_details) {
            $data = array();
            $data['id']                =$contacts_details->contact_id; /getting error  here
            $data['first_name']        =$contacts_details->first_name; /"
            $data['last_name']         =$contacts_details->last_name; /"
            $data['title']             =$contacts_details->title; /" 
            $data['city']              =$contacts_details->city; /"
            $data['country']           =$contacts_details->country_id;/"
            $data['city']              =$contacts_details->city;/"
            $data['contact_mobile']    =$contacts_details->contact_mobile;
            $data['unique_id']         =$contacts_details->unique_id;

            array_push($result, $data);
          }

          echo json_encode(array('data' => $result));

答案 1 :(得分:1)

您的function get_all_contacts()返回正常数组,并且您尝试将其作为stdClass对象执行。

将其设为result()并将其添加到循环或更改$contacts_details->contact_id;

$contacts_details['contact_id]';refer