如何在表格中每4列创建一行?请不要告诉我,我应该把它们作为div,这需要是一张桌子......
这是我的代码:
$tmpRet = array_filter($ret, function($e){
return ($e['categoryID'] == 6) ? $e : null;
});
$tmpRet = array_values($tmpRet);
$tmpCt = count($tmpRet);
$counter = 0;
echo '<table width="100%" cellpadding="10" cellspacing="5">' . PHP_EOL;
for($i = 0; $i < $tmpCt; ++$i){
$counter ++;
echo ($i == 0 || $i % 4 === 0) ? ' <tr>' . PHP_EOL : null;
echo '<td width="25%" align="center" valign="middle"><a href="' . $tmpRet[$i]['sponUrl'] . '" target="_blank"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></a></td>' . PHP_EOL;
echo ($i == 0 || $i % 4 === 0) ? ' </tr>' . PHP_EOL : null;
}
echo '</table>';
它正在吐痰:
<table width="100%" cellpadding="10" cellspacing="5">
<tr>
<td width="25%" align="center" valign="middle"><a href="http://www.adp.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/ADP_logo_tag_R_rgb 4-11-13.jpg" alt="ADP" title="ADP" style="max-height:85px;" /></a></td>
</tr>
<td width="25%" align="center" valign="middle"><a href="http://ascensus.com/RetirementPlanSolutions/index.htm" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Ascensus - color.jpg" alt="Ascensus" title="Ascensus" style="max-height:85px;" /></a></td>
<td width="25%" align="center" valign="middle"><a href="http://www.getbridgepoint.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Bridgepoint 2012.JPG" alt="Bridgepoint" title="Bridgepoint" style="max-height:85px;" /></a></td>
<td width="25%" align="center" valign="middle"><a href="http://www.brightscope.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/BrightScope_Logo JPEG 10-10-12 approved.jpg" alt="BrightScope" title="BrightScope" style="max-height:85px;" /></a></td>
<tr>
<td width="25%" align="center" valign="middle"><a href="http://www.corporateinsight.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Corporate Insight Logo JPEG 9-26-12.JPG" alt="Corporate Insight" title="Corporate Insight" style="max-height:85px;" /></a></td>
</tr>
<td width="25%" align="center" valign="middle"><a href="http://www.guardianlife.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Guardian Logo from 2011 OK.JPG" alt="Guardian" title="Guardian" style="max-height:85px;" /></a></td>
<td width="25%" align="center" valign="middle"><a href="http://www.infosysbpo.com" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Infosys McCamish_natural_vert_nt jpeg.JPG" alt="Infosys McCamish" title="Infosys McCamish" style="max-height:85px;" /></a></td>
<td width="25%" align="center" valign="middle"><a href="http://ing.us/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/INGC300.JPG" alt="ING" title="ING" style="max-height:85px;" /></a></td>
<tr>
<td width="25%" align="center" valign="middle"><a href="https://www.janus.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/JANUS logoJPEG created 9-20-12.jpg" alt="Janus Capital Group" title="Janus Capital Group" style="max-height:85px;" /></a></td>
</tr>
<td width="25%" align="center" valign="middle"><a href="http://www.jpmorgan.com/pages/jpmorgan" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/JP Morgan Logo for infotree.JPG" alt="JP Morgan" title="JP Morgan" style="max-height:85px;" /></a></td>
<td width="25%" align="center" valign="middle"><a href="http://www.newkirk.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Newkirk a DST Co 4-23-13.JPG" alt="Newkirk" title="Newkirk" style="max-height:85px;" /></a></td>
<td width="25%" align="center" valign="middle"><a href="http://www.oculuspartners.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Oculus_logo_v4-vert.jpg" alt="Oculus" title="Oculus" style="max-height:85px;" /></a></td>
<tr>
<td width="25%" align="center" valign="middle"><a href="http://us.pioneerinvestments.com" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Pioneer Investments Jpeg ogo.JPG" alt="Pioneer Investments" title="Pioneer Investments" style="max-height:85px;" /></a></td>
</tr>
<td width="25%" align="center" valign="middle"><a href="http://www.processunity.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/ProcessUnity logo JPEG created 9-20-12.jpg" alt="ProcessUnity" title="ProcessUnity" style="max-height:85px;" /></a></td>
<td width="25%" align="center" valign="middle"><a href="http://www.prudential.com/view/page/public" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Prudential Blue Gray for Lanyards.JPG" alt="Prudential" title="Prudential" style="max-height:85px;" /></a></td>
<td width="25%" align="center" valign="middle"><a href="http://www.rrdonnelley.com" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/RR Donnelley Logo for Mints.JPG" alt="RR Donnelley" title="RR Donnelley" style="max-height:85px;" /></a></td>
<tr>
<td width="25%" align="center" valign="middle"><a href="http://fc.standardandpoors.com/sites/client/us/crpsite/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/sp_capitaliq_fc_logo.jpg" alt="S&P Capital IQ" title="S&P Capital IQ" style="max-height:85px;" /></a></td>
</tr>
</table>
答案 0 :(得分:5)
尝试使用以下代码替换代码中的相关行:
// At column 0 you create a new row <tr>
echo $i % 4 == 0 ? "<tr>\n" : "";
// At column 3 you end the row </tr>
echo $i % 4 == 3 ? "</tr>\n" : "";
每行创建4列。
echo "<table>\n";
$colSpan = 4;
$rows = 0;
for($i = 0; $i < 5; $i++) {
// At column 0 you create a new row <tr>
if($i % $colSpan == 0) {
$rows++;
echo "<tr>\n";
}
echo "<td>" . ($i + 1) . "</td>\n";
// At column 3 you end the row </tr>
echo $i % $colSpan == 3 ? "</tr>\n" : "";
}
// Say you have 5 columns, you need to create 3 empty <td> to validate your table!
for($j = $i; $j < ($colSpan * $rows); $j++) {
echo "<td></td>\n";
}
// Add the final <tr>
if(($colSpan * $rows) > $i) {
echo "</tr>\n";
}
echo "</table>\n";
这应该创造出足够完美的<table></table>
结构!享受。
答案 1 :(得分:3)
除非是第一次通过,否则您希望在回复</tr>
之前回复<tr>
。
echo "<tr>";
for($i = 0; $i < $tmpCt; ++$i){
$counter ++;
// echo a row close right before you row open unless it's the first one
echo ($i != 0 || $i % 4 === 0) ? ' </tr><tr>' . PHP_EOL : null;
echo '<td width="25%" align="center" valign="middle"><a href="' . $tmpRet[$i]['sponUrl'] . '" target="_blank"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></a></td>' . PHP_EOL;
}
// close out the last row
echo "</tr>";
答案 2 :(得分:1)
尝试使用嵌套的for
循环:
for($i = 0; $i < $tmpCt; ++$i){
$counter ++;
echo ' <tr>' . PHP_EOL;
for($j = 0; $j < 4 && $i < $tmpCt; ++$j, ++$i){
echo '<td width="25%" align="center" valign="middle"><a href="' . $tmpRet[$i]['sponUrl'] . '" target="_blank"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></a></td>' . PHP_EOL;
}
echo ' </tr>' . PHP_EOL;
}
可能出错的一点是,PHP可能不支持发生两次递增的for
循环(尚未测试),所以如果没有,则只需放置++$i
在内部for
循环的末尾。
这将通过4列或直到找到最后一列。这样$ j是4的简单计数器,$ i仍然是数组的引用。现在是伪代码,它是
for(every column){
start row;
for(4 iterations){
print or use the contents of a column;
}
end row;
}
修改强> 正如OrangePill建议的那样,这也可行
for($i = 0; $i < $tmpCt; $i+=4){
$counter ++;
echo ' <tr>' . PHP_EOL;
for($j = 0; $j < 4 && $i+$j < $tmpCt; ++$j){
echo '<td width="25%" align="center" valign="middle"><a href="' . $tmpRet[$i+$j]['sponUrl'] . '" target="_blank"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i+$j]['showType']) . '/images/sponsors/' . $tmpRet[$i+$j]['sponFileName'] . '" alt="' . $tmpRet[$i+$j]['sponName'] . '" title="' . $tmpRet[$i+$j]['sponName'] . '" style="max-height:85px;" /></a></td>' . PHP_EOL;
}
echo ' </tr>' . PHP_EOL;
}
答案 3 :(得分:0)
echo '<table width="100%" cellpadding="10" cellspacing="5">' . PHP_EOL . '<tr>' . PHP_EOL;
for($i = 1; $i <= $tmpCt; ++$i){
echo '<td width="25%" align="center" valign="middle"><a href="' . $tmpRet[$i]['sponUrl'] . '" target="_blank"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></a></td>' . PHP_EOL;
echo ($i % 4 === 0) ? ' <tr>' . PHP_EOL .'</tr>' . PHP_EOL : null;
}
for($j = 1; $j <= (4 - $tmpCT % 4); $j++){
echo '<td width="25%"> </td>' . PHP_EOL;
}
echo '</tr>' . PHP_EOL . '</table>';
答案 4 :(得分:0)
你有几个问题。
首先,你将第一个和第四个单元格包裹在tr。
中尝试更改此内容:
echo '<table width="100%" cellpadding="10" cellspacing="5">' . PHP_EOL;
for($i = 0; $i < $tmpCt; ++$i){
$counter ++;
echo ($i == 0 || $i % 4 === 0) ? ' <tr>' . PHP_EOL : null;
echo '<td width="25%" align="center" valign="middle"><a href="' . $tmpRet[$i]['sponUrl'] . '" target="_blank"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></a></td>' . PHP_EOL;
echo ($i == 0 || $i % 4 === 0) ? ' </tr>' . PHP_EOL : null;
}
echo '</table>';
对此:
echo '<table width="100%" cellpadding="10" cellspacing="5">' . PHP_EOL;
echo '<tr>';//start first row
for($i = 0; $i < $tmpCt; ++$i){
$counter ++;
echo '<td width="25%" align="center" valign="middle"><a href="' . $tmpRet[$i]['sponUrl'] . '" target="_blank"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></a></td>' . PHP_EOL;
echo ($i == 0 || $i % 4 === 0) ? '</tr><tr>' . PHP_EOL : null;//end row after every fourth cell and start a new one
}
echo '</tr>';//close out last row
echo '</table>';
根据页面的布局方式,您可能必须在最后一行插入一些空表格单元格,以防您的记录数量不能被4整除。