3

I am having a problem trying to pass a variable from the controller to the view. I think I am doing a mistake somewhere, I can't find it since I am new to it.

My controller Method is :

public function paymentdetail($order_code)
{
    $this->load->model('Payment_Model');
    $paymentDetail = $this->Payment_Model->getpaymentdetail($order_code);
    $orderDetail = $this->Payment_Model->getOrderDetail($order_code);
    //        echo print_r($paymentDetail);
    $datas['content'] = $this->load->view('payment/paymentDetail',
        array('paymentDetail' => $paymentDetail,
            'orderDetail' => $orderDetail), true);
    $this->load->view('layouts/main_template', $datas);
}

The following model function getpaymentdetail() returns an array (result_array()) and this is the one I am concerned about. If I can work this one out then I can also work with other model methods.

$this->Payment_Model->getpaymentdetail($order_code);

When I type <?php echo $paymentDetail['column_name']; ?> in view file(PaymentDetail.php) I get an error

Undefined index: column_name

Why do I get this error?

4
  • 1
    The variable $paymentDetail['column_name'] does not exist. We can't help you, because we don't know what $this->Payment_Model->getpaymentdetail($order_code) returns. It's apparently not what you expect though. Commented Aug 18, 2020 at 15:17
  • Thank you for your interest. $this->Payment_Model->getpaymentdetail($order_code) returns an array like the function result_array(). I get values from the controller to view if I use echo print_r($paymentDetail); . I get the full array with value. But $paymentDetail['column_name'] is giving error that probably means I am doing something wrong that I don't know. Commented Aug 18, 2020 at 15:22
  • 1
    Directly after you set $paymentDetail, put die("<pre>".print_r($paymentDetail, true)."</pre>"); Run your script again and update your question with what that variable contains. Commented Aug 18, 2020 at 15:23
  • Also, update your question to show where you are using $paymentDetail['column_name'] Commented Aug 18, 2020 at 15:24

1 Answer 1

2

model function getpaymentdetail() returns an array (result_array())

those arrays are normally structured like this:

Array
(
    [0] => Array
        (
            [ID] => 3120
            [column_name] => col1
         )
)

hence you cannot access column_name via <?php echo $paymentDetail['column_name']; ?> as this index doesn't exist in your array structure.

if you move one index deeper, then it will work: <?php echo $paymentDetail[0]['column_name']; ?>

Attention, if you expect more than 1 row to be returned, above will only access the first result (indexed 0) row! You'd need a foreach loop to get all results, see Generating Query Results - result arrays

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.