【问题标题】:Undefined index Opencart in home.phphome.php 中未定义的索引 Opencart
【发布时间】:2016-06-12 00:28:38
【问题描述】:

Opencart 版本 1.5.5.1

我添加了一些代码:在 home.php 中然后我在 home.tpl 中显示

控制器:

<?php  
class ControllerCommonHome extends Controller {
public function index() {
    $this->document->setTitle($this->config->get('config_title'));
    $this->document->setDescription($this->config->get('config_meta_description'));
    $this->data['heading_title'] = $this->config->get('config_title');

    $this->dell(); // Custom

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

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

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

// CUSTOM START HERE -------------------------------
protected function dell() {
    $this->document->setTitle($this->config->get('config_title'));
    $this->document->setDescription($this->config->get('config_meta_description'));
    $this->data['heading_title'] = $this->config->get('config_title');
    $this->load->model('catalog/item');

    for($i=1; $i<=7; $i++)  // START FROM 1
    {
        $menu = array(
            'menu'  => $i,
        );
        $results = $this->model_catalog_item->select_id_dell($menu);

            if(isset($results)){
            $this->data['dell'][] = array(
            $results['show_product_id'],
            $results['head_text'],
            $results['title_text'],
            );
        }
        $this->data['item'] = $this->model_catalog_item->select_item_dell($results);    
        foreach($this->data['item'] as $id){
            $all_data = $this->model_catalog_item->select_description_dll($id);
            if(isset($all_data)){
                $this->data['product_dell'][$i][] = array(
                        $all_data['name'],
                        $all_data['shortDescription'],
                        $all_data['image'],
                        $all_data['price'],

                );
                //var_dump($this->data['product_dell'][1][1]);
            }
        }
    }
}
}
?>

编辑:模型

public function select_description_dll($id){
    $sql = "SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id = p2c.product_id)  WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' && p.product_id = '" . $this->db->escape($id['product_id']) . "' GROUP BY p.product_id";
    //echo $sql; exit;
    $query = $this->db->query($sql);
    return $query->row;
}

我添加代码$this-&gt;dell();,我已经尝试var_dump($this-&gt;data['product_dell'][1][1]),它正在工作。

array(1) { [0]=> array(4) { [0]=> string(5) "AAAAA" [1]=> string(2) "aa" [2]=> string(3) "aaa" [3]=> string(2) "00" } } 

但是在 display home.tpl 中是这样的错误 http://s1064.photobucket.com/user/blackarch01/media/2016-02-25_14-42-09_zpsqvd3odia.png.html?sort=3&o=0

在我的情况下,我将数据存储到 $product[1][1][1] until $product[7][7][7] 中的数组

第一个 [1] 用于 grup 菜单 1-7

2st [1] 用于子菜单(例如 1 个菜单有 5 个子菜单)

3st [1] 用于描述 1 个孩子(如姓名、dll)

然后在视图中我使用for 进行显示,它的工作方式类似于$name = $product_dell[1][$i][0];

它在'name' 中写入未定义的索引,这意味着在控制器$all_data['name'], 和另一个中。

当我在 View 中尝试 var_dump($product_dell[1][1]); 时,它正在工作(成功传递变量),idwk 为什么它是错误的以及如何解决这个问题???

【问题讨论】:

  • 我认为不同,它在控制器中写入错误(同时存储到数组),当我尝试回显时它正在工作,但仍然显示错误。我尝试签入here
  • $this-&gt;model_catalog_item-&gt;select_description_dll($id);分享您的型号代码
  • 完全正确.. select_description_dll() 正在传回数组,该数组不一定包含您要分配的索引。这是唯一可能的解释。这是一个在 SO 上被问了数千次的问题。

标签: php opencart undefined-index


【解决方案1】:

感谢您的评论,我在不断尝试后得到了答案。

Open cart (in my case) 这样的模型中将数据传递给变量后

 $all_data = $this->model_catalog_item->select_description_dll($id);

我不能像这样传递数据(像这样从数据库中获取数据):

 if(isset($all_data)){
    $this->data['product_dell'][$i][] = array(
         $all_data['name'],         // Error undefined index name !!!
         $all_data['shortDescription'], // Error undefined index shoertDescription !!!
         $all_data['image'],        // Error undefined index image !!!
         $all_data['price'],       // Error undefined index price !!!
    );
 }

所以我像这样更改variable

if(isset($all_data['name']))
    $name = $all_data['name'];
else
    $name = null;

然后再次存储到array

if(isset($all_data)){
    $this->data['product_dell'][$i][] = array(
        $name,
        $.....,
    );
}

它正在工作:)

【讨论】:

    猜你喜欢
    • 2014-11-15
    • 2014-02-26
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多