1

I am developing a web application in codeigniter. I am trying to show an array inside my view passed from model to controller then to view. I tried the accessing array like: $data->value But It didnt worked. Here is my code:

Model

    public function getPurchasesTotals($supplier_id)
    {
    $this->db->select('SUM(COALESCE(amount, 0)) as total_due', FALSE)
    ->where('supplier_id', $supplier_id);
    $q1 = $this->db->get('payments');
    if ($q1->num_rows() > 0) {
    $row = $q1->row();
    $dueamt= $row->total_due;
    }
    $this->db->select('SUM(COALESCE(grand_total, 0)) as total_amount, SUM(COALESCE(paid, 0)) as paid', FALSE)
    ->where('supplier_id', $supplier_id);
    $q = $this->db->get('purchases');
    if ($q->num_rows() > 0) {
    $row1 = $q->row();
    $grandtot = $row1->total_amount;
    $paid = $row1->paid;
    }
    $datarow= array(
    'dueamtpaid' =>$dueamt,
    'total_amount' =>$grandtot,
    'paid' =>$paid
    );
    return $datarow;
    }

Controller

    function supplier_report($user_id = NULL)
    {
    $this->sma->checkPermissions('suppliers', TRUE);
    if (!$user_id) {
    $this->session->set_flashdata('error', lang("no_supplier_selected"));
    redirect('reports/suppliers');
    }
    $this->data['purchases'] = $this->reports_model->getPurchasesTotals($user_id); // Here is my array
    $this->data['total_purchases'] = $this->reports_model->getSupplierPurchases($user_id);
    $this->data['users'] = $this->reports_model->getStaff();
    $this->data['warehouses'] = $this->site->getAllWarehouses();
    $this->data['supplierdetails'] = $this->reports_model->getSupplierDetails($user_id);
    $this->data['suppaymentdetails'] = $this->reports_model->getSupplierPaymentDetails($user_id);

    $this->data['error'] = validation_errors() ? validation_errors() : $this->session->flashdata('error');
    $this->data['user_id'] = $user_id;
    $this->page_construct('reports/supplier_report', $meta, $this->data);
    }

View:

    <div class="row">
        <div class="col-sm-12">
            <div class="row">
                <div class="col-sm-6">
                <?php echo print_r($purchases);?> //Here I get my array values, But I can't access. 
                    <a href='reports/add_payment/<?php echo $supplierdetails->id;?>' data-toggle="modal" data-target="#myModal"> <div class="small-box padding1010 col-sm-4 bpurple">
                        <h3><?= isset($supplierdetails->initial_due) ? $this->sma->formatMoney($supplierdetails->initial_due) : '0.00' ?></h3>

                        <p><?= lang('initial_due') ?></p>
                    </div></a>
                    <div class="small-box padding1010 col-sm-4 bblue">
                        <h3><?= isset($purchases->total_amount) ? $this->sma->formatMoney($purchases->total_amount) : '0.00' ?></h3>

                        <p><?= lang('purchases_amount') ?></p>
                    </div>
                    <div class="small-box padding1010 col-sm-4 blightOrange">
                        <h3><?= isset($purchases->paid) ? $this->sma->formatMoney($purchases->paid) : '0.00' ?></h3>

                        <p><?= lang('total_paid') ?></p>
                    </div>
                    <div class="small-box padding1010 col-sm-4 borange">
                        <h3><?= (isset($purchases->total_amount) || isset($purchases->paid)) ? $this->sma->formatMoney($purchases->total_amount - $purchases->paid + $supplierdetails->initial_due) : '0.00' ?></h3>

                        <p><?= lang('total_due_amount') ?></p>
                    </div>
                </div>
            </div>
        </div>

Here is my result when I print my array:

<?php echo print_r($purchases);?>

 `Array ( [dueamtpaid] => 2500.0000 [total_amount] => 17404.0000 [paid] => 7000.0000 ) 1`

I tried $purchases->total_amount and $purchases->paid ,but its showing zero. I dont know where am going wrong. Can anyone help me to fix this up.

0

1 Answer 1

3

In the code you provided, model returns the data in array format. The problem is that you're accessing this array's items as it's an object.

try:

$purchases['total_amount'] and $purchases['paid']

instead of:

$purchases->total_amount and $purchases->paid
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.