doubeng3216 2015-09-10 00:18
浏览 64

从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>
";

....

  • 写回答

2条回答 默认 最新

  • dongtingxiao4697 2015-09-10 00:31
    关注

    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>';
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)