user3209031
user3209031

Reputation: 835

Table UI View : Mysql - Php

My Table View :

id  |   packetId    |   fweight    |     
1   |     002       |     150      |    
2   |     002       |     230      |   
3   |     002       |     200      |      

But i want My Table UI should be look a like below :

id  |   packetId    |   fweight    |    sum(fweight)
1   |     002       |     150      |    
2   |               |     230      |   
3   |               |     200      |        580

I want to avoid that repeat (002) value , also fweight sum should be shown in another coloumn on last insert id.

ID - Primary id ,

How i can get this in Mysql , php .

QUERY What i Know till now:

"SELECT id, packetId , fweight from product where packetId="002" ";

Upvotes: 0

Views: 160

Answers (1)

Mark
Mark

Reputation: 8431

Assuming you have all the header file/conn etc.

Try this:

$result = mysqli_query($con,"SELECT id, packetId , fweight from product where packetId='002'");

$tmp = "";
$ctr = 0;
$total = 0;
while($row = mysqli_fetch_array($result))
  {
      echo "<tr><td>";

      echo $row['id'];

      echo "</td><td>";
      if($ctr == 0 || $tmp != $row['id'])
      {
          echo $row['packetId'];
      }
      echo "</td><td>";

      echo $row['fweight'];

      $total = $total + $row['fweight'];

      echo "</td><td>";
      if($tmp != $row['id'])
      {
          echo $total;
          $total = 0;
          $tmp = $row['id'];
      }
      echo "</td></tr>";
      $ctr++;
  }

Upvotes: 1

Related Questions