Search code examples
phpsqlcodeigniter

Get num_rows and result() data in same function and past to controller data in codeigniter


I am trying to show the users what is in the database and ask him if he still wants to insert the information he entered, but I am struggling to get their data to my controller.

This is what I have so far:

Controller

$result = $this->call_model->checkCallExists($callInfo);

if($result == true) { 
                

Model

function checkCallExists($callInfo)
{
    //pre($callInfo);      
    //die;

    $this->db->select("*");
    $this->db->from("tbl_calls");
    $this->db->where("type_of_equipment", $callInfo['type_of_equipment']);   
    $this->db->where("fk_location_id", $callInfo['fk_location_id ']);
    $this->db->where("fk_status_id", $callInfo['fk_status_id ']);
    $query = $this->db->get();

    if ($query->num_rows() > 0){
            $retun_array['data']= $query->result_array();
            return false;
        }
        else{
            return true;
    }
}

This is what I get when doing a dump in my model

enter image description here

I want to display it in table form with a button to cancel or insert into the database, but I am not getting the data to my controller.

Thank you in advance.


Solution

  • you are returning "true" or "false". Instead of "true" return the result array

    model:

      if ($query->num_rows() > 0){  
            $return_array['data']= $query->result_array();
            return $return_array;
      }
      else{
            // no records found
            return false;
      }
    

    or shorter:

    return ($query->num_rows())? $query->result_array():false;
    

    controller:

    $result = $this->call_model->checkCallExists($callInfo);
    
    if($result){ 
        echo'<pre>';print_r($result);die;  //comment this line to continue
        // send data to view
        $this->load->view('your_view', $result)
    }