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>

親切に誰かがこれをマッピングするのを手伝ってくれます.Thanks

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>

Idはおそらく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 に答える