drt12345678 2013-11-17 12:42
浏览 71
已采纳

如何在opencart admin / catalog / product_form.tpl上获取所有类别?

I'm using opencart 1.5.6 and what I'm trying to do is add a simple select with all the categories and children categories as option on product_form.tpl.

I tried add to the follow code at admin/controler/catalog/product.php:

    $this->load->model('catalog/category');

    $results = $this->model_catalog_category->getCategories(0);

    foreach ($results as $result) {

        $this->data['categories'][] = array(
            'category_id' => $result['category_id'],
            'name'        => $result['name']
        );
    }

and for the view admin/view/catalog/product_form.tpl:

<tr>
              <td>Categories</td>
              <td><select>
                      <option value="">All Categories</option>
                      <?php foreach ($categories as $category) { ?>
                      <option value="<?php echo $category['name']; ?>"><?php echo $category['name']; ?></option>
                      <?php } ?>
                  </select>
              </td>
          </tr>

but it's not working. It returns nothing.

I wish someone could help me and tell me what I'm missing.

  • 写回答

2条回答 默认 最新

  • dongpengyu1363 2013-11-18 09:41
    关注

    I think Your approach is good but one small mistake - the method getCategories() in administration part does not receive the $parent_id paramaters, but array $data parameter (for sorting and filtering possibilities). So if You want to load the categories in Your product controller, You can proceed like this (I am reusing Your code):

    $this->load->model('catalog/category');
    
    $this->data['categories'] = array();
    
    foreach ($this->model_catalog_category->getCategories(array()) as $category) {
        $this->data['categories'][] = array(
            'category_id' => $category['category_id'],
            'name'        => $category['name']
        );
    }
    

    And in Your template:

          <?php if($categories) { ?>
          <tr>
              <td>Categories</td>
              <td><select>
                      <option value="">All Categories</option>
                      <?php foreach ($categories as $category) { ?>
                      <option value="<?php echo $category['category_id']; ?>"><?php echo $category['name']; ?></option>
                      <?php } ?>
                  </select>
              </td>
          </tr>
          <?php } ?>
    

    I think this is almost the same as what You had, do not understand why it didn't work for You...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA