3

我需要帮助尝试$results分开$row['citizenship']

  1. 我不知道如何遍历结果。
  2. 我对 PHP 的限制不止于此。

所以,我会很感激一些广泛的支持。

我希望能够做到这一点,但问题是我不知道如何通过循环来分离它:

---------------------------------------------------------------------
|citizen ID | Citizen Name | Citizenship| Date    | User IP         |
---------------------------------------------------------------------
| 123455    | Any Name     | Albania    |12/01/12 | 123.1234.123.121|
| 123455    | Any Name  2  | Albania    |12/01/12 | 123.1234.123.121|
---------------------------------------------------------------------

---------------------------------------------------------------------
|citizen ID | Citizen Name | Citizenship| Date    | User IP         |
---------------------------------------------------------------------
| 123455    | Any Name  3  | Argentina  |12/01/12 | 123.1234.123.121|
| 123455    | Any Name  4  | Argentina  |12/01/12 | 123.1234.123.121|
---------------------------------------------------------------------

下面是我的 PHP 页面:

$con=mysqli_connect("localhost","citizens","password","citizens");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$color ="#ffffff";
$color3 ="#6CC";
$fontsize ="2";
$color3 ="#69C";

echo "<center><table border='3' cellspacing='0' cellpadding='2' class='sortable' bordercolor='#6699FF' width='100%'>
<tr>
<th >CitizenID</th>
<th>Citizen Name</th>
<th>Citizenship</th>
<th>Date Entry</th>
<th>IP of Admin</th>
</tr>";

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

echo "<tr>";
echo "<td align='center' valign='middle' width='75px' bgcolor='" . $color3 . "'><font size='" . $fontsize . "'><a href='http://www.erepublik.com/en/citizen/profile/" . $row['citid'] . "' target='_blank'>".$row['citid']."</a></td>";
echo "<td align='left' valign='middle' bgcolor='" . $color . "'><font size='" . $fontsize . "'>" . $row['citname'] . "</td>";
echo "<td align='left' valign='middle' bgcolor='" . $color . "'><font size='" . $fontsize . "'>" . $row['citizenship'] . "</td>";
echo "<td align='left' valign='middle' bgcolor='" . $color . "'><font size='" . $fontsize . "'>" . $row['date'] . "</td>";
echo "<td align='left' valign='middle' bgcolor='" . $color . "'><font size='" . $fontsize . "'>" . $row['user_ip'] . "</td>";
echo "</tr>";
  }
echo "</table>";
echo "<br>";

mysqli_close($con);
4

1 回答 1

3

Change

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

to

$results = mysqli_query($con,"SELECT * FROM `data` ORDER BY Citizenship ASC, Citizen_Name ASC"); 
// make sure to select correct column name for Citizenship & Citizen_Name.


then in the while loop check if Citizenship value is changing & add a new row if Citizenship changes.

while($row = mysqli_fetch_array($results))
  {

    if($row['citizenship'] <> $current_citizenship){
        $current_citizenship = $row['citizenship'];
        echo "<tr>";
        echo "<td align='middle' colspan="5" valign='middle' bgcolor='" . $color . "'><font size='" . $fontsize . "'>" . $row['citizenship'] . "</td>";
        echo "</tr>";
    }

    echo "<tr>";
    echo "<td align='center' valign='middle' width='75px' bgcolor='" . $color3 . "'><font size='" . $fontsize . "'><a href='http://www.erepublik.com/en/citizen/profile/" . $row['citid'] . "' target='_blank'>".$row['citid']."</a></td>";
    echo "<td align='left' valign='middle' bgcolor='" . $color . "'><font size='" . $fontsize . "'>" . $row['citname'] . "</td>";
    echo "<td align='left' valign='middle' bgcolor='" . $color . "'><font size='" . $fontsize . "'>" . $row['citizenship'] . "</td>";
    echo "<td align='left' valign='middle' bgcolor='" . $color . "'><font size='" . $fontsize . "'>" . $row['date'] . "</td>";
    echo "<td align='left' valign='middle' bgcolor='" . $color . "'><font size='" . $fontsize . "'>" . $row['user_ip'] . "</td>";
    echo "</tr>";

}
于 2013-05-10T19:20:01.810 回答