dongni9825 2010-06-17 05:26
浏览 36
已采纳

如何在PHP生成的表的单元格中创建链接?

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条回答 默认 最新

  • duanjianqu3685 2010-06-17 05:44
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏