自动列HTML

时间:2011-01-24 17:57:20

标签: html

我想知道是否可以使用HTML和PHP创建自动列。

我目前正在使用这样的东西:

<table>
  <tr>
    <td>
      key1: value1<br />
      key2: value2<br />
      key3: value3<br />
    </td>
    <td>
      key4: value4<br />
      key5: value5<br />
      key6: value6<br />
    </td>
  </tr>
</table>

我想在一个PHP循环中组合两个行,因此我希望有这样的结构(但不能生成一个,而是两个行):

<table>
  <tr>
    <td>
      key1: value1<br />
      key2: value2<br />
      key3: value3<br />
      key4: value4<br />
      key5: value5<br />
      key6: value6<br />
    </td>
  </tr>
</table>

3 个答案:

答案 0 :(得分:0)

我认为您会发现最佳方法是使用带有2列的原始标记。即使你发现了一些css技巧来消除第二列,你很可能需要调整键/值对来使它们显示你想要的方式。我的意思是如果你想要这个:

key1:val1  |  key4:val4  |
key2:val2  |  key5:val5  |
key3:val3  |  key6:val6  |

您需要对此进行编码:

<tr>
    <td>
      key1: value1<br />
      key4: value4<br />
      key2: value2<br />
      key5: value5<br />
      key3: value3<br />
      key6: value6<br />
    </td>
  </tr>

这将是一团糟。也许还有另一种方法来实现你的目标?

答案 1 :(得分:0)

如果您在值中进行硬编码,则可以使用以下两列:

<table>
    <tr>
      <td>key1: value1</td>
      <td>key4: value4</td>
    </tr>
    <tr>
      <td>key2: value2</td>
      <td>key5: value5</td>
    </tr>
    <tr>
      <td>key3: value3</td>
      <td>key6: value6</td>
    <tr>
</table>

这将使您的答案如下:

key1: value1      key4: value4
key2: value2      key5: value5

如果你想动态地做你的列并且有一半和一半你可以使用以下php。它需要两个循环,但会得到你想要的。

<td>
   <?php for($i = 1; $i <= (sizeof($data)/2); $i++) {
             print $data[$i] + "<br/>";
         } 
   ?>
</td>
<td>
   <?php for($i = (sizeof($data)/2) + 1; $i <= sizeof($data); $i++) {
             print $data[$i] + "<br/>";
         } 
   ?>
</td>

答案 2 :(得分:0)

一种动态性更强的,可以配置的库姆语:

$columns = 4; // Change to whatever you want

$aData['key1'] = 'col A';
$aData['key2'] = 'col B';
$aData['key3'] = 'col C';
$aData['key4'] = 'col D';
$aData['key5'] = 'col E';
$aData['key6'] = 'col F';
$aData['key7'] = 'col G';
$aData['key8'] = 'col H';
$aData['key9'] = 'col I';
$aData['key10'] = 'col J';

$aColumns = [];
foreach( $aData as $key => $value ) {
    $aColumns[] = $key . ": " . $value;
}

$count_columns = count( $aColumns );

// by row
$current_row = 0;
echo "<table border='1'>";
while( ($current_row)*$columns < $count_columns ) {
    echo "<tr>";
    $offset = $current_row *$columns;

    for( $col = 0; $col < $columns; $col++ ) {
        echo "<td>";    
        echo $aColumns[$col +$offset] ?? "";
        echo "</td>";
    }
    echo "</tr>";
    $current_row++;
}
echo "</table>";

echo "<hr>";

// or by Col
$count_rows = ceil( $count_columns /$columns );
echo "<table border='1'>";
for( $row = 0; $row < $count_rows; $row++ ) {
    echo "<tr>";
    for( $col = 0; $col < $columns; $col++ ) {
        echo "<td>";    
        echo $aColumns[$row +($col*$count_rows) ] ?? "";
        echo "</td>";
    }
    echo "</tr>";
    $current_row++;
}
echo "</table>";

输出

<table border="1"><tbody><tr><td>key1: col A</td><td>key2: col B</td><td>key3: col C</td><td>key4: col D</td></tr><tr><td>key5: col E</td><td>key6: col F</td><td>key7: col G</td><td>key8: col H</td></tr><tr><td>key9: col I</td><td>key10: col J</td><td></td><td></td></tr></tbody></table>
<hr>
<table border="1"><tbody><tr><td>key1: col A</td><td>key4: col D</td><td>key7: col G</td><td>key10: col J</td></tr><tr><td>key2: col B</td><td>key5: col E</td><td>key8: col H</td><td></td></tr><tr><td>key3: col C</td><td>key6: col F</td><td>key9: col I</td><td></td></tr></tbody></table>