Jorz
Jorz

Reputation: 358

Codeigniter: Extract certain value from an array under controller

Good day!

This is pretty basic to some.

I have this code on my controller:

$data["sales_info"] = $this->The_model->get_sales_info(SELECT * FROM sales WHERE sales_id = 123);

Then get_sales_info() method under The_model stores the specific sales information into an array.

Now, I'm doing some debugging, so from the controller, I want to extract from the $data["sales_info"] and echo only the value of 'sales_date'.

How am I going to do this?

Thank you!

Upvotes: 0

Views: 569

Answers (3)

Neophyte
Neophyte

Reputation: 143

This code may help you

$sales_info = $this->The_model->get_sales_info(SELECT * FROM sales WHERE sales_id = 123);
$data['sales_data'] = $sales_info['sales_date'];

Upvotes: 2

NikuNj Rathod
NikuNj Rathod

Reputation: 1658

You can try this solution for your problem:

Change the model file to :

The_model.php

class The_model extends MY_Model {
    function get_sales_info($sales_id){
        $this->db->select("*.*");
        if(!empty($sales_id)){
            $this->db->where('sales_id', $sales_id);
            $query = $this->db->get('sales');
            return $query->row();
        } else {
            $query = $this->db->get('sales');
            return $query->result();
        }
    }
}

and change the controller file to:

My_controller.php

$sales_id = 123;
$sales_info = $this->The_model->get_sales_info($sales_id);
echo $sales_info['sales_date'];exit; //how to get value from object

Thanks

Upvotes: 2

Bira
Bira

Reputation: 5506

$query = $this->db->get_where('sales', array('sales_id' => 123));

It should be like this, you cannot write the sql in to a method as best practice.

Upvotes: 1

Related Questions