laide234 Posted August 11, 2006 Share Posted August 11, 2006 Notice: Undefined index: name in [b]/admin/product/modify.php[/b] on line 45 (in red text).Any help would be appreciated. My code is below// get category list$sql = "SELECT cat_id, cat_parent_id, cat_name FROM tbl_category ORDER BY cat_id";$result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error());$categories = array();while($row = dbFetchArray($result)) { list($id, $parentId, $name) = $row; if ($parentId == 0) { $categories[$id] = array('name' => $name, 'children' => array()); } else { $categories[$parentId]['children'][] = array('id' => $id, 'name' => $name); }} //echo '<pre>'; print_r($categories); echo '</pre>'; exit;// build combo box options$list = '';foreach ($categories as $key => $value) { [color=red]$name = $value['name'];[/color] $children = $value['children']; $list .= "<optgroup label=\"$name\">"; foreach ($children as $child) { $list .= "<option value=\"{$child['id']}\""; if ($child['id'] == $cat_id) { $list .= " selected"; } $list .= ">{$child['name']}</option>"; } $list .= "</optgroup>";} Quote Link to comment https://forums.phpfreaks.com/topic/17265-still-need-help-with-error/ Share on other sites More sharing options...
corbin Posted August 11, 2006 Share Posted August 11, 2006 Try $name = $value[$id]['name'];$children = $value[$id]['children']; Quote Link to comment https://forums.phpfreaks.com/topic/17265-still-need-help-with-error/#findComment-73236 Share on other sites More sharing options...
laide234 Posted August 11, 2006 Author Share Posted August 11, 2006 When I tried $name = $value[$id]['name'];I got multiple errors, all reading:Notice: Undefined index: 966 in /admin/product/modify.php on line 45 Quote Link to comment https://forums.phpfreaks.com/topic/17265-still-need-help-with-error/#findComment-73268 Share on other sites More sharing options...
laide234 Posted August 11, 2006 Author Share Posted August 11, 2006 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/17265-still-need-help-with-error/#findComment-73308 Share on other sites More sharing options...
sasa Posted August 11, 2006 Share Posted August 11, 2006 try for debug[code]if ($parentId == 0) { $categories[$id] = array('name' => $name, 'children' => array()); } else { if(isset($categories[$parentID]['name'])) { echo "Data error for line $id name: $name. ParentID $parentID is NOT main cat.";exit; } $categories[$parentId]['children'][] = array('id' => $id, 'name' => $name); }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17265-still-need-help-with-error/#findComment-73360 Share on other sites More sharing options...
kenrbnsn Posted August 11, 2006 Share Posted August 11, 2006 Before the foreach statement put in a debug line:[code]<?php echo '<pre>' . print_r($catagories,true) . '</pre>'; ?>[/code]This will show you how your array is structured and from that you should be able to deduce where the problem is.Ken Quote Link to comment https://forums.phpfreaks.com/topic/17265-still-need-help-with-error/#findComment-73418 Share on other sites More sharing options...
akitchin Posted August 11, 2006 Share Posted August 11, 2006 undefined indeces are just notices from the parser saying that you are assigning a value to an array when its index was not properly initiated. for example:[code]<?php$array = array( 'index1' => '', 'index2' => '')$array['index1'] = 'stuff';?>[/code]will not spit out a notice for this. however:[code]<?php$array = array();$array['index1'] = 'stuff';?>[/code]will, because you haven't explicitly defined "index1" as an index of $array before assigning a value for it. these do not stop the processing, nor do they produce errors (thus they are NOTICES and not ERRORS). to get rid of them, either clean up your code (which will likely be a pain for you), or turn down your error reporting. look up error_reporting() in the manual. Quote Link to comment https://forums.phpfreaks.com/topic/17265-still-need-help-with-error/#findComment-73424 Share on other sites More sharing options...
laide234 Posted August 12, 2006 Author Share Posted August 12, 2006 Thanks a bunch, akitchin.Adding the error_reporting() thing to my code suppressed this notice (I just learnt about notices and errors ::))[code]// Report all errors except E_NOTICE// This is the default value set in php.inierror_reporting(E_ALL ^ E_NOTICE);[/code]Still trying to clean up the code so I dont get any notices, but this will more than do for now. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/17265-still-need-help-with-error/#findComment-73524 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.