How do I create links in the cells of a PHP generated table?
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!
You can already create an unique link with the "ID" of each employee.
You could do the following:
echo "<td><a href="employee.php?id=\"" . $row['id'] . "\">" . $row['id'] . "</td>";
or more readable
echo "<td><a href="employee.php?id=\"{$row['id']}\">{$row['id']}</td>";
Then you can use the employee.php to display it's detail (the id will be in $_GET['id'], see here). Don't forget to check the value of $_GET['id'] before you process it, since it can contain harmfull data (e.g. SQL-Injection).
BTW, the HTML attribute 'id' that you use in the table id="content_heading"
is supposed to be unique on the site, just as a site note.
精彩评论