我在尝试创建它时遇到了最糟糕的时刻,它应该很简单。我有一个由$person=>$personInfoArray
组成的$info
数组,我试图说,对于前六个,做一列,对于下一个x,做另一列。
我拥有的是:
$infoCounter = 0;
foreach($info as $person => $information){
$infoCounter++;
echo '<tr>';
if($infoCounter <= 5){
echo '<td>'.$person.'</td>';
echo '<td>'.$information['Extension'].'</td>';
}elseif($infoCounter > 5){
echo '<td>'.$person.'</td>';
echo '<td>'.$information['Extension'].'</td>';
}
echo '</tr>';
}
我本质上是在尝试创建一个表,看起来像:
Name Extension Name Extension
--------------------------------------------------------------------
Some one 54545 Name 9785
Some One else 54212 Something else 44121
但我得到了:
Name Extension Name Extension
--------------------------------------------------------------------
Some one 54545
Name 9785
Some One else 54212
Something else 44121
想法?这应该非常容易。
如果您正在为每个人创建一个<tr>
,请尝试执行以下操作:
$infoCounter = 0;
foreach($info as $person => $information){
if ($infoCounter % 2 == 0) {
echo '<tr>';
}
echo '<td>'.$person.'</td>';
echo '<td>'.$information['Extension'].'</td>';
if ($infoCounter % 2 == 0) {
echo '</tr>';
}
$infoCounter++;
}
检查%
的模量
您将始终有"2"列包含此代码。我想你想要的是这个。
$counter = 0;
foreach($info as $person => $information){
if($counter == 0) echo "<tr>";
echo "<td>$person</td>";
echo "<td>$information</td>";
$counter += 2;
if($counter == 4) {
$counter = 0;
echo "</tr>";
}
}
请记住,这太粗鲁了,让我难以想象。不确定为什么除了格式化之外,还需要4列作为2列信息。