[英]Opencart foreach showing NULL in tpl file
Am creating a custom module in my store using Opencart , that file name called sales_performance
. 我正在使用Opencart在我的商店中创建一个自定义模块,该文件名为
sales_performance
。
Here am passing the values for selected customer id
and name
. 在这里传递所选客户
id
和name
的值。
All working fine. 一切正常。 when am using
var_dump($data['customers']);
在使用
var_dump($data['customers']);
in controller it showing some values in top of the sales_performance.tpl
. 在控制器中,它在
sales_performance.tpl
顶部显示了一些值。
But in sales_performance.tpl
am using var_dump($customers);
但是在
sales_performance.tpl
我使用的是var_dump($customers);
sales_performance.tpl
it showing NULL
. 它显示
NULL
。
sales_performance.php(controller) 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 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. 您还可以让getSelectedCustomer()与客户一起返回一个数组,这是正常的做法。 I would recomment it to put it into a model as well.
我建议将它也放入模型中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.