按国家分开

时间:2013-05-10 19:06:56

标签: php mysql

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

  1. 我不知道如何循环结果。
  2. 我对PHP的限制不仅限于此。
  3. 所以,我希望得到一些广泛的支持。

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

    ---------------------------------------------------------------------
    |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);
    

1 个答案:

答案 0 :(得分:3)

更改

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

$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.


然后在while循环中检查Citizenship值是否正在变化&amp;如果Citizenship发生更改,请添加新行。

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

}