简体   繁体   中英

Opencart foreach showing NULL in tpl file

Am creating a custom module in my store using Opencart , that file name called sales_performance .

Here am passing the values for selected customer id and name .

All working fine. when am using var_dump($data['customers']); in controller it showing some values in top of the sales_performance.tpl .

But in sales_performance.tpl am using var_dump($customers); it showing NULL .

sales_performance.php(controller)

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

What am missed here...? And Thanks for advance...

The data variable is a variable in your function, you are able to create a protected variable in your class and use that one.

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

You are also able to let getSelectedCustomer() return an array with the customers, this would be the normal way to do it. I would recomment it to put it into a model as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM