从视图codeigniter中的控制器访问数组

时间:2016-01-31 04:55:19

标签: php arrays codeigniter

我正在使用codeigniter开发一个Web应用程序。我试图在我的视图中显示一个数组,从模型传递到控制器然后再查看。我尝试了访问数组,如:$data->value但它没有用。这是我的代码:

模型

    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;
    }

控制器

    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);
    }

查看:

    <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>

这是我打印数组时的结果:

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

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

我尝试了$purchases->total_amount$purchases->paid,但它显示为零。 我不知道哪里出错了。任何人都可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:3)

在您提供的代码中,model以数组格式返回数据。 问题是您正在访问此阵列的项目,因为它是一个对象。

尝试:

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

而不是:

$purchases->total_amount and $purchases->paid