Jump to content

How to get record in while loop


shashib

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

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 comment
Share on other sites

If you want to group the records by product ID in the table then you need to order the query results by that column. As you're looping through the records you need variables to keep track of the current product ID, and the current totals for that product ID. Each time through the loop you'll print the record and add the values to the totals. If you find a record where the product ID in the record is not the same as the product ID in your variable, then you've gotten to a new product ID so you print the totals from the previous one and then reset the totals for the new one and keep going.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...