SQL输出在不同长度的多列中

时间:2014-05-12 01:29:06

标签: php sql

while($row = mysql_fetch_array($result41))
{
   echo "<tr><td align='center'>" .$row['AdmitRollNo'] . " </td></tr>";
}

这会在表格的一列中返回大约50个Admit Roll Numbers。但我需要5列中的输出,分别包含9,10,7,11,13项。

如何进行?

1 个答案:

答案 0 :(得分:2)

在PHP中使用modulus运算符将是一个良好的开端。这应该有效:

// Set how many items per column.
$per_column = 9;

// Set the opening table row.
echo "<tr>";

// Setting the counter to 0.
$counter = 0;

while ($row = mysql_fetch_array($result41)) {
   // Increment the counter by one.
   $counter++;

   // Do a modulus check between the counter & the per column value.
   if ($counter % $per_column == 0) {
     echo "</tr><tr>";
   }

   // Echo the table data columns.
   echo "<td align='center'>" . $row['AdmitRollNo'] . " </td>";

}

// Set the closing table row.
echo "</tr>";

但是你每列都说9, 10, 7, 11, 13项。有点棘手。但我相信可以做到。这是一个我刚刚掀起的概念。我们的想法是有一个名为$per_column_array的每列值数组加上一个名为$per_counter的计数器。第一次达到模数时,计数器会增加1,因此会抓取下一个$per_column_array值。

// Set how many items per column.
$per_counter = 0;
$per_column_array = array(9, 10, 7, 11, 13);

// Set the opening table row.
echo "<tr>";

// Setting the counter to 0.
$counter = 0;

while ($row = mysql_fetch_array($result41)) {
   // Increment the counter by one.
   $counter++;

   // Do a modulus check between the counter & the per column value.
   if ($counter % $per_column_array[$per_counter] == 0) {
     $per_counter++;
     echo "</tr><tr>";
   }

   // Echo the table data columns.
   echo "<td align='center'>" . $row['AdmitRollNo'] . " </td>";

}

// Set the closing table row.
echo "</tr>";

另一个编辑:以下是对列的尝试。请随意调整,但基本概念是将值放在您已有的<table>结构中的嵌套<table>中:

// Set how many items per column.
$per_counter = 0;
$per_column_array = array(9, 10, 7, 11, 13);

// Set the opening table row.
echo "<tr><td>";
echo "<table>";

while ($row = mysql_fetch_array($result41)) {
   // Increment the counter by one.
   $counter++;

   // Do a modulus check between the counter & the per column value.
   if ($counter % $per_column_array[$per_counter] == 0) {
     $per_counter++;
     echo "</table>";
     echo "</td>";
     echo "<td>";
     echo "<table>";
   }

   // Echo the table data columns.
   echo "<tr><td align='center'>" . $row['AdmitRollNo'] . " </td></tr>";

}

// Set the closing table row.
echo "</table>";
echo "</td></tr>";