首页 文章

通过控制器中的功能加载视图

提问于
浏览
0

在opencart中选择产品时,我正在尝试将大小图表设置为选项卡 . 为此,我有一个如下的模型

class ModelCatalogSizes extends Model {     
    public function getSizes()
    {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "sizes ORDER BY id");
        return $query->rows;
    }
}

我有视图作为模板,它只是一个带有值作为输入无线电选项的html表 .

在我的产品控制器类中,我有一个如下所示的功能

public function sizes()
{
    $this->language->load('product/product');

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

    $this->data['sizes'] = array();

    $results = $this->model_catalog_sizes->getSizes();

    foreach ($results as $result) {
        $this->data['sizes'][] = array(
            'type'       => ucfirst($result['type']),
            'coat'       => $result['coat'],
            'chest'      => $result['chest'],
            'cverarm'    => $result['overarm'],
            'waist'      => $result['waist'],
            'hip'        => $result['hip'],
            'inseam'     => $result['inseam'],
            'neck'       => $result['neck'],
            'sleeve'     => $result['sleeve'],
            'height'     => $result['height'],
            'weightLb'   => $result['weightLb'],
            'weightKg'   => $result['weightKg'],
        );
    }

    if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/sizes.tpl')) {
        $this->template = $this->config->get('config_template') . '/template/product/sizes.tpl';
    } else {
        $this->template = 'default/template/product/sizes.tpl';
    }

    $this->response->setOutput($this->render());
}

现在我试图通过产品控制器类中的索引函数直接加载此视图,方法是 $this->data['size'] = $this->sizes();

当我在产品视图中回显我的$ size时,什么都没有出现 . 我认为上面函数中构建的整个视图应该显示出来 . 我错了(概率99%)?有人可以帮我直接通过功能发布视图吗?

1 回答

  • 1

    您需要做的是为 index() 方法的子项打开 /catalog/controller/product/product.php 的子项添加路径,并在 index() 方法的底部找到此代码

    $this->children = array(
        'common/column_left',
        'common/column_right',
        'common/content_top',
        'common/content_bottom',
        'common/footer',
        'common/header'
    );
    

    并添加您的路线, 'product/product/sizes' 为您的 sizes() 方法

    $this->children = array(
        'common/column_left',
        'common/column_right',
        'common/content_top',
        'common/content_bottom',
        'common/footer',
        'common/header',
        'product/product/sizes'
    );
    

    然后在您的模板中,您只需要在任何您想要的地方使用 <?php echo $sizes; ?>

相关问题