php - Problems trying to find the root category based on category id -


i trying figure out root level category_id of category based on it's parent_id.

in function below $this->cat_arr array of categories , parent_id's.

if parent_id set 0 assumed root node. wish loop through array until find parent_id of passed $cat_id.

function getroot($cat_id) {     foreach ($this->cat_arr $row) {         if ($row->cat_id == $cat_id && $row->parent_id == 0) {             return $row->cat_id;             break;         } else {             $this->getroot($row->parent_id);         }     } } 

in application call function when know $cat_id not @ root level (because parent_id greater 0), when try run gets stuck in infinite loop.

is logic flawed, or missing simple?

try this:

function getroot($cat_id) {     foreach ($this->cat_arr $row) {         if ($row->cat_id == $cat_id && $row->parent_id == 0) {             return $row->cat_id;         }     }     return $this->getroot($row->parent_id); } 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -