I have a table generated from some PHP code that lists a SMALL amount of important information for employees. I want to make it so each row, or at least one element in each row can be clicked on so the user will be redirected to ALL of the information (pulled from MySQL database) related to the employee who was clicked on. I am not sure how would be the best way to go about this, but I am open to suggestions. I would like to stick to PHP and/or JavaScript. Below is the code for my table:
<table>
<tr>
<td id="content_heading" width="25px">ID</td>
<td id="content_heading" width="150px">Last Name</td>
<td id="content_heading" width="150px">First Name</td>
<td id="content_heading" width="75px">SSN</td>
</tr>
<?php
$user = 'user';
$pass = 'pass';
$server = 'localhost';
$link = mysql_connect($server, $user, $pass);
if (!$link){
die('Could not connect to database!' . mysql_error());
}
mysql_select_db('mydb', $link);
$query = "SELECT * FROM employees";
$result = mysql_query($query);
mysql_close($link);
$num = mysql_num_rows($result);
for ($i = 0; $i < $num; $i++){
$row = mysql_fetch_array($result);
$class = (($i % 2) == 0) ? "table_odd_row" : "table_even_row";
echo "<tr class=".$class.">";
echo "<td>".$row[id]."</td>";
echo "<td>".$row[l_name]."</td>";
echo "<td>".$row[f_name]."</td>";
echo "<td>".$row[ssn]."</td>";
echo "</tr>";
}
?>
</table>
EDIT
Ok, after modifying what @DrColossos posted I have been able to get my links to work correctly, but now I'm having trouble with the uniqueness part. Below is the code I am now using to create my table:
echo "<td><a href=Edit_Employee.php?".$row[id].">".$row[id]."</a></td>";
echo "<td><a href=Edit_Employee.php?".$row[id].">".$row[l_name]."</a></td>";
echo "<td><a href=Edit_Employee.php?".$row[id].">".$row[f_name]."</a></td>";
echo "<td><a href=Edit_Employee.php?".$row[id].">".$row[ssn]."</a></td>";
This makes all of the elements of a row hyperlink to Edit_Employee.php?**id**
. For instance if the id
was one the hyperlink would be Edit_Employees.php?1
. Now what do I need to do in my Edit_Employee.php
page to get or recognize the id
in the link, because it is that id
that is unique and that is what I need to base my MySQL search on.
EDIT
Figured it out. This did the trick:
$id = $_GET['id'];
I found that creating my links as I did makes the id
a global variable which PHP can pull from the hyperlink. I used the code above in the page that the hyperlink points to and I was able to get what I wanted. Not too hard, but frustrating if you don't know how it is done!