长度不等的多个列中的SQL输出


SQL output in multiple columns of unequal length

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

这将在表的单个列中返回大约50个Admit Roll number。但是我需要5列的输出,分别包含9、10、7、11、13项。

如何继续?

在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>";