从php sql查询填充时,将<td>的内容放入链接中?

I am using an SQL query to populate an HTML table. The query runs fine and the table looks good. Now I want to make contents of the first column of each row a hyperlink. What is the best approach? My best guess is in the snippet below and was not successful.

...

$fields_num = mysqli_num_fields($result);

echo "<thead>";
echo "<tr>";


// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysqli_fetch_field($result);
    echo "<th>{$field->name}</th>";
}
echo "</tr>
";
echo "</thead>";

// printing table rows  
while($row = mysqli_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell) {
        if($cell[0]) {
            echo "<td><a href="index2.php/$cell"></a></td>";
        }
        else
            echo "<td>$cell</td>";
    }
    echo "</tr>
";

....

I hope this will solve your problem.

<?php

$fields_num = mysqli_num_fields($result);

echo '<thead>';
echo '<tr>';

for($i=0; $i < $fields_num; $i++){

    $field = mysqli_fetch_field($result);
    echo '<th>{'.$field->name.'}</th>';
}

echo '</tr>';

echo '</thead>';
echo '<tbody>';

while($row = mysqli_fetch_row($result)){

    echo '<tr>';
    foreach($row as $cell) {

        if($cell[0]) {
            echo '<td><a href="index2.php/'.$cell[1].'"></a></td>';
        }else{
            echo '<td>'.$cell[1].'</td>';
        }
    }
}

echo '</tr>';
echo '</tbody>';

?>

Your echo in your while loop should be using the individual array elements like your if statement is. You're mixing up the quotes in your echo as well. Also you should be able to just directly put PHP variables in echos, otherwise concat with a .

if ($cell[0]) {
    echo "<td><a href='index2.php/$cell[0]'></a></td>";
} else {
    echo "<td>$cell[0]</td>";
}

Echoing with concat:

echo "<td><a href='index2.php/".$cell[0]."'></a></td>";

etc

Not sure if you can inline echo an array, so probably best to just concat with .