[英]Opencart foreach showing NULL in tpl file
我正在使用Opencart在我的商店中創建一個自定義模塊,該文件名為sales_performance
。
在這里傳遞所選客戶id
和name
的值。
一切正常。 在使用var_dump($data['customers']);
在控制器中,它在sales_performance.tpl
頂部顯示了一些值。
但是在sales_performance.tpl
我使用的是var_dump($customers);
sales_performance.tpl
它顯示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 } ?>
這里錯過了什么...? 並感謝提前...
數據變量是函數中的變量,您可以在類中創建一個受保護的變量並使用該變量。
<?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()與客戶一起返回一個數組,這是正常的做法。 我建議將它也放入模型中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.