简体   繁体   中英

Integrate href link in table

I am trying to put in some url links in my php table. I have the table up and running on my PHP page. I need the column 'Rute_navn' to be activated as an url link. I found a code string for this online, but I do not know where to put it in my PHP table code.

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
    //$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";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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