Recursive Look-up Through Category Lineage

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

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

By Perchpole - April 27, 2013

Greg,

This is brilliant - and could have various uses.

Thanks,

Perchpole