Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2004
    Posts
    63

    Question Unanswered: Loop within a loop within a loop etc

    Hello,

    I am trying to create a family tree web app using PHP.

    I have fallen down on the following code:

    PHP Code:
    function find_child_loop$parentid )
    {
                 
    //An attempt to stop the var being re-used
        
    $passedid=$parentid;
        unset(
    $parentid);

                 
    //Select the child(ren) for parent
        
    $resultchildren[$passedid]=mysql_query("SELECT t.id, t.forename, t.surname, t.dob, t.dod, t.birthplace, tc.pid, tc.childid FROM tblpeople t, tblchildren tc WHERE tc.childid=t.id AND tc.pid='$passedid' ORDER BY t.dob")
        or die(
    "SELECT error: ".mysql_error());

        
        
    //If there are children - carry on
        
    if(mysql_num_rows($resultchildren[$passedid])>=1)
        {
                              
    //Loop to print children
            
    while($rowc[$passedid]=mysql_fetch_array($resultchildren[$passedid]))
            {
                print
    "<div id='d".++$dcount."p".$rowc[$passedid]["id"]."'><a href='#' onClick=\"hide('d".$dcount."p".$rowc[$passedid]["id"]."');return false;\">H</a>";
                print 
    "<a href='persondetails.php?pid=".$rowc[$passedid]["id"]."'>".$rowc[$passedid]["forename"]." ".$rowc[$passedid]["surname"]."<a><br>";
                print 
    "b ".dbdate_to_date($rowc[$passedid]["dob"])."<br>";
                print 
    "d ".dbdate_to_date($rowc[$passedid]["dod"])."<br>";
                
                
    //Do looping thing (THIS IS THE PROBLEM)
                
    find_child_loop$rowc[$passedid]["id"] );
                
                print
    "</div>\n";

            }
        }
    //END FUNCTION

    I am not sure if this should work but I want it to, when called, find each child for a parent and then go down each creating set of nested hidable divs.

    From running the code in opera (msie just craps out) I could see that the same id was being used each time so the loop is never ending.

    Any ideas?

    Thanks
    ChrisOfCatford

  2. #2
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    I tried a very stripped down version and it works -
    PHP Code:
    function find_child_loop$parentid 

    // get all the children of this parent
        
    $result=mysql_query("SELECT id FROM tbl WHERE pid='$parentid'") or die("SELECT error: ".mysql_error()); 

        if(
    mysql_num_rows($result)>=1
        { 
            while(
    $rowc=mysql_fetch_array($result)) 
            { 
                print
    "Parent ID: $parentid Child ID: {$rowc["id"]}<br />"

                
    find_child_loop$rowc["id"] ); 
            } 
        } 
    //END FUNCTION 

    This is for a single table where each person has his ID and a parent's PID. To find all the children of a person, query where the PID = that person's ID.

    I see you are using a table for people and a table for children, which I cannot understand. The only way I can see that your code would not work and have the same ID each time is if the $rowc[$passedid]["id"] is actually the parent's ID and not the current person's ID.

    Note: The use of $resultchildren[$passedid] and $rowc[$passedid] is not required for this function to work, but I don't thank it is the cause of your problem.
    Last edited by dbmab; 04-16-06 at 15:35.

  3. #3
    Join Date
    Sep 2004
    Posts
    63
    Hi dbmab.

    Thanks for your reply.

    The two tables are needed for this

    There will be two ways of drawing trees: forwards and backwards.

    Forwards is the one I am doing now and it picks everyone in a tree shape from a single person (or marriage), usually getting bigger and bigger as it goes down.

    One table tblpeople holds dob dod name and other personal details. There is then a table for children which can hold any number of records with a link from the id of the parent to the id of the child(ren).

    It would not be possible to keep this in one table as most people have more than one child (or none at all).

    The link in the query is to create a recordset with all details ready for printing the hideable divs.

    I will check what you have written, perhaps I could use that to test by just printing the id for each person and then do a separate select query for the name and other details of each.

    I will post back when I have had a go.

    Thanks again,
    ChrisOfCatford

  4. #4
    Join Date
    Sep 2004
    Posts
    63
    OK, some testing later and using the stripped version (with correct table and field names obviously) this is part of the result:

    Parent ID: 37 Child ID: 33
    Parent ID: 37 Child ID: 32
    Parent ID: 37 Child ID: 31
    Parent ID: 37 Child ID: 36
    Parent ID: 36 Child ID: 34
    Parent ID: 36 Child ID: 35
    Parent ID: 36 Child ID: 36
    Parent ID: 36 Child ID: 34
    Parent ID: 36 Child ID: 35
    Parent ID: 36 Child ID: 36
    Parent ID: 36 Child ID: 34
    Parent ID: 36 Child ID: 35
    Parent ID: 36 Child ID: 36
    Parent ID: 36 Child ID: 34
    Parent ID: 36 Child ID: 35
    Parent ID: 36 Child ID: 36
    Parent ID: 36 Child ID: 34
    Parent ID: 36 Child ID: 35
    Parent ID: 36 Child ID: 36
    Parent ID: 36 Child ID: 34
    Parent ID: 36 Child ID: 35
    Parent ID: 36 Child ID: 36
    Parent ID: 36 Child ID: 34
    Parent ID: 36 Child ID: 35
    Parent ID: 36 Child ID: 36
    Parent ID: 36 Child ID: 34
    Parent ID: 36 Child ID: 35
    Parent ID: 36 Child ID: 36
    Parent ID: 36 Child ID: 34
    It goes on for as long as it takes a browser to give up.

    As you can see, it selects the correct results once, then repeats the first result. I want to have the loop print each child, then child of child then child of child of child etc. before doing the second child of the start parent.

    I can't see why this doesn't work.

    This is the modified code:

    PHP Code:
    function find_child_loop$parentid )  
    {  
    // get all the children of this parent 
        
    $result=mysql_query("SELECT childid FROM tblchildren WHERE pid='$parentid'") or die("SELECT error: ".mysql_error());  

        if(
    mysql_num_rows($result)>=1)  
        {  
            while(
    $rowc=mysql_fetch_array($result))  
            {  
                print
    "Parent ID: $parentid Child ID: {$rowc["childid"]}<br />";  

                
    find_child_loop$rowc["childid"] );  
            }  
        }  
    //END FUNCTION  

    Thanks,
    ChrisOfCatford

  5. #5
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    WOW.
    Parent ID: 36 Child ID: 36
    Child 36 is listed as his own parent. Your data is recursive and the recursive function has no choice but to go on forever.

  6. #6
    Join Date
    Sep 2004
    Posts
    63
    Ooops!

    Thanks for that dbmab.

    Seems I had not allowed for error checking in the input section.

    Code now works.

    ChrisOfCatford

Posting Permissions

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