如何输出其外键id的名称

时间:2016-01-06 08:58:35

标签: php

我有一个问题,我希望外键中的每个id都可以输出名称而不是id。 Here就是形象。

这是我的代码:

<table class="altrowstable" data-responsive="table" >
                <thead >
                    <tr>
                        <th> IDno</th>
                        <th> Lastname </th>
                        <th> Firstname </th>
                        <th> Department </th>
                        <th> Program </th>
                        <th> Action</th>

                    </tr>
                </thead>
                <tbody> 
                 <div style="text-align:center; line-height:50px;"> 
                        <?php
                        include('../connection/connect.php');
                        $YearNow=Date('Y');

                        $result = $db->prepare("SELECT * FROM student,school_year  where  user_type =3 AND student.syearid = school_year.syearid AND school_year.from_year like $YearNow ");
                        $result->execute();
                        for($i=0; $row = $result->fetch(); $i++){
                        ?>
                    <tr class="record">
                        <td><?php echo $row['idno']; ?></td>
                        <td><?php echo $row['lastname']; ?></td>
                        <td><?php echo $row['firstname']; ?></td>
//name belong's to their id's
                            <td><?php echo $row['dept_id']; ?></td>
                            <td><?php echo $row['progid']; ?></td>

                    <td><a style="border:1px solid grey; background:grey; border-radius:10%; padding:7px 12px; color:white; text-decoration:none; " href="addcandidates.php?idno=<?php echo $row['idno']; ?>" > Running</a></div></td>
</tr>
        <?php
            }
        ?>
            </tbody>
                </table>    

谢谢你们需要帮助

4 个答案:

答案 0 :(得分:0)

只需替换此行

<td><?php echo $row['progid']; ?></td>

这一个

<td><?php echo $row['prog_name']; ?></td>

使用您想要的字段

您还需要调整查询以选择程序信息(如果尚未在表school_year或student(带连接)中已经存在:

$result = $db->prepare("SELECT * FROM student
    INNER JOIN school_year ON student.syearid = school_year.syearid
    INNER JOIN program ON student.progid = program.progid
    WHERE user_type =3
    AND school_year.from_year like $YearNow ");

假设程序表被称为“程序”

答案 1 :(得分:0)

更改

<td><?php echo $row['progid']; ?></td>

<td><?php echo $row['prog_name']; ?></td>

我建议学习php和sql

答案 2 :(得分:0)

更改

<td><?php echo $row['progid']; ?></td>

通过

<td><?php echo $row['prog_name']; ?></td>

答案 3 :(得分:0)

循环内部:

for($i=0; $row = $result->fetch(); $i++){
$program = $db->prepare("SELECT * FROM program where  progid = :progid");
$program->execute(array('progid' => $row['progid']));
$p = $program->fetch();

现在您可以按照以下方式使用它:$p['prog_name']