Jump to content



Photo
- - - - -

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


This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1   mattkhan

mattkhan
  • Members
  • 63 posts

Posted 20 May 2003 - 18:10

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

#2   Ajeh

Ajeh
  • Members
  • 6,327 posts

Posted 20 May 2003 - 18:16

Have you looked at the function tep_get_subcategories in the /includes/functions/general.php file?

#3   Ajeh

Ajeh
  • Members
  • 6,327 posts

Posted 20 May 2003 - 18:18

Or try making new function based on the function tep_has_category_subcategories

#4   mattkhan

mattkhan
  • Members
  • 63 posts

Posted 20 May 2003 - 18:57

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']);
     }
   }
 }