Osama Shaki
Osama Shaki

Reputation: 149

How to use model function result in controller

I have an error in my controller. I am trying to use the result coming from model function to call another function in my model. I am using Codeigniter Framework. hope you can help me out. Thanks

Controller:

function photographer_campaign_details(){

        $camp_id = $this->uri->segment(4);
        $data['page_title'] = 'Photographer Campaign Details';
        $adminId = $this->session->userdata('adminid');
        $campaign = $this->Admin_model->get_all_photographer_campaign_details($camp_id);

        $data['seller'] = $this->Admin_model->get_seller_name_by_id($campaign['uid']);//error is here: Undefined index: uid
        $data['campaign'] = $campaign;
        $this->load->view('admin/photographer_campaign_details',$data);

}

My Model:

function get_all_photographer_campaign_details($camp_id) {
        $this->db->select('*');
        $this->db->where('campaign_id',$camp_id);
        $query = $this->db->get('ps_campaigns');
        if ($query->num_rows() > 0) {
            foreach ($query->result_array() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return array();
    }
    //get seller name by id
    function get_seller_name_by_id($uid)
    {
        $this->db->select('firstname, lastname');
        $this->db->where('id', $uid);
        $query = $this->db->get('ps_users');
        //return $query->row();
        return $query->result_array();

    }

an error is coming from the controller: Undefined index: uid

Upvotes: 0

Views: 48

Answers (1)

Conor Mancone
Conor Mancone

Reputation: 2030

Looking at your get_all_photographer_campaign_details, if no rows are found then you return an empty array.

In your controller, you never check to see if a valid entry was found. As a result you get your undefined index: uid when an id is referenced in the URL that doesn't correspond to an entry, because $campaign is empty and doesn't have a uid key. Try something like this:

function photographer_campaign_details(){

        $camp_id = $this->uri->segment(4);
        $data['page_title'] = 'Photographer Campaign Details';
        $adminId = $this->session->userdata('adminid');
        $campaign = $this->Admin_model->get_all_photographer_campaign_details($camp_id);
        if (!$campaign ){
            show_404();
        }

        $data['seller'] = $this->Admin_model->get_seller_name_by_id($campaign['uid']);//error is here: Undefined index: uid
        $data['campaign'] = $campaign;
        $this->load->view('admin/photographer_campaign_details',$data);

}

Additionally, you are returning data wrong in the event that you do find data. Namely this bit in get_all_photographer_campaign_details:

        foreach ($query->result_array() as $row) {
            $data[] = $row;
        }

Should be something like:

        foreach ($query->result_array() as $row) {
            $data = $row;
            break;
        }

The problem is that you are appending the row as one row in $data, but your controller is expecting to get the actual data itself. I.e. your controller is expecting this:

[
    'campaignid' => 1,
    'uid' => 'ijerjeire'
]

But you are returning this:

[
    [
        'campaignid' => 1,
        'uid' => 'ijerjeire'
    ]
]

Note the extra array that everything is wrapped around. Basically, your model is returning an array of results, when your controller is just expecting results. My above suggestion will work if there is only ever supposed to be one campaign returned. If that is not the case, then you need to adjust your controller instead of your model method.

To reiterate my other point: make sure and validate the user input that comes from the URL. Otherwise you will return PHP errors instead of 404's.

Upvotes: 1

Related Questions