Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access array data passed from CodeIgniter controller to view [duplicate]

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?

like image 857
Mohsin Avatar asked Sep 12 '26 09:09

Mohsin


1 Answers

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

like image 130
Vickel Avatar answered Sep 14 '26 23:09

Vickel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!