Imaulle Posted April 24, 2012 Share Posted April 24, 2012 Hello, I'm not able to figure how to fix these warnings / errors I'm getting. The file that I'm creating comes out correctly so it's as if I need some type of conditional to end the function calls or something? I'm really stumped with this one... Undefined index: 1 Invalid argument supplied for foreach() Undefined index: 4 Invalid argument supplied for foreach() Undefined index: 7 Invalid argument supplied for foreach() Undefined index: 21 Invalid argument supplied for foreach() Undefined index: 2 Invalid argument supplied for foreach() Undefined index: 11 Invalid argument supplied for foreach() Undefined index: 6 Invalid argument supplied for foreach() Undefined index: 22 Invalid argument supplied for foreach() Undefined index: 24 Invalid argument supplied for foreach() Here is a var_dump of $menu array(2) { ["items"]=> array(12) { [1]=> object(stdClass)#17 (10) { ["id"]=> string(1) "1" ["name"]=> string(4) "HOME" ["parent"]=> string(1) "0" ["type"]=> string(9) "slideshow" ["order"]=> string(1) "1" ["visible"]=> string(4) "true" ["image"]=> NULL ["color"]=> NULL ["link"]=> NULL ["target"]=> NULL } [5]=> object(stdClass)#18 (10) { ["id"]=> string(1) "5" ["name"]=> string(4) "INFO" ["parent"]=> string(1) "0" ["type"]=> string(6) "parent" ["order"]=> string(1) "2" ["visible"]=> string(4) "true" ["image"]=> NULL ["color"]=> NULL ["link"]=> NULL ["target"]=> NULL } [3]=> object(stdClass)#19 (10) { ["id"]=> string(1) "3" ["name"]=> string(9) "GALLERIES" ["parent"]=> string(1) "0" ["type"]=> string(6) "parent" ["order"]=> string(1) "3" ["visible"]=> string(4) "true" ["image"]=> NULL ["color"]=> NULL ["link"]=> NULL ["target"]=> NULL } [18]=> object(stdClass)#20 (10) { ["id"]=> string(2) "18" ["name"]=> string(16) "GALLERY SECTIONS" ["parent"]=> string(1) "0" ["type"]=> string(14) "gallery_portal" ["order"]=> string(1) "4" ["visible"]=> string(4) "true" ["image"]=> string(11) "DSC0250.jpg" ["color"]=> string(6) "848a5a" ["link"]=> NULL ["target"]=> NULL } [22]=> object(stdClass)#21 (10) { ["id"]=> string(2) "22" ["name"]=> string(7) "CONTACT" ["parent"]=> string(1) "0" ["type"]=> string(5) "forms" ["order"]=> string(1) "5" ["visible"]=> string(4) "true" ["image"]=> NULL ["color"]=> NULL ["link"]=> NULL ["target"]=> NULL } [24]=> object(stdClass)#22 (10) { ["id"]=> string(2) "24" ["name"]=> string(12) "MAILING LIST" ["parent"]=> string(1) "0" ["type"]=> string(5) "forms" ["order"]=> string(1) "6" ["visible"]=> string(5) "false" ["image"]=> NULL ["color"]=> NULL ["link"]=> NULL ["target"]=> NULL } [2]=> object(stdClass)#23 (10) { ["id"]=> string(1) "2" ["name"]=> string(7) "WEDDING" ["parent"]=> string(1) "3" ["type"]=> string(7) "gallery" ["order"]=> string(1) "1" ["visible"]=> string(4) "true" ["image"]=> NULL ["color"]=> NULL ["link"]=> NULL ["target"]=> NULL } [4]=> object(stdClass)#24 (10) { ["id"]=> string(1) "4" ["name"]=> string( "ABOUT US" ["parent"]=> string(1) "5" ["type"]=> string(9) "text_page" ["order"]=> string(1) "1" ["visible"]=> string(4) "true" ["image"]=> NULL ["color"]=> NULL ["link"]=> NULL ["target"]=> NULL } [7]=> object(stdClass)#25 (10) { ["id"]=> string(1) "7" ["name"]=> string( "CALENDAR" ["parent"]=> string(1) "5" ["type"]=> string( "calendar" ["order"]=> string(1) "2" ["visible"]=> string(4) "true" ["image"]=> NULL ["color"]=> NULL ["link"]=> NULL ["target"]=> NULL } [21]=> object(stdClass)#26 (10) { ["id"]=> string(2) "21" ["name"]=> string(14) "PORTFOLIOSITEZ" ["parent"]=> string(1) "5" ["type"]=> string( "external" ["order"]=> string(1) "3" ["visible"]=> string(4) "true" ["image"]=> NULL ["color"]=> NULL ["link"]=> string(25) "http://portfoliositez.com" ["target"]=> string(6) "_blank" } [11]=> object(stdClass)#27 (10) { ["id"]=> string(2) "11" ["name"]=> string(9) "LANDSCAPE" ["parent"]=> string(2) "18" ["type"]=> string(7) "gallery" ["order"]=> string(1) "1" ["visible"]=> string(4) "true" ["image"]=> NULL ["color"]=> NULL ["link"]=> NULL ["target"]=> NULL } [6]=> object(stdClass)#28 (10) { ["id"]=> string(1) "6" ["name"]=> string(9) "PORTRAITS" ["parent"]=> string(2) "18" ["type"]=> string(7) "gallery" ["order"]=> string(1) "2" ["visible"]=> string(4) "true" ["image"]=> NULL ["color"]=> NULL ["link"]=> NULL ["target"]=> NULL } } ["parents"]=> array(4) { [0]=> array(6) { [0]=> string(1) "1" [1]=> string(1) "5" [2]=> string(1) "3" [3]=> string(2) "18" [4]=> string(2) "22" [5]=> string(2) "24" } [3]=> array(1) { [0]=> string(1) "2" } [5]=> array(3) { [0]=> string(1) "4" [1]=> string(1) "7" [2]=> string(2) "21" } [18]=> array(2) { [0]=> string(2) "11" [1]=> string(1) "6" } } } And here is the code public function sitemap() { $query = $this->db->order_by('parent', 'asc')->order_by('order', 'asc')->get('menu'); foreach($query->result() as $row) { $menu['items'][$row->id] = $row; $menu['parents'][$row->parent][] = $row->id; } function build_map($parent, $menu, &$data) { foreach($menu['parents'][$parent] as $itemId) { if(!isset($menu['parents'][$itemId])) { if(($menu['items'][$itemId]->parent == 0) && ($menu['items'][$itemId]->visible != 'false')) { $name = str_replace(' ','-', $menu['items'][$itemId]->name); $data .= "\t<url><loc>" . base_url() . "#!/". $name ."</loc></url>\n"; } elseif($menu['items'][$itemId]->visible != 'false' && $menu['items'][$itemId]->type !='external' && $menu['items'][$itemId]->type != 'parent') { $name = str_replace(' ','-', $menu['items'][$itemId]->name); $name2 = str_replace(' ','-', $menu['items'][$menu['items'][$itemId]->parent]->name); $data .= "\t<url><loc>" . base_url() . "#!/". $name2 ."/" . $name ."</loc></url>\n"; } } if(isset($menu['parents'][$itemId]) && $menu['items'][$itemId]->visible != 'false' && $menu['items'][$itemId]->type != 'parent' && $menu['items'][$itemId]->type !='external') { $name = str_replace(' ','-', $menu['items'][$itemId]->name); $data .= "\t<url><loc>" . base_url() . "#!/". $name ."</loc></url>\n"; } build_map($itemId, $menu, $data); } } $data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n\t<url><loc>".base_url()."</loc></url>\n"; build_map(0, $menu, $data); $data .= "</urlset>"; write_file('sitemap.xml', $data); } Quote Link to comment https://forums.phpfreaks.com/topic/261553-hierarchy-drop-down-list-code-issues/ Share on other sites More sharing options...
MMDE Posted April 24, 2012 Share Posted April 24, 2012 To make this a lot easier for us, could you use print_r on the array, then go into the source of the html page, and copy the structure there, then post it here with code tag. thank you! Quote Link to comment https://forums.phpfreaks.com/topic/261553-hierarchy-drop-down-list-code-issues/#findComment-1340249 Share on other sites More sharing options...
Imaulle Posted April 24, 2012 Author Share Posted April 24, 2012 oh woops! I did not think of that! sorry! Here ya go Array ( [items] => Array ( [1] => stdClass Object ( [id] => 1 [name] => HOME [parent] => 0 [type] => slideshow [order] => 1 [visible] => true [image] => [color] => [link] => [target] => ) [5] => stdClass Object ( [id] => 5 [name] => INFO [parent] => 0 [type] => parent [order] => 2 [visible] => true [image] => [color] => [link] => [target] => ) [3] => stdClass Object ( [id] => 3 [name] => GALLERIES [parent] => 0 [type] => parent [order] => 3 [visible] => true [image] => [color] => [link] => [target] => ) [18] => stdClass Object ( [id] => 18 [name] => GALLERY SECTIONS [parent] => 0 [type] => gallery_portal [order] => 4 [visible] => true [image] => DSC0250.jpg [color] => 848a5a [link] => [target] => ) [22] => stdClass Object ( [id] => 22 [name] => CONTACT [parent] => 0 [type] => forms [order] => 5 [visible] => true [image] => [color] => [link] => [target] => ) [24] => stdClass Object ( [id] => 24 [name] => MAILING LIST [parent] => 0 [type] => forms [order] => 6 [visible] => false [image] => [color] => [link] => [target] => ) [2] => stdClass Object ( [id] => 2 [name] => WEDDING [parent] => 3 [type] => gallery [order] => 1 [visible] => true [image] => [color] => [link] => [target] => ) [4] => stdClass Object ( [id] => 4 [name] => ABOUT US [parent] => 5 [type] => text_page [order] => 1 [visible] => true [image] => [color] => [link] => [target] => ) [7] => stdClass Object ( [id] => 7 [name] => CALENDAR [parent] => 5 [type] => calendar [order] => 2 [visible] => true [image] => [color] => [link] => [target] => ) [21] => stdClass Object ( [id] => 21 [name] => PORTFOLIOSITEZ [parent] => 5 [type] => external [order] => 3 [visible] => true [image] => [color] => [link] => http://portfoliositez.com [target] => _blank ) [11] => stdClass Object ( [id] => 11 [name] => LANDSCAPE [parent] => 18 [type] => gallery [order] => 1 [visible] => true [image] => [color] => [link] => [target] => ) [6] => stdClass Object ( [id] => 6 [name] => PORTRAITS [parent] => 18 [type] => gallery [order] => 2 [visible] => true [image] => [color] => [link] => [target] => ) ) [parents] => Array ( [0] => Array ( [0] => 1 [1] => 5 [2] => 3 [3] => 18 [4] => 22 [5] => 24 ) [3] => Array ( [0] => 2 ) [5] => Array ( [0] => 4 [1] => 7 [2] => 21 ) [18] => Array ( [0] => 11 [1] => 6 ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/261553-hierarchy-drop-down-list-code-issues/#findComment-1340255 Share on other sites More sharing options...
MMDE Posted April 24, 2012 Share Posted April 24, 2012 I guess: if(!isset($menu['parents'][$itemId])) should be: if(isset($menu['parents'][$itemId])) Quote Link to comment https://forums.phpfreaks.com/topic/261553-hierarchy-drop-down-list-code-issues/#findComment-1340258 Share on other sites More sharing options...
Imaulle Posted April 24, 2012 Author Share Posted April 24, 2012 No I think that's how it checks if it's a parent with no children. The 2nd if has if(isset($menu['parents'][$itemId])) without the ! The first if will print items that are parents but have no children. The next elseif will print only children. The last if will print parents that have children Hope that makes sense... Quote Link to comment https://forums.phpfreaks.com/topic/261553-hierarchy-drop-down-list-code-issues/#findComment-1340261 Share on other sites More sharing options...
MMDE Posted April 24, 2012 Share Posted April 24, 2012 No I think that's how it checks if it's a parent with no children. The 2nd if has if(isset($menu['parents'][$itemId])) without the ! The first if will print items that are parents but have no children. The next elseif will print only children. The last if will print parents that have children Hope that makes sense... Don't you get the line number where the error occurs? Try putting error_reporting(E_ALL); at the start of your file. I will look further meanwhile. Quote Link to comment https://forums.phpfreaks.com/topic/261553-hierarchy-drop-down-list-code-issues/#findComment-1340262 Share on other sites More sharing options...
Imaulle Posted April 24, 2012 Author Share Posted April 24, 2012 yes. All errors occur at foreach($menu['parents'][$parent] as $itemId) Quote Link to comment https://forums.phpfreaks.com/topic/261553-hierarchy-drop-down-list-code-issues/#findComment-1340271 Share on other sites More sharing options...
MMDE Posted April 24, 2012 Share Posted April 24, 2012 yes. All errors occur at foreach($menu['parents'][$parent] as $itemId) Oh, I think I found it now. build_map($itemId, $menu, $data); When this is called, it will in the function try to find an index in the array $menu['parents'], for example $menu['parents'][1], but it won't find it if it doesn't already exist in the $menu['parents'] array before this: build_map(0, $menu, $data); No $menu['parents'][1] is set in your function with the array you gave me, but still it will end up trying to do this: foreach($menu['parents'][1] as $itemId) Which results in: Undefined index: 1 Invalid argument supplied for foreach() Quote Link to comment https://forums.phpfreaks.com/topic/261553-hierarchy-drop-down-list-code-issues/#findComment-1340273 Share on other sites More sharing options...
Imaulle Posted April 24, 2012 Author Share Posted April 24, 2012 There an easy fix for this? ermmmm. what doesn't make sense to me is I used basically the same code elsewhere and it works perfectly (though its super messy) Any idea why it works below, but not in the above code? function buildMenu($parent, $menu, $first) { $html = ''; $fullMenu='<div style="display:none">'; if (isset($menu['parents'][$parent])) { if($first === TRUE) { $html .= '<ul id="menuList" class="menu menuFont">'; } else { $html .= '<ul>'; } foreach($menu['parents'][$parent] as $itemId) { if(!isset($menu['parents'][$itemId])) { if($menu['items'][$itemId]->type != 'external') { $a = str_replace(' ','-',$menu['items'][$itemId]->name); $a2='#!/'.$a; $target=''; $typ='typ="m"'; } else { $a = $menu['items'][$itemId]->link; $a2=$a; $target=' target="'.$menu['items'][$itemId]->target.'"'; $typ='typ="e"'; } if($menu['items'][$itemId]->parent == 0) { if($menu['items'][$itemId]->visible != 'false') { $html .= '<li><a class="mainMenu" menuID="'.$menu['items'][$itemId]->id.'" main="' . $menu['items'][$itemId]->name . '" '.$typ.' lnk="'.$menu['items'][$itemId]->name.'" href="'.$a2.'"'.$target.'><h1>'. $menu['items'][$itemId]->name .'</h1></a></li>'; } $fullMenu .= '<span class="fullMenuItem" menuID="'.$menu['items'][$itemId]->id.'" ref="#!/'.$a.'"></span>'; } else { if($menu['items'][$itemId]->type != 'external') { $string = '#!/' . $menu['items'][$menu['items'][$itemId]->parent]->name . '/' . $menu['items'][$itemId]->name ; $string = str_replace(' ','-', $string); } else { $string=$menu['items'][$itemId]->link; } if($menu['items'][$itemId]->visible != 'false' && $menu['items'][$menu['items'][$itemId]->parent]->visible != 'false' && $menu['items'][$menu['items'][$itemId]->parent]->type != 'gallery_portal') { $html .= '<li><a class="subMenu" menuID="'.$menu['items'][$itemId]->id.'" main="' . $menu['items'][$itemId]->name . '" lnk="'.$menu['items'][$itemId]->name.'" href="'.$string.'" style="z-index:300"'.$target.'><h1>'. $menu['items'][$itemId]->name .'</h1></a></li>'; } $fullMenu .= '<span class="fullMenuItem" menuID="'.$menu['items'][$itemId]->id.'" ref="'.$string.'"></span>'; } } if(isset($menu['parents'][$itemId])) { if($menu['items'][$itemId]->type != 'external') { $a = str_replace(' ','-',$menu['items'][$itemId]->name); } else { $a = $menu['items'][$itemId]->link; $a2=$a; $target=' target="'.$menu['items'][$itemId]->target.'"'; } if ($menu['items'][$itemId]->visible != 'false') { if($menu['items'][$itemId]->type != 'gallery_portal') { $html .= '<li><span class="mainMenu" menuID="'.$menu['items'][$itemId]->id.'" main="' . $menu['items'][$itemId]->name . '" typ="h" lnk="'.$menu['items'][$itemId]->name . '"'; } else { $html .= '<li><a class="mainMenu" menuID="'.$menu['items'][$itemId]->id.'" main="' . $menu['items'][$itemId]->name . '" typ="m" lnk="'.$menu['items'][$itemId]->name . '"'; } if ($menu['items'][$itemId]->type == 'gallery_portal') { $string = '#!/' . $menu['items'][$itemId]->name; $string = str_replace(' ','-', $string); $html .= ' href="'.$string.'"'; $html .= '><h1>'.$menu['items'][$itemId]->name .'</h1></a>'; $fullMenu .= '<span class="fullMenuItem" menuID="'.$menu['items'][$itemId]->id.'" ref="'.$string.'"></span>'; } else { $html .= ' onClick="javascript:showSub(this)"'; $html .= '><h1>'.$menu['items'][$itemId]->name .'</h1></span>'; } } $html .= buildMenu($itemId, $menu, FALSE); if($menu['items'][$itemId]->visible != 'false') { $html .= '</li>'; } } } $html .= '</ul>'; $fullMenu .= '</div>'; } return $html.$fullMenu; } return buildMenu(0, $menu, TRUE); } Quote Link to comment https://forums.phpfreaks.com/topic/261553-hierarchy-drop-down-list-code-issues/#findComment-1340276 Share on other sites More sharing options...
MMDE Posted April 24, 2012 Share Posted April 24, 2012 before this: build_map($itemId, $menu, $data); check if $menu['parents'][$itemId] is set, which you already have, perhaps you should create it when it doesn't exist? Quote Link to comment https://forums.phpfreaks.com/topic/261553-hierarchy-drop-down-list-code-issues/#findComment-1340280 Share on other sites More sharing options...
Imaulle Posted April 25, 2012 Author Share Posted April 25, 2012 OOOOOOOOOHHHHHHHHHHHHHHH okay I see what I did. I didn't have the function call in the isset check. I was trying to combine if statements... silly me. THANK YOU!!! Quote Link to comment https://forums.phpfreaks.com/topic/261553-hierarchy-drop-down-list-code-issues/#findComment-1340292 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.