MySQL - 如何在多列html表中显示mysql表的单列

时间:2015-02-17 20:17:23

标签: php html mysql mysqli html-table

我想知道如何在我的php文件中用html显示一个包含多列的表,但是该列只显示来自mysqli的单列结果

我现在拥有的是:

<?php

include "dbinc.php";

$con=mysqli_connect($servername,$username,$password,$database);

$letramasculinos = mysqli_query($con,"SELECT nome FROM nomes WHERE nome LIKE '$_POST[letra]%' AND sexo = 'm';");
$letrafemininos = mysqli_query($con,"SELECT nome FROM nomes WHERE nome LIKE '$_POST[letra]%' AND sexo = 'f';");

     (more code...)

echo "<table style='display: inline-block;' width='150px' border='1' bgcolor='#6699FF'>";
while($row = mysqli_fetch_array($letramasculinos))
{
echo "<tr>";
echo "<td><center>" . $row['nome'] . "</center></td>";
echo "</tr>";
echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

echo "<table style='display: inline-block;' width='150px' border='1' bgcolor='#FF99FF'>";

while($row = mysqli_fetch_array($letrafemininos))
{
echo "<tr>";
echo "<td><center>" . $row['nome'] . "</center></td>";
echo "</tr>";
}
echo "</table>";
}

     (more code...)

?>

在这个示例代码中,这显示了2个包含1列的HTML表,但由于条目太多,我希望在每个表中将该列除以3列(或4或5,等等)。

我该怎么做?

示例:

我拥有什么以及我想要什么

  I have this:      I Want Something like this:

  +---------+     +--------+---------+--------+
  | Diogo   |     | Diogo  | André   | João   |
  +---------+     +--------+---------+--------+
  | André   |     | Filipe | Carlos  | Gaspar |
  +---------+     +--------+---------+--------+
  | João    |     | Fabio  | Fernado |        |
  +---------+     +--------+---------+--------+
  | Filipe  |
  +---------+
  | Carlos  |
  +---------+
  | Gaspar  |
  +---------+
  | Fabio   |
  +---------+
  | Fernado |
  +---------+

1 个答案:

答案 0 :(得分:0)

我不是一个虚拟人,但有些算法可能有帮助.1)得到男/女的记录数

"SELECT count(*)FROM nomes WHERE nome LIKE '$_POST[letra]%' AND sexo = 'f'; ..... same for 'male'.  

将此保存到mcount和fcount变量。

if mcount >10
then 
 noOfColms=mcount /10;
 i=0;
 put "<table>"
loop records 
i=i+1;
put "<tr>"
if(i<noOfColms)
   put "<td>" + data +"</td>"
if i==noOfColms
put "</tr>"     
end loop   
put "</table>"

否则  显示列表视图

为fcount做同样的事情