SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    While Loop Question

    The function below is part of a script I use to display an item's children, grandchildren or great grandchildren. It works, except that it only displays the FIRST child or grandchild listed in my database. Obviously, I need to echo the results ($GC) inside a while loop. However, none of the while loops I've tried work. Can someone show me the correct way to write a while loop for this function?

    Thanks.

    PHP Code:
    function getChild($current) {
       
    $query "SELECT * FROM table WHERE PARENT = '".$current."'";
       
    $results mysql_query($query) or die ("An error occurred: ".mysql_error());
       
    $myrow mysql_fetch_array($results);
       return 
    $myrow['child'];


  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function getChild($current) {
       
    $query "SELECT * FROM table WHERE PARENT = '".$current."'";
       
    $results mysql_query($query) or die ("An error occurred: ".mysql_error());
       while (
    $myrow mysql_fetch_array($results)) {
         echo 
    $myrow['child'];
       }

    In the loop, every iteration $myrow is a consecutive row of results from the DB.

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but I'm still missing something; your script echoes the children, but I can't echo grandchildren.

    Below is a complete script that works (for deriving the parents and grandparents)...

    PHP Code:
    $current 'Canidae';
    $GGP getParent(getParent($current)));
    $GP getParent(getParent($current));
    // Where $GP = grandparent & GGP = great grandparent

    function getParent($current) {
       
    $query "SELECT * FROM gz_mammals WHERE Name = '".$current."'";
       
    $results mysql_query($query) or die ("An error occurred: ".mysql_error());
       
    $myrow mysql_fetch_array($results);
       return 
    $myrow['Parent'];
    }

    echo 
    $GGP;
    echo 
    $GP
    The above example echoes the grandparents AND great grandparents.

    I've implemented your script like so...

    PHP Code:
    $current 'Canidae';
    $GGC getChild(getChild($current)));
    $GC getChild(getChild($current));
    // Where $GC = grandchild & GGC = great grandchild

    function getChild($current) {
       
    $query "SELECT * FROM table WHERE PARENT = '".$current."'";
       
    $results mysql_query($query) or die ("An error occurred: ".mysql_error());
       while (
    $myrow mysql_fetch_array($results)) {
       echo 
    $myrow['Name'];
       echo 
    $GC;
       echo 
    $GGC;
      }

    Your script echoes the children, but when I insert echo $GC and echo $GGC, they display nothing. However, both of them work when I model my script after my Parent/Grandparent script (with no while loop).

    I suspect the problem may lie with the names of my database table fields. There is indeed a field named Parent, but the field that holds the children is actually named "Name."

    I changed every instance of getChild to getName, but it works just as before, displaying children but no grandchildren.

    Thanks.

  4. #4
    SitePoint Zealot glowdot's Avatar
    Join Date
    Jun 2007
    Location
    Santa Monica, CA
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your function doesn't return anything, so the outer call to getChild is passing nothing as a parameter.

    You need to return an array of children, then loop OUTSIDE the function to make subsequent calls to getChild().
    Advertise on Glowfoto
    banners as low as $25/month, text ads $10/month
    Share 10 million impressions per month!

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by glowdot View Post
    Your function doesn't return anything, so the outer call to getChild is passing nothing as a parameter.

    You need to return an array of children, then loop OUTSIDE the function to make subsequent calls to getChild().
    OK, I placed the while loop outside the function in the following script. But how do I return an array of children?

    PHP Code:
    function getChild($current) {
       
    $query "SELECT * FROM table WHERE PARENT = '".$current."'";
       
    $results mysql_query($query) or die ("An error occurred: ".mysql_error());
    }

       while (
    $myrow mysql_fetch_array($results)) {
       echo 
    $myrow['Name'];
       echo 
    $GC;
       echo 
    $GGC;
      } 

  6. #6
    SitePoint Zealot glowdot's Avatar
    Join Date
    Jun 2007
    Location
    Santa Monica, CA
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function getChild($current) {
       
    // This function now accepts an array!
       // And it returns an array!

       
    $children = Array();

       foreach(
    $current AS $value) {
          
    $query "SELECT * FROM table WHERE PARENT = '$value'";
          
    $results mysql_query($query);

          while (
    $myrow mysql_fetch_array($results)) {
             
    array_push($children$myrow['Name']);
            
          }
       }
          return 
    $children;

    Now getChild accepts an array of names, and returns an array of the children of those in $current.

    PHP Code:
    $people = array('Rick');
    $children getChild($people);         // $children contains all of Rick's children
    $grandchildren getChild($children// $grandchildren contains next generation 
    Or, as you wanted to use it:

    PHP Code:
    $grandchildren getChild(getChild(array('Rick'))); 
    Advertise on Glowfoto
    banners as low as $25/month, text ads $10/month
    Share 10 million impressions per month!

  7. #7
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by glowdot View Post
    Your function doesn't return anything, so the outer call to getChild is passing nothing as a parameter.

    You need to return an array of children, then loop OUTSIDE the function to make subsequent calls to getChild().
    glowdot your a genius! if only i got a mind like yours!

  8. #8
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, can someone tell me what I'm doing wrong now? I posted my script below. I've tried the echo variables and displays in various positions, before each of the closing brackets. The only thing I've been able to display is an error message for foreach or ArrayArrayArray.

    Thanks.

    PHP Code:
    $current 'Canidae';
    function 
    getChild($current) {
      
    $children = Array();

      foreach(
    $current AS $value) {
      
    $query "SELECT * FROM gz_mammals WHERE Parent = '$value'";
      
    $results mysql_query($query) or die ("An error occurred: ".mysql_error());

      while (
    $myrow mysql_fetch_array($results)) {
        
    array_push($children$myrow['Name']);

    // ECHO VARIABLES...
    $people = array('Rick');
    $children getChild($people);
    $GC getChild(getChild(array('Rick')));

    // DISPLAYS...
    echo $people;
    echo 
    $children;
    echo 
    $GC;
        }
      }
        return 
    $children;


  9. #9
    SitePoint Zealot glowdot's Avatar
    Join Date
    Jun 2007
    Location
    Santa Monica, CA
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you try to echo an array, it will just say array. You need to print_r them:

    Code:
    print_r($people);
    print_r($children);
    print_r($GC);
    Also, don't call the function inside the function. I don't think you really mean for this function to be recursive

    PHP Code:
    $current 'Canidae';  // FYI this line does nothing

    function getChild($current) {
      
    $children = Array();

      foreach(
    $current AS $value) {
        
    $query "SELECT * FROM gz_mammals WHERE Parent = '$value'";
        
    $results mysql_query($query) or die ("An error occurred: ".mysql_error());

        while (
    $myrow mysql_fetch_array($results)) {
          
    array_push($children$myrow['Name']);
        }

      }

      return 
    $children;

    }

    // ECHO VARIABLES...
    $people = array('Rick');
    $children getChild($people);
    $GC getChild(getChild(array('Rick')));

    // DISPLAYS...
    print_r($people);
    print_r($children);
    print_r($GC); 
    Advertise on Glowfoto
    banners as low as $25/month, text ads $10/month
    Share 10 million impressions per month!

  10. #10
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, this just isn't clicking for me. I pasted your last script into my page, and it displays the following:

    Code:
    ($People): Array ([0] => Rick)
    ($Children): Array()
    ($GC): Array()
    So I must at least be doing SOMETHING right.

  11. #11
    SitePoint Zealot glowdot's Avatar
    Join Date
    Jun 2007
    Location
    Santa Monica, CA
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now pass in something meaningful instead of 'Rick'. Something that will actually return data from the database.
    Advertise on Glowfoto
    banners as low as $25/month, text ads $10/month
    Share 10 million impressions per month!

  12. #12
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I knew I was missing something obvious; I was focused on WHERE Parent = '$value' as the interface between the script and the database.

    Anyway, it works great now. Thanks so much for all your help.


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
  •