Camille Valerio Camille Valerio - 4 years ago 100
PHP Question

How to output the name of their foreign key id

I have a problem, I want that each id in the foreign key can output the name instead of their id. Here is the image.

Here's my code :

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

<div style="text-align:center; line-height:50px;">

$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 ");
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>

Thanks guys need a help

Answer Source

Just replace this line

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

by this one

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

To use the field you want

You also need to adapt your query to select program infos if not already in the table school_year or student (with a join) :

$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 ");

Assuming the program table is called "program"

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download