0

我正在尝试使用 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>

请有人帮我映射这个。谢谢

4

4 回答 4

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>
于 2012-05-21T03:04:16.487 回答
1
$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>";
于 2012-05-21T03:04:25.667 回答
1

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

从您当前所在的任何行开始内部循环,每次添加 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>
于 2012-05-21T03:12:02.797 回答
0

这种方法将拆分数据本身

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>';
于 2012-05-21T03:13:27.310 回答