Problem with recursion

Discussion in 'PHP' started by dizyn, Nov 18, 2007.

  1. #1
    I have categories up to n levels and my db is like:
    Category: catid,catname,parent

    parent = n //where n is the value of parent category


    
    function myFun($id)
    {
    	$res = mysql_query("select * from categories where parent=".$id."") or die("Here is error");
    	while($row = mysql_fetch_array($res))
    	{
    		myFun($row["catid"])
    	}
    }
    myFun2(2)
    
    PHP:
    but this is not workin
    any help?

    thank you
     
    dizyn, Nov 18, 2007 IP
  2. mahmood

    mahmood Guest

    Messages:
    1,228
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Firstly how do you know it is not working? There is no action to be performed there so we can detect if it is working or not.

    Secondly, I guess you'd better use "if" instead of "while"
     
    mahmood, Nov 18, 2007 IP
  3. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #3
    try this instead
    function myFun($id)
    {
        $res = mysql_query("select * from categories where parent='".$id."'") or die("Here is error");
        while($row = mysql_fetch_array($res))
        {
            myFun($row["catid"])
        }
    }
    myFun2(2)
    PHP:
    on myFun2(2) the parent='2' just added two ' at the $res
     
    hip_hop_x, Nov 18, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    nico_swd, Nov 18, 2007 IP
  5. mellow-h

    mellow-h Peon

    Messages:
    750
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    mellow-h, Nov 18, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    In PHP is what?
     
    nico_swd, Nov 18, 2007 IP
  7. mellow-h

    mellow-h Peon

    Messages:
    750
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #7
    In PHP Quotation is allowed for Integers, though only single quotation.
     
    mellow-h, Nov 18, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Yes, it's allowed. But it's not necessary in the case above. And it won't make a difference.
     
    nico_swd, Nov 18, 2007 IP
  9. AdnanAhsan

    AdnanAhsan Well-Known Member

    Messages:
    601
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #9
    $res = mysql_query("select * from categories where parent=$id") or die("Here is error");

    use it, i did not check it, but it should work because you used (") double quote, so you can use variable within ("). thanks
     
    AdnanAhsan, Nov 18, 2007 IP
  10. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It doesn't matter how PHP treats quoted numbers because in this instance the quoted numbers are in the MySQL query... not in PHP.

    In PHP you shouldn't ever quote numbers, in MySQL you shouldn't EVER quote numbers.
     
    TwistMyArm, Nov 18, 2007 IP
  11. mvl

    mvl Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Your function never returns a value and does not output things so you can never tell if it does what it's supposed to do. But anyway I thing the initial call :
    
    ...
    myFun2(2)
    ...
    
    Code (markup):
    should be:

    
    ...
    myFun(2);
    ...
    
    Code (markup):
     
    mvl, Nov 18, 2007 IP