Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2009
    Posts
    7

    Unanswered: counting malnourished children

    i'm trying to search for a person who is less than 6 years old from the PERSON table then use the person_id's found to search for the persons who have more than 2 in the Indicator field of the NUTRITION table.

    Then I have to display a table containing the Ages with their corresponding Indicators as well as the number of persons who have >2 indicators out of all the persons who are less than 6 years old.

    The DB has no problem. I'm just having trouble with the logic..

    Can anyone help me...




    Here's the code:

    $result = mysql_query("SELECT PersonID, Age FROM person WHERE age < 6");
    $number = 0;
    $malnourished = 0;
    echo "<table border=1>";
    echo "<tr><td><b>Age</b></td><td><b>Nutrition Indicator</b></tr>";

    while ($row = mysql_fetch_row($result)) // first while statement
    {
    $number++;
    $result2 = mysql_query("SELECT PersonID, Indicator FROM nutrition WHERE PersonId=$row[PersonID] AND Indicator > 2");

    while ($row2 = mysql_fetch_row($result2)) //second while statement
    {
    $malnourished++;
    printf ("<tr><td>$row2[0]</td><td>$row2[1]</td></tr>");
    }//end of second while statement

    }//end of first while statement

    echo "<tr><td colspan='2'>There are $malnourished out of $number</td></tr>";
    echo "</table>";

  2. #2
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    Code:
    $number = 0;
    $malnourished = 0; 
    
    echo "<table border=1>";
    echo "<tr><td><b>Age</b></td><td><b>Nutrition Indicator</b></tr>";
    
    $sql = "SELECT p.age, n.Indicator
            FROM   person p,
                   nutrition n
            WHERE  p.age < 6 
                   and n.PersonId=p.PersonID 
                   and n.Indicator > 2";
    
    $result = mysql_query( $sql );
    
    while ( list( $age, $indicator ) = mysql_fetch_row($result) ) {
         $malnourished++;
         printf ("<tr><td>$age</td><td>$indicator</td></tr>");
    }
    
    $sql = "SELECT count(*)
            FROM   person
            WHERE  age < 6";
    
    $result = mysql_query( $sql );
    list( $number ) = mysql_fetch_row($result);
    
    echo "<tr><td colspan='2'>There are $malnourished out of $number</td></tr>";
    echo "</table>";
    I tried to simplify your code to make it easier to understand. I don't have your tables or data so I can't test it but it looks about right.

    Mike
    Last edited by mike_bike_kite; 02-20-09 at 07:04.

  3. #3
    Join Date
    Feb 2009
    Posts
    7

    Pulling data from two tables

    I have two tables, Person and Nutrition.

    I'm trying to search for a person who is less than 6 years old from the PERSON table then use the person_id's found to search for the persons who have more than 2 in the Indicator field of the NUTRITION table.

    Then I have to display a table containing the Ages with their corresponding Indicators as well as the number of persons who have >2 indicators out of all the persons who are less than 6 years old.

    The DB has no problem. I'm just having trouble with the logic..

    Can anyone help me...




    Here's the code:

    $result = mysql_query("SELECT PersonID, Age FROM person WHERE age < 6");
    $number = 0;
    $malnourished = 0;
    echo "<table border=1>";
    echo "<tr><td><b>Age</b></td><td><b>Nutrition Indicator</b></tr>";

    while ($row = mysql_fetch_row($result)) // first while statement
    {
    $number++;
    $result2 = mysql_query("SELECT PersonID, Indicator FROM nutrition WHERE PersonId=$row[PersonID] AND Indicator > 2");

    while ($row2 = mysql_fetch_row($result2)) //second while statement
    {
    $malnourished++;
    printf ("<tr><td>$row2[0]</td><td>$row2[1]</td></tr>");
    }//end of second while statement

    }//end of first while statement

    echo "<tr><td colspan='2'>There are $malnourished out of $number</td></tr>";

  4. #4
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    What exactly was wrong with the code I gave you?

  5. #5
    Join Date
    Feb 2009
    Posts
    7

    sorry for the late reply

    thanks so much for the code you gave me..i used it and it worked.. as to the double post that was an accident..

    thanks again..!

Posting Permissions

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