简体   繁体   English

在表格中集成href链接

[英]Integrate href link in table

I am trying to put in some url links in my php table. 我试图在我的php表中放入一些URL链接。 I have the table up and running on my PHP page. 我已经在PHP页面上运行了该表。 I need the column 'Rute_navn' to be activated as an url link. 我需要将“ Rute_navn”列激活为网址链接。 I found a code string for this online, but I do not know where to put it in my PHP table code. 我在网上找到了一个代码字符串,但是我不知道将其放在PHP表代码中的哪个位置。

Anybody have any suggestions? 有人有什么建议吗? I have tried to paste the single code line several places, but each time the table changes appearance into something strange. 我试图将单个代码行粘贴到多个位置,但是每次表更改外观时都会出现一些奇怪的情况。

My single line of code: 我的单行代码:

<?php echo"<a href='result.php?id=$Rute_navn'>$Rute_navn</a>";?> 

Here is my main PHP table code: 这是我的主要PHP表代码:

<?php
    //$db_host = 'localhost';
    //$db_user = 'user';
    //$db_pwd = 'password';
    //$database = 'database';
    //$table = 'Avisruter';

    if (!mysql_connect('localhost', 'user', 'password'))
    die("Can't connect to database");

    if (!mysql_select_db('database'))
    die("Can't select database");

    // sending query
    $result = mysql_query ("SELECT Rute_nr, Rute_navn FROM Avisruter WHERE Bruger=''");
    if (!$result) {
        die("Query to show fields from table failed");
    }

    $fields_num = mysql_num_fields($result);

    echo "<h2>Ledige avisruter</h2>";
    echo "<table border='5' width=305> <tr>";

    // printing table headers
    for($i=0; $i<$fields_num; $i++) {
        $field = mysql_fetch_field($result);
        echo "<td>{$field->name}</td>";
    }
    echo "</tr>\n";

    while($row = mysql_fetch_row($result)) {
        echo "<tr>";

        foreach($row as $cell)
            echo "<td>$cell</td>";

        echo "</tr>\n";
    }
    mysql_free_result($result);
?>

Change 更改

foreach($row as $cell)
   echo "<td>$cell</td>";

To

foreach($row as $column => $value){
    if ($column == 'Rute_navn') {
        echo "<td><a href='result.php?id=$value'>$value</a></td>";
    } else {
        echo "<td>$value</td>";
    }
}
while($row = mysql_fetch_row($result))
    {

            echo "<tr>";

           foreach($row as $cell){
             if($cell == "Rute_navn"){
             echo "<td><a href='result.php?id=$Rute_navn'>$Rute_navn</a></td>";
                                     }
              else
        echo "<td>$cell</td>";
}

        echo "</tr>\n";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM