简体   繁体   中英

MySQL - How to show in multiple columns html table, single column of mysql table

I want to know how to show in my php file with html a table with multiple columns but in that columns show only a single column results from mysqli

What I have for now is:

<?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...)

?>

In this example code this shows 2 HTML tables with 1 column but as entries are too much I want divide that column by 3 columns (or by 4 or 5, etc...) in each table.

How can I do this?

Example:

What I have and What I want

  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 |
  +---------+

I am not a php guy but some algo might help.1) get the count of records for male /female

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

save this to mcount and fcount variables.

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

else show list view

do same thing for fcount

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM