如何在while循环中获取记录

时间:2015-03-23 11:22:55

标签: php html mysql loops while-loop

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

结果我想要如下:

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

但我得到的结果如下

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

我不想要,我想要如下

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

更新:2015年3月30日

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

的总和
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

请注意:旧的重量值有重复的重量,而且在重量已达到单一价值的情况下仍然存在

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

请注意:旧的重量值有重复的重量,而且在重量已达到单一价值的情况下仍然存在

因产品ID 12,19两次,因此以resp值显示一次

请帮助我,如果我错误的HTML tr td格式请纠正我,没有必要进入同一个表tr,td ..我只想要我的结果以上格式

3 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:1)

while($row = mysql_fetch_array($query )){ echo "<td>.$row['pid'].</td>"; echo "<td>.$row['owgh'].</td>"; echo "<td>.$row['nwgh'].</td>"; if (isset($before) and $before!=$row['pid']) { echo "<td>-----</td>"; } $before= $row['pid']; }

答案 2 :(得分:1)

最好的方法是在单个查询中执行此操作,以避免加载多个查询的时间并处理php端的数据。 所以你的查询必须看起来像这样:

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

通过pid排序结果将帮助您管理php端的数据,如下所示:

$last_pid = null;
echo '<table class="myTable" border="0">'."\n";
while($row = mysql_fetch_array($query )) {
    $class = ($last_pid == $row['pid']) ? ' class="same"' : ''; 
    echo '    <tr'.$class.'>'."\n"
        .'        <td>'.$row["pid"].'</td>'."\n"
        .'        <td>'.$row["owgh"].'</td>'."\n"
        .'        <td>'.$row["owgh"].'</td>'."\n"
        .'    </tr>."\n";
    $last_pid = $row["pid"];
}
echo '</table>'."\n";

这个类的样式将是:

table.myTable { border:0px;}
table.myTable tr { border-bottom:1px solid #000; }
table.myTable tr.same { border-bottom:1px solid #fff; }