可以';不要让它以正确的PHP方式出现


Can't make it appear in the correct way PHP

我们今天有一个任务,其中我们必须回显数据库的一些数据。数据为month、case、vehicle_id。一个月内可能会有很多案例,而在一个案例中可能会有许多车辆ID。我要做的是以以下方式展示

month st_case veh_id
1     10001   1000011
1     10002   1000021
1     10002   1000022
2     10058   1000581

使用此代码:

<table border="1">
        <tr>
                <th>month</th>
        <th>st_case</th>
        <th>veh_id</th>
        </tr>
        <?php
        // Loop on rows in the result set.
        for($ri = 0; $ri < $numrows; $ri++) {
                echo "<tr>'n";
                $row = pg_fetch_array($result, $ri);
                echo " <td>", $row["month"], "</td>
        <td>", $row["st_case"], "</td>
        <td>", $row["veh_id"], "</td>
                </tr>
                ";
        }
        pg_close($link);
        ?>
</table>

问题是,我真正想做的是让它显示每个月的案例和每个案例的车辆。

1
  10001
     1000011
  10002
     1000021
     1000022
2
  10058
     1000581

我试着做这样的事情,但没有正确显示。如果有人能帮我,我真的很感激。

<table border="1">
        <tr>
                <th>month</th>
        <th>st_case</th>
        <th>veh_id</th>
        </tr>
        <?php
        // Loop on rows in the result set.
        $currentmonth = 0;
        for($ri = 0; $ri < $numrows; $ri++) 
        {
            $row = pg_fetch_array($result, $ri);
            if ($currentmonth != $row["month"])
            {
                echo "<tr>";
                echo "<td>";
                echo "<strong>".$row["month"]."</strong>";
                echo "</td>";
                echo "</tr>";
            }
            echo "<tr>";
            echo "<td>".$row["st_case"]."</td>";
            echo "</tr>";
            echo "<tr>";
            echo "<td>".$row["veh_id"]."</td>";
            echo "</tr>";
            $currentmonth = $row["month"];
        }
        pg_close($link);
        ?>
</table>

图片:http://i61.tinypic.com/2nb8vgl.png

$ret = pg_fetch_all($result);
$group = array();
foreach($ret as $v){
    $group[$v['month']][$v['st_case']][] = $v['veh_id'];
}
foreach($group as $month=>$value){
    echo $month."<br/>";
    foreach($value as $st_case=>$v){
        echo $st_case."<br/>";
        foreach($v as $veh_id){
             echo $veh_id."<br/>";
        }
    }
}

PS。然后添加一些css来样式表

如果不想在表中显示项目,请不要使用表你想要的是这样的东西:

<div class="result">
    <div class="month_group">
        <span class="month bold">1</span>
        <div class="case_group">
            <span class="case">10001</span>
            <div class="veh_group">
                <span class="veh_id">1000011</span>
            </div>
            <span class="case">10002</span>
            <div class="veh_group">
                <span class="veh_id">1000021</span>
                <span class="veh_id">1000022</span>
            </div>
        </div>
    </div>
    <div class="month_group">
        <span class="month">2</span>
        <div class="case_group">
            <span class="case">10058</span>
            <div class="veh_group">
                <span class="veh_id">1000081</span>
            </div>
        </div>
    </div>
</div>

然后剩下要做的就是应用简单的css给类一些填充/边距。HTML是一种标记语言。您可以使用HTML标记构建网页。然而,它的外观更多地取决于css(这就是为什么它被称为级联样式表)。

这是渲染代码。我还没有测试过它,也有一段时间没有接触过PHP,但它应该会给你一些大致的想法:

echo "<div class='"result'">";
for($ri = 0; $ri < $numrows; $ri++) {
    $row = pg_fetch_array($result, $ri);
    $month = $row["month"];
    echo "<div class='"month_group'">'n";
    echo "<span class='"month'">".$month."</span>'n";
    echo "<div class='"case_group'">'n";
    for ($ci = $ri; $ci < $numrow; $ci++) {
        if ($ci == $ri) {
            $c_row = $row;
        } else {
            $c_row = pg_fetch_array($result, $ci);
        }
        if ($c_row["month"] != $month) {
            // We have moved to another month
            break;
        }
        $case = $c_row["st_case"];
        echo "<span class='"case'">".$case."</span>'n";
        echo "<div class='"veh_group'">'n";
        for ($vi = $ci; $vi < $numrow; $vi++) {
            if ($vi == $ci) {
                $v_row = $c_row;
            } else {
                $v_row = pg_fetch_array($result, $vi);
            }
            if ($v_row["st_case"] != $case) {
               // We have moved to another case
                break;
            }
            $veh = $v_row["veh_id"];
            echo "<span class='"veh_id'">".$veh."</span>'n";
            // we have already processed rows whose indexes are less or equal $vi
            $ri = $vi;
        }
        echo "</div>";
    }
    echo "</div></div>";
}

尝试此更新的代码

<table border="1">
   <tr>
      <th>month</th>
      <th>st_case</th>
      <th>veh_id</th>
   </tr>
   <?php
       // Loop on rows in the result set.
       $currentmonth = 0;
       for($ri = 0; $ri < $numrows; $ri++) 
       {
         $row = pg_fetch_array($result, $ri);
            // Open the row
            echo "<tr>";
             echo "<td><strong>" . $row["month"] . "</strong></td>"; //open and close each column
             echo "<td>".$row["st_case"]."</td>";
             echo "<td>".$row["veh_id"]."</td>";
            echo "</tr>"; //close the row
           $currentmonth = $row["month"];
        }
        pg_close($link);
    ?>
</table>

您还需要调用MySQL语句中的ORDER BY关键字来按月订购。

这里有一个关于如何做到这一点的简单教程。

下面是它在应用程序中的演示。

让我知道这是否有帮助,如果我可以提供任何其他帮助。