3

次のような動的な行を作成しようとしています:

[1] [7] [13]

[2] [8] [14]

[3] [9] [15]

[4] [10]

[5] [11]

[6] [12]

現在、次のように出力しています。

[1] [2] [3]

[4] [5] [6]

[7] [8] [9]

[10] [11] [12]

[13] [14] [15]

別の列を作成する前に、アルファベット順/数字順に 6 行出力したいと考えています。以下のコードは、1 つの行に 3 つのデータを作成し、別の 3 つのデータ用に別の行を作成するためのものです。出力したい画像を添付できないことをお詫びします。

<?php

$query = "SELECT name FROM categories WHERE parent_id=1 ORDER BY order_id ASC";
$result = mysql_query($query) or die ("Query failed");

$numrows = (mysql_num_rows ($result));


if($numrows >0){
echo "<table width = 100% border = '0' cellspacing = '2' cellpadding = '0'><tr>";

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

    echo "<td width='33%'>";

    while ($friendList = mysql_fetch_array($result)){

     $end = '<div class="left">'.$friendList['name'].'</div>'; 
      if ( isset($friendList[$j+6] )) 
      { 
        $end = '<div class="right">'.$friendList[$j+6].'</div>'; 
      } 
     echo $end."\n"; 

    }
    echo "</td>";
}
echo $end."</tr></table> ";

}

?>

ありがとうございました。

4

1 に答える 1

-2

返される行数がわかっているので、これ$numrowsを利用してテーブルを事前に構築し、レコードのループが終了した後にアセンブルできます。

$maxRows = 6;
$numCols = ceil($numRows / $maxRows);

テーブルの作成中に、返された値をループして、出力を配列に割り当てます。すべてのレコードが追加されたら、テーブルをアセンブルして出力します。

注:番号付けはインデックス作成専用であるため、クエリでレコードが適切に順序付けられていることを確認する必要があります。

<?php

$numRows = 41;
$maxRows = 6;
$numCols = ceil($numRows / $maxRows);

$records = array();

for($i=1; $i <= $maxRows; $i++){
    $records[$i] = array();
}

for($i=1; $i <= $numRows; $i++){
    $row = ($i % $maxRows == 0 ? $maxRows : $i % $maxRows);
    $records[$row][]  = "[$i]";
}

$table = ['<table>'];

for($i=1; $i <= count($records); $i++){
    $row = ['<tr>'];
    $col = $records[$i];

    for($j=0; $j < count($col); $j++){

        /*
         * Begin modification area:
         * 
         * You would change the value of $col[$j] to reflect the desired 
         * output within each cell.
         */

         $output = $col[$j];

        /* End modification area */

         $row[$j+1] = '<td>' . $output . '</td>';
    }
    array_push($row, '</tr>');
    $table[$i] = implode('', $row);
}

$table[$i++] = '</table>';
echo implode('', $table);

?> 

出力:

[1] [7]  [13] [19] [25] [31] [37] 
[2] [8]  [14] [20] [26] [32] [38] 
[3] [9]  [15] [21] [27] [33] [39] 
[4] [10] [16] [22] [28] [34] [40] 
[5] [11] [17] [23] [29] [35] [41] 
[6] [12] [18] [24] [30] [36] 
于 2012-07-12T23:45:26.370 に答える