SitePoint Sponsor

User Tag List

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

    recursion count question

    hi...
    which is the best way to count how many times a function make a recursion on itself?
    any suggestion appreciated..
    thanx in advance...

  2. #2
    SitePoint Enthusiast michelangelo's Avatar
    Join Date
    May 2005
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try using a global variable, you can even just use $_GLOBALS['my_var'] and away you go.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you very much...
    cheers

  4. #4
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Quote Originally Posted by michelangelo
    [...], you can even just use $_GLOBALS['my_var'] and away you go.
    The variable is called $GLOBALS, not $_GLOBALS.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hth about adding or deleting a category...
    when u delete the category it cancel from database all pending categories of the one which u delete...
    hth
    PHP Code:
    <form method="post" action="<?php $_SERVER["PHP_SELF"]; ?>">
    <?php
    include_once("config.php");

    if(!empty(
    $_POST["gocat"])) {
    $catname $_POST["pcat"];
    if(empty(
    $_POST["catsp"])) {
    $ret "INSERT INTO category VALUES('', '$catname', '0')";
    $ret mysql_query($ret);
    } else {
    foreach(
    $_POST["catsp"] as $value) {
    $ret "INSERT INTO category VALUES('', '$catname', '$value')";
    $ret mysql_query($ret);
    }
    }
    }

    if(!empty(
    $_POST["godelcat"])) {
    foreach(
    $_POST["catsp"] as $value) {
    $ret mysql_query("DELETE FROM category WHERE id = '$value'");
    $var[] = $value;
    }
    # repeat the deletion if exsists more then 1 subcategories 
    for($i 0$i count($var); ++$i) {
    $ret mysql_query("DELETE FROM category WHERE pid = '$var[$i]'");
    }
    }

    function 
    sub_cat($cat$cr) {
    $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] = str_repeat("---"$cr);
    $value[$i] .= "<input type='checkbox' style='border:0px' name='catsp[]' value='".$row[0]."'> ".$row[1]."<br>\n";
    # recursion
    $var sub_cat($row[0], $cr 1);
    # echoing the recursion array
    for($j 0$j count($var); ++$j) {
    $value[$i] .= $var[$j];
    }
    }
    return 
    $value;
    }

    # calling the function with empty $cat and 1 for start the recursion counting
    $var sub_cat(""1);
    # echoing the function array
    for($i 0$i count($var); ++$i) {
    echo 
    $var[$i];
    }

    ?>
    <br><input type="text" name="pcat">&nbsp;<input type="submit" name="gocat" value="Enter cat">&nbsp;<input type="submit" name="godelcat" value="Delete cat"></form>
    the table is
    id cat pid

    cheers
    Last edited by reminder; May 9, 2005 at 02:28.


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
  •