【问题标题】:PHP AND FOR LOOPPHP 和 FOR 循环
【发布时间】:2012-05-21 02:56:20
【问题描述】:

我正在尝试使用 for 循环创建一个动态返回如下内容的表:注意 td 内容的排列方式

<table>
 <tr>
  <td>1</td>
  <td>3</td>
  <td>5</td>
  <td>7</td>
  <td>9</td>
 </tr>
 <tr>
  <td>2</td>
  <td>4</td>
  <td>6</td>
  <td>8</td>
  <td>10</td>
 </tr>
</table>

到目前为止我尝试过的是

$row=2;
$col=20;
$x=0;
echo '<table>';
for($i=0;$i<$row;$i++){
   echo '<tr>';
     for($k=0;$k<$col;$k++)
     {
        echo '<td>'.echo $x+=1.'</td>';
     }
   echo '</tr>';

}

在这种情况下,我得到了一些不同的东西,这不是我想要的。

<table>
    <tr>
     <td>1</td>
     <td>2</td>
     <td>3</td>
     <td>4</td>
     <td>5</td>
    </tr>
    <tr>
     <td>6</td>
     <td>7</td>
     <td>8</td>
     <td>9</td>
     <td>10</td>
    </tr>
  </table>

请有人帮我映射一下。谢谢

【问题讨论】:

  • 感谢指正。发布时尝试格式化,因此出现拼写错误。抱歉
  • 您只是想要一个带有数字的表格,还是数字是您希望数据排序方式的示例?即你是在追逐这个循环的盗版使用吗?
  • 我想在表格中显示内容。他们有没有格式的索引。简而言之,我的桌号应该以这种方式排列
  • 是你知道你想要的列数还是你知道你想要的行数?

标签: php html for-loop


【解决方案1】:
<?php 
  # how high to count
  $count = 10;

  # how many rows in the table
  $rows = 2;

  # figure out how many columns
  $columns = ceil($count/$rows);

  # could also go the other way and declare there to be 5 columns and then 
  # divide to get the rows, but since you're counting down the columns, 
  # I thought this made more sense.  Either way.

?><table><?php 

  for ($i=0; $i<$rows; ++$i) {

 ?><tr><?php

    for ($j=0; $j<$columns; ++$j) {

      # calculate which number to show in column $j of row $i.  Each column adds
      # $rows numbers to the total, while each row just adds one more.  And we
      # want to start at 1 instead of 0.
      $n = $j * $rows + $i + 1;

      ?><td><?= $n ?></td><?php 
    }

  ?></tr><?php

  }
?></table>

【讨论】:

    【解决方案2】:
    $total_count = 10;
    $total_rows = 2;
    
    $table = array();
    //building table array
    for($i=1;$i<=$total_count;$i++) {
        $row = $i % $total_rows;
        $row = $row == 0 ? $total_rows : $row;
        $table[$row][] = $i;
    }
    
    //generate table based on array
    echo "<table>";
    for($row=1;$row<=$total_rows;$row++) {
        echo "<tr>";
        foreach($table[$row] as $cell) {
            echo "<td>".$cell."</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
    

    【讨论】:

    • 这里不需要数组。它比这简单得多。
    【解决方案3】:

    这并不像人们想象的那么复杂

    从当前所在行开始内循环,每次加 2。

    <?php
    $rows=2;
    $cols=10;
    ?>
    
    
    <table>
    <?php for($i=1;$i<=$rows;$i++): ?>
        <tr>
        <?php for($k=$i;$k<$cols;$k+=2): ?>
            <td><?php echo $k ?></td>
        <?php endfor; ?>
        </tr>
    <?php endfor; ?>
    </table>
    

    我可能会使用 range 和 foreach

    <?php
    $rows=2;
    $cols=10;
    ?>
    
    <table>
    <?php foreach( range( 1, $rows ) as $row ): ?>
        <tr>
        <?php foreach( range( $row, $cols, 2 ) as $col ): ?>
            <td><?php echo $col ?></td>
        <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
    </table>
    

    【讨论】:

    • 这取决于已知的情况,盖伦。如果预先确定应该有 5 列,那就太好了。但如果它只是“我想在两行中以列优先顺序数到 N”,那么你必须做一些数学运算。我认为我的解决方案非常简单。
    【解决方案4】:

    这种方法会拆分数据本身

    function array_chunk_vertical($array = null,$cols = 3, $pad = array(null))
    {
      if (is_array($array) == true and !empty($array))
      {
        $rows = ceil(count($array)/$cols);
        $array = array_chunk($array,$rows);
        if (count($array) < $cols)
        {
          $array = array_pad($array,$cols,$pad);
        }
        foreach($array as $key => $value)
        {
          $array[$key] = array_pad($value,$rows,null);
        }
        foreach($array as $key => $value)
        {
          foreach($value as $sub_key => $sub_value)
          {
            $output[$sub_key][$key] = $sub_value;
          }
        }
        return $output;
      }
      return $array;
    }
    
    $data = array(1,2,3,4,5,6,7,8,9,10,11);
    
    echo '<table border="1">';
    foreach(array_chunk_vertical($data,ceil(count($data)/2)) as $row)
    {
      echo '<tr>';
      foreach($row as $col)
      {
        echo '<td>' . $col . '</td>';
      }
      echo '</tr>';
    }
    echo '</table>';
    

    【讨论】:

      猜你喜欢
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      相关资源
      最近更新 更多