Jump to content

How to get record in while loop


Recommended Posts

producttable :

    id | mid | pid | owgh | nwgh |    1    3      12    1.5    0.6    2    3      12    1.5    0.3    3    3      14    0.6    0.4    4    3      15    1.2    1.1    5    4      16    1.5    1.0    6    4      17    2.4    1.2    7    3      19    3.0    1.4    8    3      19    3.0    1.2

I need result in while loop as **below** : as per mysql query

    $sql = "SELECT pid,owgh,nwgh from produttable      WHERE mid = 3 ";

**RESULT I WANT AS BELOW :**

Product Id  |  Old weight   | New weight    ---------------------------------------        12            1.5           0.6        12            1.5           0.3                 Tot: 3.0           0.9       --------------------------------        14            0.6           0.4                 Tot: 0.6           0.4       --------------------------------        15            1.2          1.1                 Tot: 1.2          1.1       --------------------------------        19            3.0          1.4        19            3.0          1.2                 Tot: 6.0          2.6

$query = mysql_query($sql);

 echo"<table>         <tr>           <td> Product Id </td>           <td> Old Weight </td>           <td> New Wight </td>        </tr>        ";        while($row = mysql_fetch_array($query )){            echo "<tr>";            echo "<td>".$row['pid']."</td>";            echo "<td>".$row['owgh']."</td>";            echo "<td>".$row['nwgh']."</td>";            echo "</tr>";            echo "<tr><td>-----</td>                      <td>Tot : </td>                      <td> </td></tr>";        }        echo "        </table>";

But result i am getting is as below

    Product Id  |  Old weight   | New weight    ---------------------------------------        12            1.5           0.6       -------------- Tot        12            1.5           0.3       ---------------Tot        14            0.6           0.4       ---------------Tot        15            1.2          1.1       ---------------Tot        19            3.0          1.4       ---------------Tot        19            3.0          1.2which i dont want , **i want as below** :    Product Id  |  Old weight   | New weight    ---------------------------------------        12            1.5           0.6        12            1.5           0.3                 Tot: 3.0           0.9       --------------------------------        14            0.6           0.4                 Tot: 0.6           0.4       --------------------------------        15            1.2          1.1                 Tot: 1.2          1.1       --------------------------------        19            3.0          1.4        19            3.0          1.2                 Tot: 6.0          2.6

**Update : 27/3/2015**Can i get below result, with sum of each

 Product Id  |  Old weight   | New weight    ---------------------------------------        12            1.5           0.6        12            1.5           0.3                 Tot: 3.0           0.9       --------------------------------        14            0.6           0.4                 Tot: 0.6           0.4       --------------------------------        15            1.2          1.1                 Tot: 1.2          1.1       --------------------------------        19            3.0          1.4        19            3.0          1.2                 Tot: 6.0          2.6------------------------------------------Grand Total :        6.3             5     Note in above Old weight : repeat value is not added thats 1.5 , 3.0 is coming twice    hence while addition takensingle of it**OR**    Product Id  |  Old weight   | New weight    ---------------------------------------        12            1.5           0.6                      1.5           0.3                 Tot: 3.0           0.9       --------------------------------        14            0.6           0.4                 Tot: 0.6           0.4       --------------------------------        15            1.2          1.1                 Tot: 1.2          1.1       --------------------------------        19            3.0          1.4                      3.0           1.2                                Tot: 6.0          2.6------------------------------------------       Grand Total :  6.3           5Note in above Old weight : repeat value is not added thats 1.5 , 3.0 is coming twice hence while addition takensingle of it

as product id 12, 19 are coming twice hence shown once with its resp valuePlease help me, if i am wrong in HTML tr td format please correct me , it not necessary to get in same table tr,td ..i just want my result in above format

Edited by shashib
Link to post
Share on other sites

Maybe...

 echo "<table>         <tr>           <td> Product Id </td>           <td> Old Weight </td>           <td> New Weight </td>        </tr>";        $pid = null;        $sumold = 0;        $sumnew = 0;        while($row = mysql_fetch_array($query)){            if ($row['pid'] == $pid || $pid === null){               echo "<tr>";               echo "<td>".$row['pid']."</td>";               echo "<td>".$row['owgh']."</td>";               echo "<td>".$row['nwgh']."</td>";               echo "</tr>";               $pid = $row['pid'];               $sumold += $row['owgh'];               $sumnew += $row['nwgh'];            }else{               echo "<tr><td></td><td></td><td></td></tr>                     <tr><td></td><td>Tot :".$sumold."</td><td>".$sumnew."</td></tr>                     <tr><td>------</td><td>------</td><td>------</td></tr>";               echo "<tr>";               echo "<td>".$row['pid']."</td>";               echo "<td>".$row['owgh']."</td>";               echo "<td>".$row['nwgh']."</td>";               echo "</tr>";               $sumold = $row['owgh'];               $sumnew = $row['nwgh'];               $pid = $row['pid'];            }        }        echo "<tr><td></td><td></td><td></td></tr>             <tr><td></td><td>Tot :".$sumold."</td><td>".$sumnew."</td></tr>             <tr><td>------</td><td>------</td><td>------</td></tr></table>";
  • Like 1
Link to post
Share on other sites

Hi ,

 

Can i get Grand Total : at last ? of each column

 

Please note : in old weight column weight are repeated twice , trice but i want its single value to be added as below :

Product Id  |  Old weight   | New weight    ---------------------------------------        12            1.5           0.6        12            1.5           0.3                 Tot: 3.0           0.9       --------------------------------        14            0.6           0.4                 Tot: 0.6           0.4       --------------------------------        15            1.2          1.1                 Tot: 1.2          1.1       --------------------------------        19            3.0          1.4        19            3.0          1.2                 Tot: 6.0          2.6------------------------------------------       Grand Total :  6.3            5 Note in above Old weight : repeat value is not added thats 1.5 , 3.0 is coming twice hence while addition takensingle of it**OR**    Product Id  |  Old weight   | New weight    ---------------------------------------        12            1.5           0.6                      1.5           0.3                 Tot: 3.0           0.9       --------------------------------        14            0.6           0.4                 Tot: 0.6           0.4       --------------------------------        15            1.2          1.1                 Tot: 1.2          1.1       --------------------------------        19            3.0          1.4                      3.0          1.2                                Tot: 6.0          2.6------------------------------------------      Grand Total :   6.3          5Note in above Old weight : repeat value is not added thats 1.5 , 3.0 is coming twice hence while addition takensingle of it
echo "<table><tr><td> Product Id </td><td> Old Weight </td><td> New Weight </td></tr>";$pid = null;$sumold = 0;$sumnew = 0;while($row = mysql_fetch_array($query)){if ($row['pid'] == $pid || $pid === null){echo "<tr>";echo "<td>".$row['pid']."</td>";echo "<td>".$row['owgh']."</td>";echo "<td>".$row['nwgh']."</td>";echo "</tr>";$pid = $row['pid'];$sumold += $row['owgh'];$sumnew += $row['nwgh'];}else{echo "<tr><td></td><td></td><td></td></tr><tr><td></td><td>Tot :".$sumold."</td><td>".$sumnew."</td></tr><tr><td>------</td><td>------</td><td>------</td></tr>";echo "<tr>";echo "<td>".$row['pid']."</td>";echo "<td>".$row['owgh']."</td>";echo "<td>".$row['nwgh']."</td>";echo "</tr>";$sumold = $row['owgh'];$sumnew = $row['nwgh'];$pid = $row['pid'];}}echo "<tr><td></td><td></td><td></td></tr><tr><td></td><td>Tot :".$sumold."</td><td>".$sumnew."</td></tr><tr><td>------</td><td>------</td><td>------</td></tr><tr><td>Grand Tol </td><td>Total OLD WGT</td><td>Total NEW WGT</td></tr></table>";

Please help me

Edited by shashib
Link to post
Share on other sites

Yes, you would just add two more variables. This might work...

echo "<table><tr><td> Product Id </td><td> Old Weight </td><td> New Weight </td></tr>";$pid = null;$sumold = 0;$sumnew = 0;$grandold = 0;$grandnew = 0;while($row = mysql_fetch_array($query)){if ($row['pid'] == $pid || $pid === null){echo "<tr>";echo "<td>".$row['pid']."</td>";echo "<td>".$row['owgh']."</td>";echo "<td>".$row['nwgh']."</td>";echo "</tr>";$pid = $row['pid'];$sumold += $row['owgh'];$sumnew += $row['nwgh'];}else{$grandold += $sumold;$grandnew += $sumnew;echo "<tr><td></td><td></td><td></td></tr><tr><td></td><td>Tot :".$sumold."</td><td>".$sumnew."</td></tr><tr><td>------</td><td>------</td><td>------</td></tr>";echo "<tr>";echo "<td>".$row['pid']."</td>";echo "<td>".$row['owgh']."</td>";echo "<td>".$row['nwgh']."</td>";echo "</tr>";$sumold = $row['owgh'];$sumnew = $row['nwgh'];$pid = $row['pid'];}}$grandold += $sumold;$grandnew += $sumnew;echo "<tr><td></td><td></td><td></td></tr><tr><td></td><td>Tot :".$sumold."</td><td>".$sumnew."</td></tr><tr><td>------</td><td>------</td><td>------</td></tr><tr><td>Grand Tol </td><td>Total OLD WGT:".$grandold."</td><td>Total NEW WGT:".$grandnew."</td></tr></table>";
Link to post
Share on other sites

Hi davej ,

 

Thanks for reply ...

 

Sir Old Weight is coming wrong .. its taking SUM of all ... but if you check above i need only SUM of ( 1.5 + 0.6 + 1.2 + 3.0 ) = 6.3

 

but its doing actually ( (1.5 + 1.5)+ 0.6 + 1.2 + (3.0 +3.0 ))

 

ii dont want that repeated value sum of same group but if sum of this should work ( 1.5 + 0.6 + 1.5 + (3.0 +3.0 )) in this 1.5 are of other group id hence its should added

 

Hope you got me ....

 

and sir New weight is working perfect ...

 

 

if i have another column suppose as name = another_weight which values are ( 1+2+4+5+6+7) = 25 .. which belong to each above resp. id ..then can i get sum of it

 

Thanks

Link to post
Share on other sites

Hmmm... I don't see the error, the grand totals are...

 

3.0 + 0.6 + 1.2 + 6.0 = 10.8

0.9 + 0.4 + 1.1 + 2.6 = 5.0

 

...but here is an alternative approach...

echo "<table><tr><td> Product Id </td><td> Old Weight </td><td> New Weight </td></tr>";$pid = null;$sumold = 0;$sumnew = 0;$grandold = 0;$grandnew = 0;while($row = mysql_fetch_array($query)){  if ($row['pid'] == $pid || $pid === null){    echo "<tr>";    echo "<td>".$row['pid']."</td>";    echo "<td>".$row['owgh']."</td>";    echo "<td>".$row['nwgh']."</td>";    echo "</tr>";    $pid = $row['pid'];    $grandold += $row['owgh'];    $grandnew += $row['nwgh'];    $sumold += $row['owgh'];    $sumnew += $row['nwgh'];  }else{    echo "<tr><td></td><td></td><td></td></tr>    <tr><td></td><td>Tot :".$sumold."</td><td>".$sumnew."</td></tr>    <tr><td>------</td><td>------</td><td>------</td></tr>";    echo "<tr>";    echo "<td>".$row['pid']."</td>";    echo "<td>".$row['owgh']."</td>";    echo "<td>".$row['nwgh']."</td>";    echo "</tr>";    $sumold = $row['owgh'];    $sumnew = $row['nwgh'];    $grandold += $row['owgh'];    $grandnew += $row['nwgh'];    $pid = $row['pid'];  }}echo "<tr><td></td><td></td><td></td></tr><tr><td></td><td>Tot :".$sumold."</td><td>".$sumnew."</td></tr><tr><td>------</td><td>------</td><td>------</td></tr><tr><td>Grand Tol </td><td>Total OLD WGT:".$grandold."</td><td>Total NEW WGT:".$grandnew."</td></tr></table>";
  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...