Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Undefined Index error in displaying Nested While Loops

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
codesoftly1000
Smarty n00b


Joined: 18 Jul 2018
Posts: 1

PostPosted: Wed Jul 18, 2018 3:17 pm    Post subject: Undefined Index error in displaying Nested While Loops Reply with quote

I have a nested while loop that fetches Mega Menus. The while loop returns me the proper array data that I exactly want. I am trying to reflect this in Smarty but I am getting error.

This is my PHP

Code:
    $cats = array();
    $catSQL = $pdo->prepare("SELECT * FROM category");
    $catSQL-> execute();
   
    while($rowCat = $catSQL->fetch()) {
        $cat = array();
        $cat['id']   = $rowCat['cat_id'];
        $cat['name'] = $rowCat['cat_name'];
   
        $childCat = array();
   
        $subCatSQL = $pdo->prepare("SELECT * FROM sub_category WHERE sc_cat = ".$rowCat['cat_id']);
        $subCatSQL-> execute();
   
        while($subCatResult = $subCatSQL->fetch()) {
            $subCatID = $subCatResult['sc_id'];
            $project = $subCatResult;
   
            $childCats = array();
   
            $childCatSQL = $pdo->prepare("SELECT * FROM child_category WHERE cc_subcat=".$subCatID);
            $childCatSQL-> execute();
   
            while($childCatResult = $childCatSQL->fetch()) {
                $childCats[] = $childCatResult;
                $project['ccname'] = $childCats;
            }
            $childCat[] = $project;
        }
        $cat['categories'] = $childCat;
        $cats[] = $cat;
        // echo "<pre>"; print_r($cat);
    }
    $smarty->assign('cats', $cats);


This is my array data from `print_r($cat);`

Code:
    Array
    (
        [id] => 1
        [name] => Computers
        [categories] => Array
            (
                [0] => Array
                    (
                        [sc_id] => 1
                        [0] => 1
                        [sc_cat] => 1
                        [1] => 1
                        [sc_name] => Laptops
                        [2] => Laptops
                        [ccname] => Array
                            (
                                [0] => Array
                                    (
                                        [cc_id] => 1
                                        [0] => 1
                                        [cc_subcat] => 1
                                        [1] => 1
                                        [cc_name] => Hewlett-Packard
                                        [2] => Hewlett-Packard
                                    )
   
                                [1] => Array
                                    (
                                        [cc_id] => 2
                                        [0] => 2
                                        [cc_subcat] => 1
                                        [1] => 1
                                        [cc_name] => Dell
                                        [2] => Dell
                                    )
   
                                [2] => Array
                                    (
                                        [cc_id] => 3
                                        [0] => 3
                                        [cc_subcat] => 1
                                        [1] => 1
                                        [cc_name] => Lenovo
                                        [2] => Lenovo
                                    )
   
                                [3] => Array
                                    (
                                        [cc_id] => 4
                                        [0] => 4
                                        [cc_subcat] => 1
                                        [1] => 1
                                        [cc_name] => Acer
                                        [2] => Acer
                                    )
   
                            )
   
                    )
   
                [1] => Array
                    (
                        [sc_id] => 2
                        [0] => 2
                        [sc_cat] => 1
                        [1] => 1
                        [sc_name] => Desktops
                        [2] => Desktops
                        [ccname] => Array
                            (
                                [0] => Array
                                    (
                                        [cc_id] => 5
                                        [0] => 5
                                        [cc_subcat] => 2
                                        [1] => 2
                                        [cc_name] => Dell
                                        [2] => Dell
                                    )
   
                                [1] => Array
                                    (
                                        [cc_id] => 6
                                        [0] => 6
                                        [cc_subcat] => 2
                                        [1] => 2
                                        [cc_name] => Lenovo
                                        [2] => Lenovo
                                    )
   
                            )
   
                    )
   
            )
   
    )
   
    Array
    (
        [id] => 2
        [name] => Components
        [categories] => Array
            (
                [0] => Array
                    (
                        [sc_id] => 3
                        [0] => 3
                        [sc_cat] => 2
                        [1] => 2
                        [sc_name] => Monitors
                        [2] => Monitors
                    )
   
                [1] => Array
                    (
                        [sc_id] => 4
                        [0] => 4
                        [sc_cat] => 2
                        [1] => 2
                        [sc_name] => Printers
                        [2] => Printers
                    )
   
                [2] => Array
                    (
                        [sc_id] => 5
                        [0] => 5
                        [sc_cat] => 2
                        [1] => 2
                        [sc_name] => Scanners
                        [2] => Scanners
                    )
   
                [3] => Array
                    (
                        [sc_id] => 6
                        [0] => 6
                        [sc_cat] => 2
                        [1] => 2
                        [sc_name] => Web Cameras
                        [2] => Web Cameras
                    )
   
            )
   
    )


This is my Smarty Code

Code:
    {foreach from=$cats item=cat}
        <p>{$cat.name}</p>
        {foreach from=$cat.categories item=subcat}
          <p>{$subcat.sc_name}</p>
          {foreach from=$subcat.ccname item=childcat}
            {$childcat.cc_name}
          {/foreach}
        {/foreach}
      {/foreach}

This is the data with error that I am getting in my page.

Code:
    Computers
   
    Laptops
    Hewlett-Packard Dell Lenovo Acer
   
    Desktops
    Dell Lenovo
   
    Components
   
    Monitors
   
    Notice: Undefined index: ccname in E:\xampp\htdocs\flexicart\tmp\87fa8c3a5ed8262adb884ae139069921fd39e8c8_0.file.layout.tpl.cache.php on line 64
   
    Printers
   
    Notice: Undefined index: ccname in E:\xampp\htdocs\flexicart\tmp\87fa8c3a5ed8262adb884ae139069921fd39e8c8_0.file.layout.tpl.cache.php on line 64
   
    Scanners
   
    Notice: Undefined index: ccname in E:\xampp\htdocs\flexicart\tmp\87fa8c3a5ed8262adb884ae139069921fd39e8c8_0.file.layout.tpl.cache.php on line 64
   
    Web Cameras
   
    Notice: Undefined index: ccname in E:\xampp\htdocs\flexicart\tmp\87fa8c3a5ed8262adb884ae139069921fd39e8c8_0.file.layout.tpl.cache.php on line 64

Here as you can see that though I am getting all the data I need still this Undefined Index error seems to occur out of nowhere. I don't understand why this is happening. I got this reference solution from here https://www.smarty.net/forums/viewtopic.php?p=58031 (the last topic). Please help me. Stuck for long here.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Jul 19, 2018 1:40 pm    Post subject: Reply with quote

Start by reading https://phpdelusions.net/pdo#why
You're using a precise tool, which is PDO, like a stoneage hammer, and write your queries in a way that will get you fired the day your colleagues see them.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP