phpcodeignitermodelcontrollermethod-parameters

codeigniter pass variable from controller to model


simple issue I presume.

My controller is getting the if to display from the url using $this->uri->segment(3). This will always be a single value. I am putting this in an array to pass to the model with:

$customerid = array(
   'id' => $this->uri->segment(3)
);

The controller syntax is below:

function confirm_delete_customer()
{
            $data['title']="Confirm Customer Deletion";

            $customerid=array(
                'id'=>$this->uri->segment(3)
                );

            //query model to get data results for form
            $data=array();

            if($query=$this->model_master_data->get_customer_records_to_delete()){
                $data['records']=$query;


            $this->load->view("master_data/view_master_data_header",$data);
            $this->load->view("master_data/view_master_data_nav");
            $this->load->view("master_data/view_content_master_data_confirm_customer_deletion",$data);
            $this->load->view("master_data/view_master_data_footer");



}

I am then trying to access this array value and pass it to my model to process. If I hard code the array into the model it works as per below syntax:

Model - Manual Syntax is:

function get_customer_records_to_delete()
{
    $query = $this->db->get_where('customers', array('id'=>43));
    return $query->result();
}

if I try replace this with the array from my controller it fails with error: Undefined variable: customerid

idea of model that I want to get working:

function get_customer_records_to_delete()
{
    $query = $this->db->get_where('customers', $customerid);
    return $query->result();
}

I have a feeling it is something small. however is this the best way to get a single record from the database in order to output to a view?

Thanks in advance for the assistance.


Solution

  • The best way to do that is:

    function confirm_delete_customer()
    {
        $data=array();
    
        $data['title']="Confirm Customer Deletion";
    
        $customerId = $this->uri->segment(3);
    
        //Prevent SQL injections
        if(!is_numeric($customerId) || empty($customerId)) {
            show_error("Bad Request");
        }
    
        $query = $this->model_master_data->get_customer_records_to_delete($customerId);
    
        if ($query){
            $data['records']=$query;
    
    
            $this->load->view("master_data/view_master_data_header",$data);
            $this->load->view("master_data/view_master_data_nav");
            $this->load->view("master_data/view_content_master_data_confirm_customer_deletion",$data);
            $this->load->view("master_data/view_master_data_footer");
    
        }
    }
    

    and then you can simply call:

    function get_customer_records_to_delete($customerId)
    {
        $query = $this->db->get_where('customers', array('id'=>$customerId));
        return $query->result();
    }
    

    at your model.