Recursive Look-up Through Category Lineage

3 posts by 2 authors in: Forums > CMS Builder
Last Post: April 27, 2013   (RSS)

By Perchpole - April 26, 2013

Hello, All

I'm trying to think-up a method for recursively looking back through a category lineage in search of a value. The process would begin with the selectedCategory:

If the value isset, then use that value.

If there is no value then check to see if there is a parent category. If yes and if the value isset, then use that value.

If there is no value then check to see if there is a parent category....

And so on and so forth until a value is found or the selectedCategory runs out of ancestors.

How could I set this up?

:0)

Perch

Hi Perch,

This is a great Friday morning puzzler. 

I've written a recursive function that can be used on a category section to find the closest parent field with a value:

  function getClosestParentWithTrue($section, $field, $recordNum){
    //Retrieve the record
    $record = mysql_get($section, $recordNum);
    if($record){
      //If the chosen field has a value
      if(@$record[$field]){
        return $record;
      //If the chosen field doesn't have a value rerun the function 
      }elseif(@$record['parentNum']){
        return getClosestParentWithTrue($section,$field,$record['parentNum']);
      }
    }
    //If nothing else is found return an empty array
    return array();
  }
  $item = getClosestParentWithTrue('animal_category','highlight',12);
  
  showme($item);

So the system will get the record num from the section passed into the function, then if a record exists it will check if the field has a value, if it has it will return the value. If it doesn't have a value, the function will run again but using the records parent record num.

Cheers

Greg

Greg Thomas







PHP Programmer - interactivetools.com