想要将每个元组指向不同的链接,我该怎么做?

时间:2015-04-20 13:55:29

标签: php html hyperlink

基本上,我想让每个元组指向不同的URL,以便我可以查看所点击的部门的学期,我正在一张表中查看。

我尝试在此代码中的echo之前传递一个href标记,但是当我按下表中的任何元组时,这会将我带到同一个链接。

<!Doctype html>
<table border="1" id="table">

  <p>
    <tr>
      <th bgcolor="#9999FF">Departements</th>
      </>
    </tr>

    <?php $servername="localhost" ; $username="root" ; $password="" ; $dbname="dbms_pro" ; // Create connection $conn=n ew mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT dep_name FROM department"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "
    <tr>
      <td department:>" . $row["dep_name"]. "</tr>
    </td>"; } } else { echo "0 results"; } $conn->close(); ?>
</table>

</html>

1 个答案:

答案 0 :(得分:1)

html中基本上有一个用于制作超链接的标记,它是一个(http://www.w3schools.com/tags/tag_a.asp):

    <a href=""></a>

您需要遍历数组并向href属性添加唯一ID以制作您想要的内容。下面的示例代码,但是将html和php混合在一个文件中并不好。

    <!Doctype html>
    <head></head>
    <body>
    <table border="1" id="table">
      <p>
        <tr>
          <th bgcolor="#9999FF">Departements</th>
          </>
        </tr>

        <?php foreach ($departements as $row) {
            echo "
        <tr>
          <td><a href='/department/{$row["id"]}'>{$row["dep_name"]}</tr>
        </td>"; 
        }
        ?>
    </table>
    </body>
    </html>