Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Can somebody help me out with a function()??


Guest

Recommended Posts

Hi there,

 

I would need a function to find out which categories are underneith the top category. I know that there is a function the other way around. Let me explain what I mean

 

If I have a look at the "categories" table I find two columns that are of interest (categories_ID and parent_ID). Let's say my top category has the ID 1. Now I would look for all IDs where its parent_ID is 1. Now I would use those results to go deeper. As soon as such a query returns no results anymore I would't have any deeper categories.

 

My problem is 1. I don't know exactly how to write such a function, 2. I am scared that this would generate imense work load just to find that out and 3. maybe such a function already exists?!

 

Another idea could be to set up a table that already contains this information? I suppose this would either need manual or even better automatic generation of this information after changes on categories have been made.

 

Thanks for your ideas in advance ;-)

 

Regards Matt

Link to comment
Share on other sites

Hi Linda,

 

thanks for the quick reply. This function does exactly what I was looking for and it does it the same way I supposed to solve this issue in my first post.

 

I've seen that this functions has been used in on the advanced search page. I do wonder, if the function doesn't slow down too much, so?!

 

Could anybody post his/her view on that? Just have in mind how many SQL queries need to be done in order to retrieve the wanted information...

 

For everyones convenience I'll quote the function underneith.

 

Thanks again and best regards

 

Matt

 

// Return all subcategory IDs

// TABLES: categories

 function tep_get_subcategories(&$subcategories_array, $parent_id = 0) {

   $subcategories_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where parent_id = '" . $parent_id . "'");

   while ($subcategories = tep_db_fetch_array($subcategories_query)) {

     $subcategories_array[sizeof($subcategories_array)] = $subcategories['categories_id'];

     if ($subcategories['categories_id'] != $parent_id) {

       tep_get_subcategories($subcategories_array, $subcategories['categories_id']);

     }

   }

 }

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...