captain monk
captain monk

Reputation: 727

Can't make it appear in the correct way PHP

we have an assignment for today in which we have to echo some data of the database. The data is month, case, vehicle_id. In a month there can be many cases and in a case there can be many vehicle_ids. What i have achieve to do is to show it in the following way

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

using this code:

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

The problem is that what i really want to do is to make it show for each month the cases and for each case the vehicles.

1
  10001
     1000011
  10002
     1000021
     1000022
2
  10058
     1000581

I tried to do something like this but it doesn't show correctly. If someone could help me with this i would be really thankfull.

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

picture: http://i61.tinypic.com/2nb8vgl.png

Upvotes: 0

Views: 87

Answers (3)

Master Yoda
Master Yoda

Reputation: 4422

Try this updated code

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

You will also need to call the ORDER BY keyword in your MySQL statement to order by month.

Here is a simple tutorial on how to do this.

Here is a Demo of how it might look in your application.

Let me know if this helps and if i can be of any other help.

Upvotes: 0

Quan To
Quan To

Reputation: 696

If you do not intend to display items in a table, do not use table What you want is something like this:

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

Then all that's left to do is applying simple css to give the classes some padding/margin. HTML is a markup language. You structure the web page using the HTML tags. However, how it would look like depends more on css (which is why it's called Cascading Style Sheets).

Here's the rendering code. I have not tested it yet, and I have not touched PHP for sometime, but it should give you some general ideas:

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

Upvotes: 1

Qingfeng
Qingfeng

Reputation: 720

$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.then add some css to style your table

Upvotes: 1

Related Questions