如何在while循环中获取记录 [英] How to get record in while loop

查看:101
本文介绍了如何在while循环中获取记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我需要在while循环中以低于结果:按照mysql查询

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

结果我希望如下:

 产品ID |旧重量|新体重
---------------------------------------
12 1.5 0.6
12 1.5 0.3

总计:3.0 0.9
------------------------- -------
14 0.6 0.4

总计:0.6 0.4
-------------------- ------------
15 1.2 1.1

总计:1.2 1.1
--------------- -----------------
19 3.0 1.4
19 3.0 1.2

总计:6.0 2.6




$ query = mysql_query($ sql);



  echo< table> 
< tr>
< td>产品ID< / td>
< td>旧权重< / td>
< td>新怀特< / 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>;
回声< / tr>;
echo< tr>< td> -----< / td>
< td> Tot:< / td>
< td>< TD>< / TR>中;
}
回显
< / table>;

但是我得到的结果如下

 产品ID |旧重量|新体重
---------------------------------------
12 1.5 0.6
-------------- Tot
12 1.5 0.3
--------------- Tot
14 0.6 0.4
---------------小费
15 1.2 1.1
-------------- -Tot
19 3.0 1.4
--------------- Tot
19 3.0 1.2

我不想要,我想要如下

 产品ID |旧重量|新体重
---------------------------------------
12 1.5 0.6
12 1.5 0.3

总计:3.0 0.9
------------------------- -------
14 0.6 0.4

总计:0.6 0.4
-------------------- ------------
15 1.2 1.1

总计:1.2 1.1
--------------- -----------------
19 3.0 1.4
19 3.0 1.2

总计:6.0 2.6

更新日期:30/3/2015

我可以得到低于结果,每个的总和

 产品Id |旧重量|新体重
---------------------------------------
12 1.5 0.6
12 1.5 0.3

总计:3.0 0.9
------------------------- -------
14 0.6 0.4

总计:0.6 0.4
-------------------- ------------
15 1.2 1.1

总计:1.2 1.1
--------------- -----------------
19 3.0 1.4
19 3.0 1.2

总计:6.0 2.6
--- --------------------------------
GRAND TOTAL = 6.3 5







OR

 产品ID |旧重量|新体重
---------------------------------------
12 1.5 0.6
1.5 0.3

总计:3.0 0.9
-------------------------- ------
14 0.6 0.4

总计:0.6 0.4
--------------------- -----------
15 1.2 1.1

总计:1.2 1.1
---------------- ----------------
19 3.0 1.4
3.0 1.2

总计:6.0 2.6
----- ------------------------------
GRAND TOTAL = 6.3 5



请注意:旧重量值具有重复性,当单一价值获得总值时,重量减少

<因为产品编号为12,因此19次出现两次,因此会显示一次其resp值。

请帮助我,如果我在HTML tr td格式中出错,请相应ct我,它不需要在同一个表tr,td ..我只想我的结果以上述格式

解决方案

试试这

  $ oldsubtotal = 0; 
$ newsubtotal = 0;
$ olsgrandtotal = 0;
$ newgrandtotal = 0;
while($ row = mysql_fetch_array($ query)){

if(isset($ prev)&&($ prev!= $ row ['pid'])){
echo< tr>< td style ='text-align:right'> Total< / td>< td> ;. $ oldsubtotal。< / td>< td>。 $ newsubtotal。 < / TD>< / TR> 中
echo< tr>< td colspan =3> -----< / td>< / tr>;
$ oldsubtotal = 0;
$ newsubtotal = 0;
}
echo< tr>;
echo< td>。$ row ['pid']。< / td>;
echo< td>。$ row ['owgh']。< / td>;
echo< td>。$ row ['nwgh']。< / td>;
回声< / tr>;
$ oldsubtotal + = $ row ['owgh'];
$ newsubtotal + = $ row ['nwgh'];
$ olsgrandtotal + = $ row ['owgh'];
$ newgrandtotal + = $ row ['nwgh'];
$ prev = $ row ['pid'];
}
echo< tr>< td style ='text-align:right'> Grand Total< / td>< td>。$ olsgrandtotal。< / td> < TD>中$ newgrandtotal。 < / TD> 中


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

which 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 : 30/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

PLEASE NOTE : OLD WEIGHT VALUE HAS REPEAT WEIGHT , HENCE WHILE GRAND TOTAL ITS SINGLE VALUE IS TAKEN

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            5

PLEASE NOTE : OLD WEIGHT VALUE HAS REPEAT WEIGHT , HENCE WHILE GRAND TOTAL ITS SINGLE VALUE IS TAKEN

as product id 12, 19 are coming twice hence shown once with its resp value

Please 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

解决方案

Try this

   $oldsubtotal = 0;
   $newsubtotal = 0;
   $olsgrandtotal = 0;
   $newgrandtotal = 0;
    while($row = mysql_fetch_array($query )){

        if(isset($prev) && ($prev != $row['pid'])){
            echo "<tr><td style='text-align: right'>Total</td><td>".$oldsubtotal."</td><td>".$newsubtotal."</td></tr>"
            echo "<tr><td colspan="3">-----</td></tr>";
            $oldsubtotal = 0;
            $newsubtotal = 0;
        }
        echo "<tr>";            
        echo "<td>.$row['pid'].</td>";
        echo "<td>.$row['owgh'].</td>";
        echo "<td>.$row['nwgh'].</td>";
        echo "</tr>";
        $oldsubtotal += $row['owgh'];
        $newsubtotal += $row['nwgh'];
        $olsgrandtotal += $row['owgh'];
        $newgrandtotal += $row['nwgh'];
        $prev = $row['pid'];
    }
    echo "<tr><td style='text-align: right'>Grand Total</td><td>".$olsgrandtotal."</td><td>".$newgrandtotal."</td>"

这篇关于如何在while循环中获取记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆