SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    category function help

    hi...
    If i have this mysql table structure for categories and i need recursion to get the subcategories how to do that?
    example...
    Code:
    id  category   parent_id
    1     blabal        0
    2      heheh       0
    3      gigi          1
    4      lala          3
    so i get out

    blabal
    ->gigi
    --->lala
    heheh

    any help appreciated..
    thanx in advance...

  2. #2
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    564
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Hi,

    PHP Code:
    $q1 "SELECT * FROM categories";
    $r1 mysql_query($q1) or die(mysql_error());
    $rows mysql_num_rows($r1);

    while(
    $a1 mysql_fetch_array($r1))
    {
            echo 
    $a1["category"] . "<br>\n";
            
            
    // We check sub categories for this category
            
    $q2 "SELECT * FROM categories WHERE parent_id = " $a1["id"];
            
    $r2 mysql_query($q2) or die(mysql_error());
            
    $rows mysql_num_rows($r2);
            if (
    $rows <> 0)
            {
                    while(
    $a2 mysql_fetch_array($r2))
                    {
                            echo 
    "     " $a2["category"] . "<br>\n";
                    }
            }

    Hope this helps.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you very much but i need a recursive function couse of unlimited subcategories...
    Any other example?
    thanx in advance....

  4. #4
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    564
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    This example can handle any number of categories and sub categories.

    Thanks

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it cant couse it stops after 1 loop
    but ive resolved the problem...
    thanx anyway...
    hth
    PHP Code:
    <?php
    function sub_cat($cat) {
    $value = array(); 
    if(empty(
    $cat))
    $cat 0;
    $ret mysql_query("SELECT * from category WHERE pid = '$cat' ORDER BY id");
    for(
    $i 0$i mysql_num_rows($ret); ++$i) {
    $row mysql_fetch_array($ret);
    $value[$i] = $row[1]."<br>\n";
    $var sub_cat($row[0]);
    for(
    $j 0$j count($var); ++$j) {
    $value[$i] .= str_repeat("-"$cat).$var[$j];
    }
    }
    return 
    $value;
    }

    $var sub_cat("");
    for(
    $i 0$i count($var); ++$i) {
    echo 
    $var[$i];
    }
    ?>
    cheers

  6. #6
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    564
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I am not sure how can you say that it only loops once. According to me the code i have supplied is perfect though i am not sure. But if you got it working then its great.

    Thanks


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
  •