0

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!

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! It worked! Aaand, question.. can I also echo other data using $sales_info['any_column']? =)
Yes you can get any column from return get_sales_info function.
I not sure why have exit
2

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'];

Comments

1
$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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.