Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming your DB is like this:</p> <p>(1, 0, URL1), (3, 1, URL3), (4, 3, URL4), (5, 3, URL5), (2, 0, URL2), (6, 2, URL6)</p> <p>Then you just need to walk up the list</p> <p>e.g. </p> <pre><code>function get_top_parent($category_id, $root_id=0) { // Grab the id's and category's $item_list = array(); foreach($this-&gt;items as $item) { $item_list[$item['id']] = $item['category_id']; } $current_category = $category_id; while(TRUE) { if ($item_list[$current_category] == $root_id) { // Check to see if we have found the parent category. return $current_category; } else { // update our current category $current_category = $item_list[$current_category]; } } } function get_parents($category_id, $root_id=0) { $parents = array(); // Grab the id's and category's $item_list = array(); foreach($this-&gt;items as $item) { $item_list[$item['id']] = $item['category_id']; } $current_category = $category_id; while(TRUE) { // Check to see if we have found the root category. if ($item_list[$current_category] == $root_id) { return $parents; } else { // update our current category and parents $current_category = $item_list[$current_category]; array_unshift($parents, $current_category); } } } </code></pre> <p>reworked to return URL (I did not verify this code but it should work):</p> <pre><code>function get_top_parent($category_id, $root_id=0) { // Grab the id's and category's $item_list = array(); foreach($this-&gt;items as $item) { $item_list[$item['id']] = array( 'category_id' =&gt; $item['category_id'], 'url' =&gt; $item['url'] ); } $current_category = $category_id; while(TRUE) { if ($item_list[$current_category]['category_id'] == $root_id) { // Check to see if we have found the parent category. return $item_list[$current_category]['url']; } else { // update our current category $current_category = $item_list[$current_category]['category_id']; } } } function get_parents($category_id, $root_id=0) { $parents = array(); // Grab the id's and category's $item_list = array(); foreach($this-&gt;items as $item) { $item_list[$item['id']] = array( 'category_id' =&gt; $item['category_id'], 'url' =&gt; $item['url'] ); } $current_category = $category_id; while(TRUE) { // Check to see if we have found the root category. if ($item_list[$current_category]['category_id'] == $root_id) { return $parents; } else { $temp_array = array( 'category_id' =&gt; $current_category 'url' =&gt; $item_list[$current_category]['url'] ); // update our current category and parents $current_category = $item_list[$current_category]['category_id']; array_unshift($parents, $temp_array); } } } </code></pre> <p>The first function returns the URL, the second function should return an array of arrays... You will have the standard index, with "category_id" and "url" as nested/sub arrays... (If in doubt, just do a print_r of the return value to see what I mean)</p> <p>again, i checkced the origional code but not the update...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload