Jump to content

Issue with variable in function array_key_exists


son
 Share

Recommended Posts

I use the following data and function successfully on another page that has lots of other stuff going on (and a test page with only this bit):

while (list($catID, $catNameSelect, $catParentSelect) = mysqli_fetch_array($catResult, MYSQLI_BOTH))  {  $categoryNR[(string)$catID] = $catID;  $categoryNames[(string)$catID] = $catNameSelect;	  $catParent = (string)$catParentSelect;	   if(!array_key_exists($catParent, $childrenTree))  $childrenTree[$catParent] = array();  $childrenTree[$catParent][] = (string)$catID;  }function renderTree($parent = 0, $subCounter = 0){global $cid;global $categoryNR;    global $categoryNames;    global $childrenTree;	 if($parent != 0)  {  echo "<option value=\"webster.php?cid=" . $categoryNR[$parent] .  "\"";  echo " style=\"padding-left:" . $subCounter . "px;\">", $categoryNames[$parent], "</option>\n";  $subCounter = $subCounter + 10;  }    $children = $childrenTree[$parent];	 if(count($children) > 0)  { //If node has children	    foreach($children as $child)	    renderTree($child, $subCounter);	 }  }renderTree();  //This renders the hierarchical tree

However, on the relevant page it shows drop down ok, but when you look in code you can see that there is an issue...Undefined variable: childrenTreearray_key_exists() [<a href=function.array-key-exists'>function.array-key-exists</a>]: The second argument should be either an array or an object I do not get this. It is working fine on other page/s with same query and the lot. Why is it saying that childrenTree is not defined in this case? Where could I look? Thanks,Son

Link to comment
Share on other sites

In my desperation I have tried already above this bit of code: $childrenTree = 0; $childrenTree = ''; $childrenTree = FALSE;The first about undefined variable indeed gone, but still 'The second argument should be either an array or an object'. In addition, I did not do anything else on other pages where it is working... Could it be that other variable infringe with the code? I do not have another $childrenTree, but maybe another one is causing this? Son

Link to comment
Share on other sites

But this is where the my problem is to understand. I only want to initialise when there are children as

if(!array_key_exists($catParent, $childrenTree))  $childrenTree[$catParent] = array();  $childrenTree[$catParent][] = (string)$catID;

However, going through all the thrown error codes after 'Undefined variable: childrenTree' it also repeatetly complains 'Undefined index'. Checking all those indexes in detail I can clearly see that those are all the ones without children. The one category with children does not come up in error code. Also, that it works on other page exactly like it is totally confuses me... Any ideas where else I could check? Son

Edited by son
Link to comment
Share on other sites

If you're getting an error that $childrenTree is undefined in the while loop when you're building the arrays, the solution is to initialize that variable to an empty array before the loop like I showed. Inside the function that prints everything you also want to check if the $parent index is set before trying to access it.

Link to comment
Share on other sites

I did set

$childrenTree = array();

just before loop as suggested and had then also added a check if $parent is set in function as

	 if(isset($parent) && $parent != 0)  {

I still get the same 'Undefined index: ' for all the entries that are not parents themselves... The one that is a parent to two entries is fine... Son

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...