Parent and Child tables - While loops?

Hi everyone,

This may be a simple soluton to you guys but I’m having trouble trying to pull data from two tables that I have. One being a parent and the other a child… Parent has more than one child…

I can get the data from the parent table with a do while loop but when I try and run a loop to display the children with that, it only shows the first child from that result… Eventually I want to use it to send out an email to a parent and how much they owe for a membership. Look at the example below:

From: MyWebsite ([email protected])
To: Parents Name ([email protected])
Subject: Invoice

Dear Parents Name

Your music centre fees for this term are below for each child.

Childs Name - £18
Childs Name - £12

Kind Regards,

Me

[hr]

The child table has a “parent_id” so I can use this to link the records but its driving me mad. Hope you can help.

Dan

Hello,

If you can, can you please provide your code that you are having problems with so we can pinpoint if there are any problem with your code or if you are just simply missing code.

Thanks!

Hi,

Thanks so much for your time.

Here is what I have but I’ve just been trying to get the childs to display I’ve got myself in a muggle and it won’t even display one now… Take a look below and you will see what I’m trying to do…

[php]

<?php do { ?>

From: Me ([email protected])
To: <?php echo $row_Parents['name']; ?> (<?php echo $row_Parents['email']; ?>)
Subject: Invoice (<?php echo $row_Terms['name']; ?>)

Dear <?php echo $row_Parents['name']; ?>

Your music centre fees for this term are below for each child.

<?php if ($row_Pupils['parents_id'] == $row_Parents['id']){ do {

 echo $row_Pupils['first_name']." - £10"."<br />";

} while ($row_Pupils = mysql_fetch_assoc($Pupils)); 
}

?>
This fee covers all the bands and ensembles for which they are registered. . .


<?php } while ($row_Parents = mysql_fetch_assoc($Parents)); ?>

[/php]

I think I might have it…

Would it be nested while loops?

Where am I going wrong here? I have gone back to basics and only trying to display the child fields that belong to the parents…

But it seems to only post the following:

[php]2

Ian Hatton

  • Lucy Greenslade
  • Mark Greenslade
    • Dan Hatton
    • Chris Hatton
  • Joe Bloggs
  • John Lennon
  • Harry Hedge
  • Rob Smith
  • Katie Loop
  • Katie Loop
  • 3

    Mark Bloggs

    4

    David Lennon

    5

    Thomas Hedge

    6

    Joan Smith

    [/php]

    [php] while($row_Parents = mysql_fetch_array($Parents))
    {
    $par_id= $row_Parents[‘id’];
    echo $par_id;
    echo “

    ”;
    echo $row_Parents[‘name’];
    echo “

    ”;

    while($row_Pupils = mysql_fetch_array($Pupils))
    {
        if($par_id == $row_Pupils['parents_id'])
    	echo "<ul>";
        echo "<li>".$row_Pupils['first_name']." ".$row_Pupils['last_name']."</li>";
    	echo "</ul>";
    }
    

    }
    [/php]

    I have finally done it…

    For those who want to know… Here it is:

    [php]<?php do { ?>

    From: Me ([email protected])
    To: <?php echo $row_Parents['name']; ?> (<?php echo $row_Parents['email']; ?>)
    Subject: Invoice (<?php echo $row_Terms['name']; ?>)

    Dear <?php echo $row_Parents['name']; ?>

    Your music centre fees for this term are below for each child.

    <?php mysql_select_db($database_SDMClocal, $SDMClocal); $query_Pupils = "SELECT id, parents_id, first_name, last_name, groups FROM pupils WHERE parents_id = ".$row_Parents['id'].""; $Pupils = mysql_query($query_Pupils, $SDMClocal) or die(mysql_error()); $row_Pupils = mysql_fetch_assoc($Pupils); $totalRows_Pupils = mysql_num_rows($Pupils); if ($row_Pupils['parents_id'] == $row_Parents['id']){ do { echo $row_Pupils['first_name']." ".$row_Pupils['last_name']." - £10"."
    "; } while ($row_Pupils = mysql_fetch_assoc($Pupils)); } ?>

    This fee covers all the bands and ensembles for which they are registered. . .


    <?php } while ($row_Parents = mysql_fetch_assoc($Parents)); ?>[/php]
    Sponsor our Newsletter | Privacy Policy | Terms of Service