views:

39

answers:

1

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!

+1  A: 

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.

DrColossos
Thanks for your reply, I am getting tired so I will give that a go tomorrow. Thanks for the heads up on the id too, but I am a bit confused as to what you are saying. I know it isn't right the way it is now (actually I shouldn't be using `width` in my `<td>` tags either) and I planned on putting it into a style sheet later when I got the chance, but I thought `id` only had to be unique per page, not the entire site. Are you saying that once I use an `id` on any page in my site I cannot use it again on another page in the same site? What should I do to format my `<td>` tags correctly?
typoknig
http://www.w3schools.com/tags/att_standard_id.asp, No, each id attribute should be used on a per page basis. You can of course use it on every page in a unique fashion.
DrColossos