-2

嗨,我只是 phpmysql 的初学者,并试图在单独的表格中逐行显示结果。

我有这个查询:

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

我知道这会将所有结果输出到一张桌子上。我想将结果放在单独的表中,具体取决于$row['citizenship']

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

4

2 回答 2

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>"
于 2013-05-05T06:10:54.203 回答
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
    }
于 2013-05-05T06:11:30.623 回答