Opencart foreach在tpl文件中显示NULL

时间:2017-04-28 06:46:15

标签: php foreach opencart

使用 Opencart 在我的商店中创建自定义模块,该文件名称为sales_performance

此处传递所选客户idname的值。

一切正常。当我在控制器中使用var_dump($data['customers']);时,它会在sales_performance.tpl顶部显示一些值。

但在使用sales_performance.tpl的{​​{1}}中,它显示var_dump($customers);

sales_performance.php(控制器)

NULL

sales_performance.tpl

<?php
class ControllerProductSalesPerformance extends Controller
{
    private $error = array();

    public function index()
    {
        $this->load->language('product/allproduct');

        $this->document->setTitle($this->language->get('Sales Performance'));

        $this->load->model('catalog/allproduct');

        $this->getSelectedCustomer();

        $data['text_list'] = $this->language->get('text_list');
        $data['text_no_results'] = $this->language->get('text_no_results');
        $data['text_confirm'] = $this->language->get('text_confirm');
        $data['text_missing'] = $this->language->get('text_missing');

        $data['column_order_id'] = $this->language->get('column_order_id');
        $data['column_customer'] = $this->language->get('column_customer');
        $data['column_status'] = $this->language->get('column_status');
        $data['column_total'] = $this->language->get('column_total');
        $data['column_date_added'] = $this->language->get('column_date_added');
        $data['column_date_modified'] = $this->language->get('column_date_modified');
        $data['column_action'] = $this->language->get('column_action');

        $data['entry_return_id'] = $this->language->get('entry_return_id');
        $data['entry_order_id'] = $this->language->get('entry_order_id');
        $data['entry_customer'] = $this->language->get('entry_customer');
        $data['entry_order_status'] = $this->language->get('entry_order_status');
        $data['entry_total'] = $this->language->get('entry_total');
        $data['entry_date_added'] = $this->language->get('entry_date_added');
        $data['entry_date_modified'] = $this->language->get('entry_date_modified');

        $data['button_invoice_print'] = $this->language->get('button_invoice_print');
        $data['button_shipping_print'] = $this->language->get('button_shipping_print');
        $data['button_add'] = $this->language->get('button_add');
        $data['button_edit'] = $this->language->get('button_edit');
        $data['button_delete'] = $this->language->get('button_delete');
        $data['button_filter'] = $this->language->get('button_filter');
        $data['button_view'] = $this->language->get('button_view');

        //$data['token'] = $this->session->data['token'];



        $pagination = new Pagination();
        //$pagination->total = $order_total;
        //$pagination->page = $page;

        $data['pagination'] = $pagination->render();

        $data['sort'] = $sort;
        $data['order'] = $order;

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view('default/template/product/sales_performance.tpl', $data));

    }

    public function getSelectedCustomer()
    {
        $isSalesPerson=$_COOKIE['isSalesPerson'];
        if($isSalesPerson =='true')
        {
            $selectedCustomerId = $_GET['selectedCustomerId'];
            $selectedCustomerName = $_GET['selectedCustomerName'];
        }
        //select customer
        $customerId=$_COOKIE['customerId'];
        $query = $this->db->query("select customer_id,CONCAT(c.firstname,' ', c.lastname) as name from " . DB_PREFIX . "customer c where c.sales_person_id=".$customerId." and customer_id=".$selectedCustomerId."");
        $getcustomers=$query->rows;
        foreach ($getcustomers as $customer) {
           $data['customers'][] = array(
            'customer_id' => $customer['customer_id'],
            'name' => $customer['name']
            );
        }

        var_dump($data['customers']); // this the code showing some value

        //select customer
    }
}

这里错过了什么......? 并感谢您提前......

1 个答案:

答案 0 :(得分:2)

数据变量是函数中的变量,您可以在类中创建受保护的变量并使用该变量。

<?php
class ControllerProductSalesPerformance extends Controller
{
    private $error = array();
    private $data = array();

    public function index()
    {
        $this->load->language('product/allproduct');

        $this->document->setTitle($this->language->get('Sales Performance'));

        $this->load->model('catalog/allproduct');

        $this->getSelectedCustomer();

        $this->data['text_list'] = $this->language->get('text_list');
        $this->data['text_no_results'] = $this->language->get('text_no_results');
        $this->data['text_confirm'] = $this->language->get('text_confirm');
        $this->data['text_missing'] = $this->language->get('text_missing');

        $this->data['column_order_id'] = $this->language->get('column_order_id');
        $this->data['column_customer'] = $this->language->get('column_customer');
        $this->data['column_status'] = $this->language->get('column_status');
        $this->data['column_total'] = $this->language->get('column_total');
        $this->data['column_date_added'] = $this->language->get('column_date_added');
        $this->data['column_date_modified'] = $this->language->get('column_date_modified');
        $this->data['column_action'] = $this->language->get('column_action');

        $this->data['entry_return_id'] = $this->language->get('entry_return_id');
        $this->data['entry_order_id'] = $this->language->get('entry_order_id');
        $this->data['entry_customer'] = $this->language->get('entry_customer');
        $this->data['entry_order_status'] = $this->language->get('entry_order_status');
        $this->data['entry_total'] = $this->language->get('entry_total');
        $this->data['entry_date_added'] = $this->language->get('entry_date_added');
        $this->data['entry_date_modified'] = $this->language->get('entry_date_modified');

        $this->data['button_invoice_print'] = $this->language->get('button_invoice_print');
        $this->data['button_shipping_print'] = $this->language->get('button_shipping_print');
        $this->data['button_add'] = $this->language->get('button_add');
        $this->data['button_edit'] = $this->language->get('button_edit');
        $this->data['button_delete'] = $this->language->get('button_delete');
        $this->data['button_filter'] = $this->language->get('button_filter');
        $this->data['button_view'] = $this->language->get('button_view');

        //$data['token'] = $this->session->data['token'];



        $pagination = new Pagination();
        //$pagination->total = $order_total;
        //$pagination->page = $page;

        $data['pagination'] = $pagination->render();

        $this->data['sort'] = $sort;
        $this->data['order'] = $order;

        $this->data['header'] = $this->load->controller('common/header');
        $this->data['column_left'] = $this->load->controller('common/column_left');
        $this->data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view('default/template/product/sales_performance.tpl', $this->data));

    }

    public function getSelectedCustomer()
    {
        $isSalesPerson=$_COOKIE['isSalesPerson'];
        if($isSalesPerson =='true')
        {
            $selectedCustomerId = $_GET['selectedCustomerId'];
            $selectedCustomerName = $_GET['selectedCustomerName'];
        }
        //select customer
        $customerId=$_COOKIE['customerId'];
        $query = $this->db->query("select customer_id,CONCAT(c.firstname,' ', c.lastname) as name from " . DB_PREFIX . "customer c where c.sales_person_id=".$customerId." and customer_id=".$selectedCustomerId."");
        $getcustomers=$query->rows;
        foreach ($getcustomers as $customer) {
           $this->data['customers'][] = array(
            'customer_id' => $customer['customer_id'],
            'name' => $customer['name']
            );
        }

        var_dump($this->data['customers']); // this the code showing some value

        //select customer
    }
}

您还可以让getSelectedCustomer()返回一个包含客户的数组,这是正常的方法。我会建议将它放入模型中。