2013-10-01 54 views
0

我剛從Opencart開始。我需要有品牌列表,比如llist of catgories。我試試這樣:opencart如何添加廠商列表如類別列表

<div class="box"> 
    <div class="box-heading"><?php echo "По брендам" ?></div> 
    <div class="box-content"> 
    <ul class="box-category"> 
     <?php foreach ($manufacturers as $manufacturer) { ?> 
     <li> 
     <?php if ($manufacturer['manufacturer_id'] == $manufacturer_id) { ?> 
     <a href="<?php echo $manufacturer['href']; ?>" class="active"><?php echo $manufacturer['name']; ?></a> 
     <?php } else { ?> 
     <a href="<?php echo $manufacturer['href']; ?>"><?php echo $manufacturer['name']; ?></a> 
     <?php } ?> 
     <?php if ($manufacturer['children']) { ?> 
     <ul> 
      <?php foreach ($manufacturer['children'] as $child) { ?> 
      <li> 
      <?php if ($child['manufacturer_id'] == $child_id) { ?> 
      <a href="<?php echo $child['href']; ?>" class="active"> - <?php echo $child['name']; ?></a> 
      <?php } else { ?> 
      <a href="<?php echo $child['href']; ?>"> - <?php echo $child['name']; ?></a> 
      <?php } ?> 
      </li> 
      <?php } ?> 
     </ul> 
     <?php } ?> 
     </li> 
     <?php } ?> 
    </ul> 
    </div> 
</div> 

我在default/template/module/category.tpl文件中添加了這段代碼。 我得到這個錯誤:注意:未定義變量:製造商在....

我應該怎麼做? 謝謝。

+3

當Opencart的開始,請先閱讀偉大來自** Jay Gilford的帖子**:[如何成爲OpenCart的大師](http://stackoverflow.com/questions/13478995/how-to-become-an-opencart-guru)。那麼你應該能夠進一步移動。 – shadyyx

回答

4

category.tpl類別控制器的模板。其目的是顯示類別。嘗試引導您的瀏覽器到http://your.site/index.php?route=product/manufacturer,你會得到製造商名單。該模板是/catalog/view/theme/default/template/product/manufacturer_list.tpl

您的錯誤是由一件簡單的事情造成的:$manufacturers變量未在ControllerModuleCategory控制器(又名category.php)中定義。如果你想在一個模板中使用一個變量,你必須在相關的控制器中定義它。

1)在/catalog/model/catalog/manufacturer.php文件(模型ModelCatalogManufacturer:

爲了獲得準確的/catalog/view/theme/default/template/module/category.tpl的廠商名單(即使它不是這個模塊的目的),你需要一些東西來完成)添加功能(該功能將幫助您獲得的製造商從數據庫列表):

public function getManufacturerByCategory($category_id) { 
    $query = $this->db->query("SELECT m.* 
     FROM " . DB_PREFIX . "product p 
     RIGHT JOIN " . DB_PREFIX . "product_to_category p2c ON 
      p.product_id = p2c.product_id 
     LEFT JOIN " . DB_PREFIX . "manufacturer m ON 
      p.manufacturer_id = m.manufacturer_id 
     WHERE 
      p2c.category_id = " . (int)$category_id . " AND 
      m.manufacturer_id IS NOT NULL 
     GROUP BY m.manufacturer_id"); 
    return $query->rows; 
} 

2)在/catalog/controller/module/category.php文件(又名ControllerModuleCategory控制器),之前if (file_exists(DIR_TEMPLATE . $this->config->get('confi...插入代碼:

if (isset($this->request->get['path'])) { 
    $category_id = array_pop($parts); 

    $this->load->model('catalog/manufacturer'); 
    $this->data['manufacturers'] = $this->model_catalog_manufacturer->getManufacturerByCategory($category_id); 
} else { 
    $this->data['manufacturers'] = array(); 
} 

(記住$this->data['manufacturers']從控制器將可在模板中作爲$manufacturers$this->data['categories']將是$categories$this->data['another_var']$another_var等。)

而且,由於時刻,你可以使用類模塊模板您foreach ($manufacturers as $manufacturer)(你曾嘗試foreach,當您收到「未定義的地方變量「錯誤)以便輸出適用於當前類別產品的製造商列表。

最好的方法是創建另一個模塊,「製造商」模塊,而不是非法使用類別模塊。但我不確定你現在是否準備好接受這個挑戰。

順便說一句,我全部爲shadyyx's link傑伊吉爾福德的指導。這對於初學者來說是非常棒的快速入門指南。

2

我輕解: 就在控制器中添加代碼:category.php:

$this->load->model('catalog/manufacturer'); 
$this->data['mans'] = $this->data['manufacturers'] = 
$this->model_catalog_manufacturer->getManufacturers(); 

,並查看:category.tpl:

<?php 
     foreach($mans as $mans_value) 
     { 
      var_dump($mans_value); 
     } 
?>