Jump to content


Photo

Still need help with error...


  • Please log in to reply
7 replies to this topic

#1 laide234

laide234
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 11 August 2006 - 05:47 PM

Notice: Undefined index: name in /admin/product/modify.php 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) {
$name    = $value['name'];
$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>";
}

#2 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 11 August 2006 - 05:52 PM

Try
$name = $value[$id]['name'];
$children = $value[$id]['children'];
Why doesn't anyone ever say hi, hey, or whad up world?

#3 laide234

laide234
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 11 August 2006 - 06:26 PM

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



#4 laide234

laide234
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 11 August 2006 - 07:51 PM

Anyone?

#5 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 11 August 2006 - 09:58 PM

try for debug
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);   
   }


#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 August 2006 - 11:20 PM

Before the foreach statement put in a debug line:
<?php echo '<pre>' . print_r($catagories,true) . '</pre>'; ?>
This will show you how your array is structured and from that you should be able to deduce where the problem is.

Ken

#7 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 11 August 2006 - 11:28 PM

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:

<?php
$array = array
(
  'index1' => '',
  'index2' => ''
)

$array['index1'] = 'stuff';
?>

will not spit out a notice for this.  however:

<?php
$array = array();

$array['index1'] = 'stuff';
?>

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.

#8 laide234

laide234
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 12 August 2006 - 02:27 AM

Thanks a bunch, akitchin.

Adding the error_reporting() thing to my code suppressed this notice (I just learnt about notices and errors ::))

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

Still trying to clean up the code so I dont get any notices, but this will more than do for now.

Thanks again!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users