Opencart foreach 在 tpl 文件中显示 NULL

Opencart foreach showing NULL in tpl file

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

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

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

顶部显示一些值

但是在 sales_performance.tpl 我使用 var_dump($customers); 它显示 NULL.

sales_performance.php(控制器)

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

sales_performance.tpl

<?php var_dump($customers); ?>
<?php foreach ($customers as $customer) { ?>
    <h3>Customer ID: <?php echo $customer['customer_id']; ?></h3>
    <h3>Customer Name:</h3>
<?php } ?>

这里遗漏了什么...? 感谢您的提前...

数据变量是您函数中的一个变量,您可以在 class 中创建一个受保护的变量并使用它。

<?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() return 一个包含客户的数组,这是正常的做法。我会推荐它也将其放入模型中。