Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7515053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:40:44+00:00 2026-05-30T00:40:44+00:00

I have a MySQL table with id, category_id (parent id), and url. I have

  • 0

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 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:40:45+00:00Added an answer on May 30, 2026 at 12:40 am

    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.

    function get_top_parent($category_id, $root_id=0)
    {
        // Grab the id's and category's
        $item_list = array();
        foreach($this->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->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);
            }
        }
    
    }
    

    reworked to return URL (I did not verify this code but it should work):

    function get_top_parent($category_id, $root_id=0) 
    { 
        // Grab the id's and category's 
        $item_list = array(); 
        foreach($this->items as $item) { 
            $item_list[$item['id']] = array(
                'category_id'   => $item['category_id'],
                'url'           => $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->items as $item) { 
            $item_list[$item['id']] = array(
                'category_id' => $item['category_id'],
                'url' => $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'   => $current_category
                    'url'           => $item_list[$current_category]['url']
                );
                // update our current category and parents 
                $current_category = $item_list[$current_category]['category_id']; 
                array_unshift($parents, $temp_array); 
            } 
        } 
    
    } 
    

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table that looks something like this: +-----+------------+ | id |
I have a categories table in MySql something like this: categoryId | categoryTitle |
I have a mysql table named category. the basic structure looks like this- ------
I have something like Facebook's Wall build on PHP that uses MySQL database. Structure:
I'm really upset with Hibernate! I have a database table (mysql) that holds parent-child
This is for MySQL and PHP I have a table that contains the following
I have a table that looks like: What I want to do is output
I have a MySQL table like id | text | category | active I
I have mysql table that has a column that stores xml as a string.
I have MySQL InnoDB table utf-8 encoded. This table has only id and name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.