Forum Moderators: coopster
If section ABOUT US has the id_section = 18, then
get_seccion_trace(18) should return:
$arr[0] = 'About Us'
$arr[1] = 'Our Company'
$arr[2] = 'Start'
Then, i use something like this to make it legible for the final user:
$arr = array_reverse($hash);
//this prints Start > Our Company > About Us
echo implode(' » ',$arr);
Till know everything works fine. I was just wondering: Is there a any
way to optimize the function? I dont know if the -while loop- could
consume less resources with another recursive algorithm ... Here's the
code:
function get_seccion_trace($id) {
global $cnn;
$trace = array();
$sql = 'SELECT * FROM section WHERE id_section='.q($id);
$rs = &$cnn->Execute($sql);
while(!$rs->EOF) {
$trace[] = $rs->fields['title'];
$sql = 'SELECT * FROM section WHERE
id_section='.$rs->fields['id_section_parent'];
$rs = &$cnn->Execute($sql);
}
return $trace;
}
Thanks,
Andres S.
Gye, Ecuador
Waht I'm asking in the simplest terms is why are you doing this:
$rs = &$cnn->Execute($sql);
Instead of this
$rs = $cnn->Execute($sql);
As of PHP4, PHP uses a system called "reference counting [zend.com]" which means that by default variables are passed by reference and assigned by reference, with copies only being made once a new value is assigned to one of the references. The except to this is with objects, so when you assign
$rs = &$cnn->Execute($sql);
In PHP4 you are making a copy of the object returned by the Execute() function. As I mentioned, in PHP5, objects behave like any other variable, so no copy will get made until you try to modify one object or the other. Since you probably don't have access to the original object within $cnn (unless it's assigned to $this->rs before getting returned), there's not much worry there. So in PHP5 the reference operator is just confusing and gives no performance boost.
If you are using PHP4, you may get a significant or insignificant performance bump depending on the nature of the $rs object.
There's no performance loss by using the & in either case, but in the name of performance you may have inadvertently set up a variable alias to the $cnn object and that could get confusing, but probably not if the $rs gets thrown away relatively quickly and you never attempt to modify it or assign values to it.