Jump to content

Still need help with error...


laide234

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>";
}
Link to comment
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]
Link to comment
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
Link to comment
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.
Link to comment
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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.