[英]Alternate multiple columns in an html table with php
How can I output an alternate multiple column like this 我如何输出这样的替代多列
<table border="1">
<tr>
<td> userid 1 </td>
</tr>
<tr>
<td> userid 2</td> <td>userid 3 </td>
</tr>
<tr>
<td> userid 4 </td>
</tr>
<tr>
<td> userid5 </td> <td>userid6 </td>
</tr>
<tr>
<td> userid7 </td>
<td>userid8 </td>
<td> userid9 </td>
<td>userid10</td>
</tr>
</table>
<br>
<table border=1>
My table query is like this: 我的表查询是这样的:
$result = mysql_query("SELECT id,name FROM `tbl-record`") or die(mysql_error());
Classic example in action is like this: 实际的经典示例如下所示:
http://www.cashcashpinoy.com
A table with five rows and an alternate 1x2x1x2x4 columns (TD ) on each rows ( TR ) 具有五行的表,每行(TR)上都有交替的1x2x1x2x4列(TD)
Since this is in a table, you can use the colspan
attribute on the td
elements, like this: 由于这是在表中,因此可以在td
元素上使用colspan
属性,如下所示:
<table>
<tr>
<td colspan="4">Full width data.</td>
</tr>
<tr>
<td colspan="2">Half width data.</td>
<td colspan="2">Half width data.</td>
</tr>
<tr>
<td colspan="1">Quarter width data.</td>
<td colspan="1">Quarter width data.</td>
<td colspan="1">Quarter width data.</td>
<td colspan="1">Quarter width data.</td>
</tr>
</table>
You can do this with any number of columns. 您可以使用任意数量的列进行此操作。
If you want to assign a number of columns dynamically, you'll want to have a set number of results to provide some consistency, which you could do using a LIMIT
along with your query. 如果要动态分配多个列,则需要具有一定数量的结果以提供一定的一致性,您可以将LIMIT
与查询一起使用。
$results; // This is all of the results of your query
$colOps = array(1,2,4); // Different colspan values
$numCols = 4; // Maximum columns to allow per line
echo '<table border="1">';
while(count($results) > 0) {
$ind = mt_rand(0, 2); // Generate a random index number
if(count($results) >= $colOps[$ind]) {
echo '<tr>';
for($i = 0; $i < $cols; $i++)
echo '<td colspan="'.$colOps[$ind].'">'.array_shift($results).'</td>';
echo '</tr>';
}
}
echo '</table>';
Note that this is untested and may need some modification to work properly. 请注意,这未经测试,可能需要进行一些修改才能正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.