简体   繁体   中英

Undefined index Opencart in home.php

Opencart Version 1.5.5.1

I add some code: in home.php then I display in home.tpl

controller :

<?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]);
            }
        }
    }
}
}
?>

EDIT : MODEL

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;
}

I add code $this->dell(); , I already try var_dump($this->data['product_dell'][1][1]) and it's working.

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

But in display home.tpl it's error like this http://s1064.photobucket.com/user/blackarch01/media/2016-02-25_14-42-09_zpsqvd3odia.png.html?sort=3&o=0

In my case I store data to array in $product[1][1][1] until $product[7][7][7]

1st [1] for grup menu 1-7

2st [1] for sub menu (Eg 1 menu have 5 child)

3st [1] for description for 1 child (like name,dll)

Then in view I using for to display and it's work like this $name = $product_dell[1][$i][0];

It's write undefined index in 'name' it's mean in controller $all_data['name'], and another too.

When i try var_dump($product_dell[1][1]); in View, it's working (success passing variable), idwk why it's error and how to fix this ???

Thank you for comment, I got the answers after I keep trying.

In Open cart (in my case) after passing data to variable from model like this

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

I cant passing data like this ( take data from database like this ):

 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 !!!
    );
 }

So i change variable like this :

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

Then store again to array :

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

And it's working :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM