在单独的表中显示mysql

时间:2013-05-05 05:58:41

标签: php mysqli

您好我只是phpmysql的初学者,并试图在一行中单独显示结果。

我有这个问题:

$albania = mysqli_query($con,"SELECT * FROM `data` ORDER BY citid ASC");
while($row = mysqli_fetch_array($albania))

我知道这会将所有结果输出到1个表格中。我想根据$row['citizenship']

将结果放到单独的表中

我在该行中有大约40个不同的结果,所以我想将它与该行分开并单独输出。

2 个答案:

答案 0 :(得分:0)

如果您按要分割的字段(citizenship)进行排序并跟踪当前值(使用$currentCitizenship完成),那么您应该只需查找该值并在下一节开始时开始一个新表。

$albania = mysqli_query($con,"SELECT * FROM `data` ORDER BY citizenship ASC, citid ASC");
$currentCitizenship = ""

while($row = mysqli_fetch_array($albania)){
   //handle the first time through the loop and start the first table
   if ($currentCitizenship = ""){
       //Start the first table
       echo "<table>";
       $currentCitizenship = $row['citizenship'];
   }elseif ($currentCitizenship != $row['citizenship']){
       //change to next citizenship table
       echo "</table><table>";
       $currentCitizenship = $row['citizenship'];
   }
   /*
      Your current row printing code
   */
}
//close off the last table
echo "</table>"

答案 1 :(得分:0)

你所写的内容还不会输出任何内容。它只会从数据库中获取所有行。您需要在PHP中添加代码以将其显示在屏幕上。

像这样的东西。需要使用正确的php语法:

    $albania = mysqli_query($con,"SELECT * FROM `data` ORDER BY citid ASC");
    while($row = mysqli_fetch_array($albania))
    {
       if($row['citizenship'] equals 'valuea')
              print in one table
       else
              print in another table
    }