【问题标题】:How to create two columns PHP?如何创建两列PHP?
【发布时间】:2016-10-18 07:24:30
【问题描述】:

我在尝试创建它时遇到了最糟糕的情况,它应该很简单。我有一个由$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

想法?这应该非常简单。

【问题讨论】:

  • 这对我没有任何帮助。我要求解释如何完成这项工作
  • 在第二个名称 (在同一个 中)输入 $info[$key+6] 值。

标签: php arrays html-table


【解决方案1】:

看到您正在为每个人创建一个&lt;tr&gt;,请尝试执行以下操作:

$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++;
}

检查%modulus

【讨论】:

    【解决方案2】:

    您将始终有“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 列信息,而不是格式化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-19
      • 2020-04-29
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 2021-03-27
      相关资源
      最近更新 更多