dongnuoyi8833 2013-03-05 18:30
浏览 56
已采纳

在php中创建多个html表时,可以将宽度设置为变量吗?

I'm trying to create an admin page for a small web site. My plan is to create a table for each user with the relevant data put into individual cells.

Here's my code right now:

foreach ($user_data as $key => $element)
{
    echo <<<EOT
<div><table border="1" style="width:100%">
EOT;
        foreach ($element as $subkey => $sub_element)
        {
            echo <<<EOT
<td>$sub_element</td>
EOT;
        }
        echo <<<EOT
</table></div>
EOT;

The problem is the cells in each table are of different length so the data does not line up nice and neat under the column headings (not shown here). I'm wondering if there is a way (using CSS?) to have each cell be a different, but specific, length using a variable for the width. I'm thinking I could just use a counter to keep track of which cell is being created and use a different width for each number in the counter (i.e. an array of 5 different lengths that are looped through along with the data).

Am I even approaching this the right way?

  • 写回答

1条回答 默认 最新

  • dswfyq6201 2013-03-05 19:02
    关注

    Following is the solution I came up with. Is this best practice? Is there a better way?

    <?php
    $cell_width = array('20%','10%','10%','10%', '5%', '10%','25%','10%');
    
    foreach ($user_data as $key => $element)
    {
        $counter = 0;
        echo <<<EOT
    <br /><div><table style="width:100%">
    EOT;
        foreach ($element as $subkey => $sub_element)
        {
            echo <<<EOT
    <td width=$cell_width[$counter]>$sub_element</td>
    EOT;
            $counter++;
        }
        echo <<<EOT
    </table></div>
    EOT;
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源