SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard Busch's Avatar
    Join Date
    Jan 2004
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with recursive function

    I can't get my recursive function to append a variable ($sub_cats) each time a query is run.

    CODE:
    PHP Code:
    $this_cat_id $arg['category'];

    function 
    get_all_sub_cats ($this_cat_id$sub_cats='') {

        
    $sub_cats_result mysql_query("SELECT category_id, category_parent FROM default_categories WHERE category_parent = '" $this_cat_id "'"); 
        
    //echo $sub_cats_result;
        
    if (@mysql_num_rows($sub_cats_result) >= 1) {
            while (
    $sub_cat mysql_fetch_array($sub_cats_result)) {
                
    $this_cat_id $sub_cat['category_id'];
                
    $sub_cats .= $sub_cat['category_id'] . "','";
                
    get_all_sub_cats ($this_cat_id$sub_cats);
            }
        }            
        return 
    $sub_cats;
    }


    $all_sub_cats get_all_sub_cats ($this_cat_id$sub_cats); 
    I want to keep appending $sub_cats with the sub category ID's. But for a category that has 4 sub-categories it's only returning 1 sub-category deep.

    What am I doing wrong?

  2. #2
    SitePoint Member etheon's Avatar
    Join Date
    Aug 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, you're not using the returned value of your recursive function.

    The code should be :

    PHP Code:
    $this_cat_id $arg['category'];

    function 
    get_all_sub_cats ($this_cat_id$sub_cats='') {

        
    $sub_cats_result mysql_query("SELECT category_id, category_parent FROM default_categories WHERE category_parent = '" $this_cat_id "'"); 
        
    //echo $sub_cats_result;
        
    if (@mysql_num_rows($sub_cats_result) >= 1) {
            while (
    $sub_cat mysql_fetch_array($sub_cats_result)) {
                
    $this_cat_id $sub_cat['category_id'];
                
    $sub_cats .= $sub_cat['category_id'] . "','";
                
    $sub_cats .= get_all_sub_cats ($this_cat_id$sub_cats);
            }
        }            
        return 
    $sub_cats;
    }


    $all_sub_cats get_all_sub_cats ($this_cat_id$sub_cats); 

  3. #3
    SitePoint Wizard Busch's Avatar
    Join Date
    Jan 2004
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that was it! thanks and welcome to SP.

    eric

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Functions are so much easier to understand and go though when you keep them simple. Also you should do some checking on the var that is being submitted.

    PHP Code:
    function get_all_sub_cats ($id$sub '') {

        
    // Sanity Check
        
    if (!is_scalar($id)) { return -1; }
        
    $id mysql_real_escape_string(stripslashes((string)$id));

        
    $results mysql_query("SELECT category_id FROM default_categories WHERE category_parent = '" $id "'");

        if (
    mysql_num_rows($results) > 0) {

            while (
    $row mysql_fetch_assoc($results)) {

                
    $sub .= $sub_cat['category_id'] . "','";
                
    $sub .= get_all_sub_cats($row['category_id'], $sub);

            }

        }

        return 
    $sub;


    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •