0

I am trying to display a row of data from my main table (parents details) and then under each of those rows display the children's names from another table. I have had a go using the following code:

$sql="select distinct members_main.first_name as main_firstname,
    members_main.first_name as main_firstname,
    members_main.last_name as main_lastname,
    members_main.address_1 as main_address_1,
    members_main.address_2 as main_address_2,
    members_main.address_3 as main_address_3,
    members_main.address_4 as main_address_4,
    members_main.post_code as main_post_code,
    members_main.home_tel as main_home_tel,
    members_main.mobile as main_mobile,
    members_main.home_email as main_home_email
    from members_main, members_family where members_main.contact_id=members_family.contact_id";

$sql2="select members_family.first_name as fam_firstname,
    members_family.last_name as fam_lastname
    from members_family, members_main 
    where members_main.contact_id=members_family.contact_id";

$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){

echo '<table width="100%"  cellpadding="3" cellspacing="0">
<tr>
<th align="left" valign="top">First Name</th>
<th align="left" valign="top">Last Name</th>
<th align="left" valign="top">Address 1</th>
<th align="left" valign="top">Address 2</th>
<th align="left" valign="top">Address 3</th>
<th align="left" valign="top">Address 4</th>
<th align="left" valign="top">Post Code</th>
<th align="left" valign="top">Home Tel</th>
<th align="left" valign="top">Mobile</th>
<th align="left" valign="top">Email</th>
</tr>';

echo '<tr>
<td valign="top">'.$rows['main_firstname'].'</td>
<td valign="top">'.$rows['main_lastname'].'</td>
<td valign="top">'.$rows['main_address_1'].'</td>
<td valign="top">'.$rows['main_address_2'].'</td>
<td valign="top">'.$rows['main_address_3'].'</td>
<td valign="top">'.$rows['main_address_4'].'</td>
<td valign="top">'.$rows['main_post_code'].'</td>
<td valign="top">'.$rows['main_home_tel'].'</td>
<td valign="top">'.$rows['main_mobile'].'</td>
<td valign="top">'.$rows['main_home_email'].'</td>
</tr>';

$result2=mysql_query($sql2);

echo '<table width="100%"  cellpadding="3" cellspacing="0">
<tr>
<th align="left" valign="top">First Name</th>
<th align="left" valign="top">Last Name</th>
</tr>';

while($rows=mysql_fetch_array($result2)){

echo '<tr>
<td valign="top">'.$rows['fam_firstname'].'</td>
<td valign="top">'.$rows['fam_lastname'].'</td>
</tr>';

}

}

echo "</table>";

I am getting all rows from the members_family table per each record from the members_main. There can be more than one record in the members_family which will be associated to one record in the members_main. Bascially, members_main (parent) can have multiple children in the members_main.

4

2 回答 2

2

我认为您只需members_main.contact_id要从第一个表中检索您的表,以便稍后在第二个while循环中使用。然后,您必须将第二个查询移到第一个查询while loop中,这样代码将如下所示:

$sql="select distinct 
members_main.contact_id as ID,
members_main.first_name as main_firstname,
members_main.first_name as main_firstname,
members_main.last_name as main_lastname,
members_main.address_1 as main_address_1,
members_main.address_2 as main_address_2,
members_main.address_3 as main_address_3,
members_main.address_4 as main_address_4,
members_main.post_code as main_post_code,
members_main.home_tel as main_home_tel,
members_main.mobile as main_mobile,
members_main.home_email as main_home_email
from members_main, members_family where members_main.contact_id=members_family.contact_id";

$result = mysql_query($sql);
while($rows=mysql_fetch_array($result)) {

//script goes on with table printing untill
<td valign="top">'.$rows['main_home_email'].'</td>
</tr>';

现在是查询的时候了

$sql2="select members_family.first_name as fam_firstname,
members_family.last_name as fam_lastname
from members_family, members_main 
where members_main.contact_id=members_family.contact_id and members_main.contact_id = '".$rows['ID']."'";

如您所见,我添加了条件以限制我从第一个查询中检索到WHERE的指定结果。ID现在您可以打印表格的其余部分。哦,顺便说一下,你关闭桌子应该是这样的

echo "</table></td></tr></table>";

这是因为您打开了两个表。

然后我希望您记住这些mysql_*功能已被弃用,因此我建议您切换到mysqliPDO

于 2013-05-13T11:58:24.257 回答
0

I feel some confusion with your second query. I am thinking that you have to construct your second query with the contact_id which is coming from first query(members_main). I assumes in that way and i have coded below. May be it will help you

        <?php
        $sql = "select distinct members_main.first_name as main_firstname,
            members_main.first_name as main_firstname,
            members_main.last_name as main_lastname,
            members_main.address_1 as main_address_1,
            members_main.address_2 as main_address_2,
            members_main.address_3 as main_address_3,
            members_main.address_4 as main_address_4,
            members_main.post_code as main_post_code,
            members_main.home_tel as main_home_tel,
            members_main.mobile as main_mobile,
            members_main.home_email as main_home_email
            from members_main, members_family where members_main.contact_id=members_family.contact_id";
        $result = mysql_query($sql);
        ?>
        <table width="100%"  cellpadding="3" cellspacing="0">
            <tr>
                <th align="left" valign="top">First Name</th>
                <th align="left" valign="top">Last Name</th>
                <th align="left" valign="top">Address 1</th>
                <th align="left" valign="top">Address 2</th>
                <th align="left" valign="top">Address 3</th>
                <th align="left" valign="top">Address 4</th>
                <th align="left" valign="top">Post Code</th>
                <th align="left" valign="top">Home Tel</th>
                <th align="left" valign="top">Mobile</th>
                <th align="left" valign="top">Email</th>
            </tr>
            <?php
            while ($rows = mysql_fetch_array($result))
            {
                $conId = $rows['contact_id']; //Which is comming from first Query
                $sql2 = "select members_family.first_name as fam_firstname,
            members_family.last_name as fam_lastname
            from members_family, members_main 
            where members_family.contact_id=$conId";

                $result2 = mysql_query($sql2);
                ?>
                <tr>
                    <td valign="top"><?= $rows['main_firstname']; ?></td>
                    <td valign="top"><?= $rows['main_lastname']; ?></td>
                    <td valign="top"><?= $rows['main_address_1']; ?></td>
                    <td valign="top"><?= $rows['main_address_2']; ?></td>
                    <td valign="top"><?= $rows['main_address_3']; ?></td>
                    <td valign="top"><?= $rows['main_address_4']; ?></td>
                    <td valign="top"><?= $rows['main_post_code']; ?></td>
                    <td valign="top"><?= $rows['main_home_tel']; ?></td>
                    <td valign="top"><?= $rows['main_mobile']; ?></td>
                    <td valign="top"><?= $rows['main_home_email']; ?></td>
                </tr>    

                <tr>
                    <td colspan="10">
                        <table width="100%"  cellpadding="3" cellspacing="0">
                            <tr>
                                <th align="left" valign="top">First Name</th>
                                <th align="left" valign="top">Last Name</th>
                            </tr>
                            <?php
                            while ($rowsFamily = mysql_fetch_array($result2))
                            {
                                ?>
                                <tr>
                                    <td valign="top"><?= $rowsFamily['fam_firstname']; ?></td>
                                    <td valign="top"><?= $rowsFamily['fam_lastname']; ?></td>
                                </tr>
                                <?php
                            }
                            ?>
                        </table>
                    </td>
                </tr>
                <?php
            }
            ?>
        </table>
于 2013-05-13T12:07:46.187 回答