如何从一个控制器到另一个控制器获取变量值? 这是my manufacturer.php(控制器文件)
这将取决于具体情况。如果您希望将数据保留一段时间,那么会话数据将是一种可行的方法。但是,如果只需要使用一次,flash数据可能更合适 第一步是初始化会话库:如何从一个控制器到另一个控制器获取变量值? 这是my manufacturer.php(控制器文件),php,model-view-controller,opencart,Php,Model View Controller,Opencart,这将取决于具体情况。如果您希望将数据保留一段时间,那么会话数据将是一种可行的方法。但是,如果只需要使用一次,flash数据可能更合适 第一步是初始化会话库: <li><a href="<?php echo $category['manufacturer'][$i]['href']; ?>"><?php echo $category['manufacturer'][$i]['name']; ?></a></li> 然后将信息
<li><a href="<?php echo $category['manufacturer'][$i]['href']; ?>"><?php echo $category['manufacturer'][$i]['name']; ?></a></li>
然后将信息存储在闪存数据中:
$this->load->library('session');
最后,在第二个控制器中,获取数据:
$this->session->set_flashdata('item', $myVar);
显然,这意味着您必须从第二个控制器再次初始化会话库,或者创建自己的基本控制器来加载会话库,并使两个控制器都从该控制器继承。这将取决于具体情况。如果您希望将数据保留一段时间,那么会话数据将是一种可行的方法。但是,如果只需要使用一次,flash数据可能更合适 第一步是初始化会话库:
<li><a href="<?php echo $category['manufacturer'][$i]['href']; ?>"><?php echo $category['manufacturer'][$i]['name']; ?></a></li>
然后将信息存储在闪存数据中:
$this->load->library('session');
最后,在第二个控制器中,获取数据:
$this->session->set_flashdata('item', $myVar);
显然,这意味着您必须从第二个控制器再次初始化会话库,或者创建自己的基本控制器来加载会话库,并使两个控制器都从该控制器继承。这是header.php代码
$myVar = $this->session->flashdata('item');
这是您的header.php代码
$myVar = $this->session->flashdata('item');
我相信会话将是最好的解决方案
您只能获取和存储模型中的所有数据,然后将其放入$\u会话超变量中
<?php
class ControllerCommonHeader extends Controller {
public function index() {
$data['title'] = $this->document->getTitle();
if ($this->request->server['HTTPS']) {
$server = $this->config->get('config_ssl');
} else {
$server = $this->config->get('config_url');
}
$data['base'] = $server;
$data['description'] = $this->document->getDescription();
$data['keywords'] = $this->document->getKeywords();
$data['links'] = $this->document->getLinks();
$data['styles'] = $this->document->getStyles();
$data['scripts'] = $this->document->getScripts();
$data['lang'] = $this->language->get('code');
$data['direction'] = $this->language->get('direction');
if ($this->config->get('config_google_analytics_status')) {
$data['google_analytics'] = html_entity_decode($this->config->get('config_google_analytics'), ENT_QUOTES, 'UTF-8');
} else {
$data['google_analytics'] = '';
}
$data['name'] = $this->config->get('config_name');
if (is_file(DIR_IMAGE . $this->config->get('config_icon'))) {
$data['icon'] = $server . 'image/' . $this->config->get('config_icon');
} else {
$data['icon'] = '';
}
if (is_file(DIR_IMAGE . $this->config->get('config_logo'))) {
$data['logo'] = $server . 'image/' . $this->config->get('config_logo');
} else {
$data['logo'] = '';
}
$this->load->language('common/header');
$data['text_home'] = $this->language->get('text_home');
$data['text_wishlist'] = sprintf($this->language->get('text_wishlist'), (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));
$data['text_shopping_cart'] = $this->language->get('text_shopping_cart');
$data['text_logged'] = sprintf($this->language->get('text_logged'), $this->url->link('account/account', '', 'SSL'), $this->customer->getFirstName(), $this->url->link('account/logout', '', 'SSL'));
$data['text_account'] = $this->language->get('text_account');
$data['text_register'] = $this->language->get('text_register');
$data['text_login'] = $this->language->get('text_login');
$data['text_order'] = $this->language->get('text_order');
$data['text_transaction'] = $this->language->get('text_transaction');
$data['text_download'] = $this->language->get('text_download');
$data['text_logout'] = $this->language->get('text_logout');
$data['text_checkout'] = $this->language->get('text_checkout');
$data['text_category'] = $this->language->get('text_category');
$data['text_all'] = $this->language->get('text_all');
$data['home'] = $this->url->link('common/home');
$data['wishlist'] = $this->url->link('account/wishlist', '', 'SSL');
$data['logged'] = $this->customer->isLogged();
$data['account'] = $this->url->link('account/account', '', 'SSL');
$data['register'] = $this->url->link('account/register', '', 'SSL');
$data['login'] = $this->url->link('account/login', '', 'SSL');
$data['order'] = $this->url->link('account/order', '', 'SSL');
$data['transaction'] = $this->url->link('account/transaction', '', 'SSL');
$data['download'] = $this->url->link('account/download', '', 'SSL');
$data['logout'] = $this->url->link('account/logout', '', 'SSL');
$data['shopping_cart'] = $this->url->link('checkout/cart');
$data['checkout'] = $this->url->link('checkout/checkout', '', 'SSL');
$data['contact'] = $this->url->link('information/contact');
$data['telephone'] = $this->config->get('config_telephone');
$status = true;
if (isset($this->request->server['HTTP_USER_AGENT'])) {
$robots = explode("\n", str_replace(array("\r\n", "\r"), "\n", trim($this->config->get('config_robots'))));
foreach ($robots as $robot) {
if ($robot && strpos($this->request->server['HTTP_USER_AGENT'], trim($robot)) !== false) {
$status = false;
break;
}
}
}
// Menu
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$data['categories'] = array();
$categories = $this->model_catalog_category->getCategories(0);
foreach ($categories as $category) {
if ($category['top']) {
// Level 2
$children_data = array();
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach ($children as $child) {
$filter_data = array(
'filter_category_id' => $child['category_id'],
'filter_sub_category' => true
);
$children_data[] = array(
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
}
// Level 1
$data['categories'][] = array(
'name' => $category['name'],
'children' => $children_data,
'column' => $category['column'] ? $category['column'] : 1,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
}
}
$data['language'] = $this->load->controller('common/language');
$data['currency'] = $this->load->controller('common/currency');
$data['search'] = $this->load->controller('common/search');
$data['cart'] = $this->load->controller('common/cart');
// For page specific css
if (isset($this->request->get['route'])) {
if (isset($this->request->get['product_id'])) {
$class = '-' . $this->request->get['product_id'];
} elseif (isset($this->request->get['path'])) {
$class = '-' . $this->request->get['path'];
} elseif (isset($this->request->get['manufacturer_id'])) {
$class = '-' . $this->request->get['manufacturer_id'];
} else {
$class = '';
}
$data['class'] = str_replace('/', '-', $this->request->get['route']) . $class;
} else {
$data['class'] = 'common-home';
}
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/header.tpl')) {
return $this->load->view($this->config->get('config_template') . '/template/common/header.tpl', $data);
} else {
return $this->load->view('default/template/common/header.tpl', $data);
}
}
// I have added this
$data['categories'] = array();
$results = $this->model_catalog_manufacturer->getManufacturers();
foreach ($results as $result) {
if (is_numeric(utf8_substr($result['name'], 0, 1))) {
$key = '0 - 9';
} else {
$key = utf8_substr(utf8_strtoupper($result['name']), 0, 1);
}
if (!isset($data['categories'][$key])) {
$data['categories'][$key]['name'] = $key;
}
$data['categories'][$key]['manufacturer'][] = array(
'name' => $result['name'],
'href' => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $result['manufacturer_id'])
);
}
}
我认为,会议将是解决这一问题的最佳办法
您只能获取和存储模型中的所有数据,然后将其放入$\u会话超变量中
<?php
class ControllerCommonHeader extends Controller {
public function index() {
$data['title'] = $this->document->getTitle();
if ($this->request->server['HTTPS']) {
$server = $this->config->get('config_ssl');
} else {
$server = $this->config->get('config_url');
}
$data['base'] = $server;
$data['description'] = $this->document->getDescription();
$data['keywords'] = $this->document->getKeywords();
$data['links'] = $this->document->getLinks();
$data['styles'] = $this->document->getStyles();
$data['scripts'] = $this->document->getScripts();
$data['lang'] = $this->language->get('code');
$data['direction'] = $this->language->get('direction');
if ($this->config->get('config_google_analytics_status')) {
$data['google_analytics'] = html_entity_decode($this->config->get('config_google_analytics'), ENT_QUOTES, 'UTF-8');
} else {
$data['google_analytics'] = '';
}
$data['name'] = $this->config->get('config_name');
if (is_file(DIR_IMAGE . $this->config->get('config_icon'))) {
$data['icon'] = $server . 'image/' . $this->config->get('config_icon');
} else {
$data['icon'] = '';
}
if (is_file(DIR_IMAGE . $this->config->get('config_logo'))) {
$data['logo'] = $server . 'image/' . $this->config->get('config_logo');
} else {
$data['logo'] = '';
}
$this->load->language('common/header');
$data['text_home'] = $this->language->get('text_home');
$data['text_wishlist'] = sprintf($this->language->get('text_wishlist'), (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));
$data['text_shopping_cart'] = $this->language->get('text_shopping_cart');
$data['text_logged'] = sprintf($this->language->get('text_logged'), $this->url->link('account/account', '', 'SSL'), $this->customer->getFirstName(), $this->url->link('account/logout', '', 'SSL'));
$data['text_account'] = $this->language->get('text_account');
$data['text_register'] = $this->language->get('text_register');
$data['text_login'] = $this->language->get('text_login');
$data['text_order'] = $this->language->get('text_order');
$data['text_transaction'] = $this->language->get('text_transaction');
$data['text_download'] = $this->language->get('text_download');
$data['text_logout'] = $this->language->get('text_logout');
$data['text_checkout'] = $this->language->get('text_checkout');
$data['text_category'] = $this->language->get('text_category');
$data['text_all'] = $this->language->get('text_all');
$data['home'] = $this->url->link('common/home');
$data['wishlist'] = $this->url->link('account/wishlist', '', 'SSL');
$data['logged'] = $this->customer->isLogged();
$data['account'] = $this->url->link('account/account', '', 'SSL');
$data['register'] = $this->url->link('account/register', '', 'SSL');
$data['login'] = $this->url->link('account/login', '', 'SSL');
$data['order'] = $this->url->link('account/order', '', 'SSL');
$data['transaction'] = $this->url->link('account/transaction', '', 'SSL');
$data['download'] = $this->url->link('account/download', '', 'SSL');
$data['logout'] = $this->url->link('account/logout', '', 'SSL');
$data['shopping_cart'] = $this->url->link('checkout/cart');
$data['checkout'] = $this->url->link('checkout/checkout', '', 'SSL');
$data['contact'] = $this->url->link('information/contact');
$data['telephone'] = $this->config->get('config_telephone');
$status = true;
if (isset($this->request->server['HTTP_USER_AGENT'])) {
$robots = explode("\n", str_replace(array("\r\n", "\r"), "\n", trim($this->config->get('config_robots'))));
foreach ($robots as $robot) {
if ($robot && strpos($this->request->server['HTTP_USER_AGENT'], trim($robot)) !== false) {
$status = false;
break;
}
}
}
// Menu
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$data['categories'] = array();
$categories = $this->model_catalog_category->getCategories(0);
foreach ($categories as $category) {
if ($category['top']) {
// Level 2
$children_data = array();
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach ($children as $child) {
$filter_data = array(
'filter_category_id' => $child['category_id'],
'filter_sub_category' => true
);
$children_data[] = array(
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
}
// Level 1
$data['categories'][] = array(
'name' => $category['name'],
'children' => $children_data,
'column' => $category['column'] ? $category['column'] : 1,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
}
}
$data['language'] = $this->load->controller('common/language');
$data['currency'] = $this->load->controller('common/currency');
$data['search'] = $this->load->controller('common/search');
$data['cart'] = $this->load->controller('common/cart');
// For page specific css
if (isset($this->request->get['route'])) {
if (isset($this->request->get['product_id'])) {
$class = '-' . $this->request->get['product_id'];
} elseif (isset($this->request->get['path'])) {
$class = '-' . $this->request->get['path'];
} elseif (isset($this->request->get['manufacturer_id'])) {
$class = '-' . $this->request->get['manufacturer_id'];
} else {
$class = '';
}
$data['class'] = str_replace('/', '-', $this->request->get['route']) . $class;
} else {
$data['class'] = 'common-home';
}
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/header.tpl')) {
return $this->load->view($this->config->get('config_template') . '/template/common/header.tpl', $data);
} else {
return $this->load->view('default/template/common/header.tpl', $data);
}
}
// I have added this
$data['categories'] = array();
$results = $this->model_catalog_manufacturer->getManufacturers();
foreach ($results as $result) {
if (is_numeric(utf8_substr($result['name'], 0, 1))) {
$key = '0 - 9';
} else {
$key = utf8_substr(utf8_strtoupper($result['name']), 0, 1);
}
if (!isset($data['categories'][$key])) {
$data['categories'][$key]['name'] = $key;
}
$data['categories'][$key]['manufacturer'][] = array(
'name' => $result['name'],
'href' => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $result['manufacturer_id'])
);
}
}
这将起作用,请检查。。
这将起作用,请检查。。
我想显示这个$data['categories'][$key]['manufacturer'][]array('name'=>$result['name'],'href'=>$this->url->link('product/manufacturer/info','manufacturer\u id=')。$result['manufacturer\u id']);上面的代码来自manufacturer.php controller,我想将其添加到header.php中controller@Barney斯汀森那我怎么才能得到呢?请帮助meya offcourse@Barney Stinsoni我需要从manufacturer.php controller复制该索引函数并放入header.php controller..对吗?我想显示这个$data['categories'][$key]['manufacturer'][]array('name'=>$result['name'],'href'=>$this->url->链接('product/manufacturer/info','manufacturer\u id=')。$result['manufacturer_id']));上面的代码来自manufacturer.php controller,我想将其添加到header.php中controller@BarneyStinson那么我该怎么做呢?请帮助meya offcourse@Barney Stinson我需要从manufacturer.php controller复制索引函数并放入header.php controller..对吗?请查看上面的注释我想从m从一个控制器到另一个控制器(仅限特定数据)请帮助我如何执行此操作?我使用了上面的用户方法,但出现了致命错误:在第5行的D:\wamp\www\ssass\catalog\controller\common\header.php中调用未定义的方法Session::flashdata(),请参见上面的注释我希望从一个控制器访问到另一个控制器的数据(仅限特定数据)请帮助我如何执行此操作?我使用了上面的用户方法,但出现错误致命错误:调用未定义的方法会话::flashdata()在D:\wamp\www\ssass\catalog\controller\common\header.php第5行中,我添加了重命名categories
,并在header.tpl文件中进行了重命名更改,您的getCategories
函数在哪里?如果它在任何模型中,请将该模型加载到header.php
ya我加载了该模型文件,它仍然显示未定义的变量renamecategories
以及我在部分中添加的其他名称,并且确实需要在header.tpl文件中重命名更改。您的getCategories
函数在哪里?如果它在任何模型中,那么将该模型加载到您的header.php
中我加载了该模型文件,它仍然显示未定义的变量能够的
<?php foreach ($manufacturer as $manufacturers) { ?>
<?php if ($manufacturers['name']){ ?>
<li><?php echo $manufacturers['name']; ?></li>
<?php }?>
<?php }?>