I have a MySQL table with id, category_id (parent id), and url.
I have a class that looks something like this. I have removed all unnecessary functions.
class categoriesBuilder
{
var $items = array();
var $html = array();
function fetch_assoc_all( $sql )
{
$result = mysql_query( $sql, $this->conn );
if ( !$result ){
return false;
}
$assoc_all = array();
while( $fetch = mysql_fetch_assoc( $result ) ){
$assoc_all[] = $fetch;
}
mysql_free_result( $result );
return $assoc_all;
}
function get_categories()
{
$sql = 'SELECT id, category_id, url FROM categories ORDER BY category_id, id;';
return $this->fetch_assoc_all( $sql );
}
function get_category_string($root_id=0)
{
$this->html = array();
$this->items = $this->get_categories();
foreach ( $this->items as $item )
$children[$item['category_id']][] = $item;
// loop will be false if the root has no children
$loop = !empty( $children[$root_id] );
// initializing $parent as the root
$parent = $root_id;
$parent_stack = array();
while ( $loop && ( ( $option = each( $children[$parent] ) ) || ( $parent > $root_id ) ) )
{
if ( $option === false )
{
$parent = array_pop( $parent_stack );
}
elseif ( !empty( $children[$option['value']['id']] ) )
{
array_push( $parent_stack, $option['value']['category_id'] );
$parent = $option['value']['id'];
// HTML for menu item containing childrens (open)
$this->html[] = $option['value']['url'] . "/";
}
else
{
$this->html[] = $option['value']['url'] . "/";
}
}
return implode($this->html );
}
}
I need a function that returns only the top level parent. there may be several sub categories.
i.e.
- category id 1
- category id 3
- category id 4
- category id 5
- category id 3
- category id 2
- category id 6
In this example if i entered 3, 4 or 5 into the function the output would be 1. if i entered 6 i would get 2.
I also need a similar function that shows all parent folders.
for example if i entered 3 i would get 1 but if i entered 4 or 5 i would get 1-3
Thanks for your help.
Assuming your DB is like this:
(1, 0, URL1),
(3, 1, URL3),
(4, 3, URL4),
(5, 3, URL5),
(2, 0, URL2),
(6, 2, URL6)
Then you just need to walk up the list
e.g.
reworked to return URL (I did not verify this code but it should work):
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)
again, i checkced the origional code but not the update…