Thanks to all of you for your explanation of my method. Minuk, thanks for
the detailed explanation, it really helps me understand what I'm doing
wrong.
Now I have one more problem. When the $children array is empty, it returns a
10 instead of a 0. Why is that? Here's the method again with the recommended
improvements you guys provided:
function get_children($parent) {
// FORMULA: rgt value of first child + 1 = left value of second
child
// check rgt value of parent against each child (rgt + 1).
// If rgt valueof child plus 1 equals rgt value of parent
then
// that child is the last child of the parent.
$parent_coord = tree::get_item_coord($parent);
$descendants = $this->get_descendants($parent);
$children = array();
for ($i = 0; $i < count($descendants); $i++) {
if ((empty($children)) && (($parent_coord['0']['lft'] + 1) ==
$descendants[$i]['lft'])) {
$children[] = $descendants[$i]; // found first child
}
elseif (($children[count($children)-1]['rgt'] + 1 ==
$descendants[$i]['lft']) && $children[count($children)-1]['rgt'] + 1 !=
$parent_coord['0']['rgt']) {
$children[] = $descendants[$i]; // find next children
}
}
if (count($children)>0) {
return $children;
} else {
return 0;
}
}
----------------------------------------------------------------------------
-----Original Message-----
From: Minuk Choi [mailto:[EMAIL PROTECTED]
Sent: Sunday, October 10, 2004 1:55 AM
To: [EMAIL PROTECTED]
Subject: Re: [PHP]
I can tell you from a quick glance that you have the following case
function X()
{
if ()
{
$children=...
}
elseif ()
{
$children=...
}
return $children;
}
Have you tried giving $children an initial value, such as
function X()
{
$children = array();
if ()
{
$children[]=...
}
elseif ()
{
$children[]=...
}
if (count($children)>0)
return $children;
return 0;
}
I may be getting a head of myself, but the
$children[]=...
line adds the $descendants into the ARRAY variable, $children. Unless I'm
wrong, you MUST declare $children as an ARRAY FIRST(see first line of the
function, "$children=array();")
Secondly, if you want the function to return 0 if no $descendant entries are
added to $children, you must check to see if the $children ARRAY is EMPTY.
if (count($children)>0)
return $children;
that line basically says if ARRAY variable $children has more than 0
elements, return that ARRAY.
Finally,
return 0;
if the ARRAY variable $children has 0 elements(count($children) returns the
number of elements in ARRAY variable, $children), "return $children" is NOT
executed. The next line is "return 0".
This is a lazy way of writing the following :
if (count($children)>0)
return $children;
else
return 0;
If it seems like I'm explaining too much, it's because from the code
snipplet you provided, it sounds like you're a novice to PHP... and you did
write, '...explain this error to me'. :-P
Hope This Helps
-Minuk
----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, October 09, 2004 10:29 PM
Subject: [PHP]
> Hello everyone,
>
> I'm hoping you guys can help me with this error. Here's what I'm getting:
>
> Notice: Undefined variable: children in
> L:\localhost\catalog\catalog_0.1.0\includes\classes\tree.class.php on line
> 58
>
> This error occurs only when the class's method outputs $children as having
> a
> value of zero. Below is the class. I want it to return zero but I don't
> want
> this error. Do I need to change my error settings in my php.ini file? The
> error level is currently set to E_ALL and I'm using PHP 5.0.1. If at all
> possible, I want to keep this error setting at high because I want
> everything to be coded strictly. What did I do wrong in my class and how
> can
> I fix it to where it doesn't output an error when a class's method returns
> a
> value of zero? Here's the class:
>
> function get_children($parent) {
> $parent_coord = tree::get_item_coord($parent);
> $descendants = $this->get_descendants($parent);
> for ($i = 0; $i < count($descendants); $i++) {
> if ((empty($children)) && (($parent_coord['0']['lft'] + 1) ==
> $descendants[$i]['lft'])) {
> $children[] = $descendants[$i]; // found first child
> }
> elseif (($children[count($children)-1]['rgt'] + 1 ==
> $descendants[$i]['lft']) && $children[count($children)-1]['rgt'] + 1 !=
> $parent_coord['0']['rgt']) {
> $children[] = $descendants[$i]; // find next children
> }
> }
> return $children;
> }
>
>
> Thanks in advance to anyone that can explain this error to me.
>
> Nilaab
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php