Jump to content

Archived

This topic is now archived and is closed to further replies.

laide234

Still need help with error...

Recommended Posts

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

Share this post


Link to post
Share on other sites
Try
$name = $value[$id]['name'];
$children = $value[$id]['children'];

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.ini
error_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!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.