Php 如何在while循环中获取记录
producttable:Php 如何在while循环中获取记录,php,html,mysql,loops,while-loop,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
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\u查询($sql)代码>
这是我不想要的,我想要的如下:
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..我只想要上面格式的结果
$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>"
$oldsubtotal=0;
$newsubtotal=0;
$olsgrandtotal=0;
$newgrandtotal=0;
while($row=mysql\u fetch\u array($query)){
如果(isset($prev)&($prev!=$row['pid'])){
回显“总计”。$oldsubtotal。“$newsubtotal.”
回声“----”;
$oldsubtotal=0;
$newsubtotal=0;
}
回声“;
回显“$row['pid']”;
回声“$row['owgh']”;
回声“$row['nwgh']”;
回声“;
$oldsubtotal+=$row['owgh'];
$newsubtotal+=$row['nwgh'];
$olsgrandtotal+=$row['owgh'];
$newgrandtotal+=$row['nwgh'];
$prev=$row['pid'];
}
回显“总计”。$olsgrandtotal.“$newgrandtotal.”
试试这个
$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>"
$oldsubtotal=0;
$newsubtotal=0;
$olsgrandtotal=0;
$newgrandtotal=0;
while($row=mysql\u fetch\u array($query)){
如果(isset($prev)&($prev!=$row['pid'])){
回显“总计”。$oldsubtotal。“$newsubtotal.”
回声“----”;
$oldsubtotal=0;
$newsubtotal=0;
}
回声“;
回显“$row['pid']”;
回声“$row['owgh']”;
回声“$row['nwgh']”;
回声“;
$oldsubtotal+=$row['owgh'];
$newsubtotal+=$row['nwgh'];
$olsgrandtotal+=$row['owgh'];
$newgrandtotal+=$row['nwgh'];
$prev=$row['pid'];
}
回显“总计”。$olsgrandtotal.“$newgrandtotal.”
试试这个
$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>"
$oldsubtotal=0;
$newsubtotal=0;
$olsgrandtotal=0;
$newgrandtotal=0;
while($row=mysql\u fetch\u array($query)){
如果(isset($prev)&($prev!=$row['pid'])){
回显“总计”。$oldsubtotal。“$newsubtotal.”
回声“----”;
$oldsubtotal=0;
$newsubtotal=0;
}
回声“;
回显“$row['pid']”;
回声“$row['owgh']”;
回声“$row['nwgh']”;
回声“;
$oldsubtotal+=$row['owgh'];
$newsubtotal+=$row['nwgh'];
$olsgrandtotal+=$row['owgh'];
$newgrandtotal+=$row['nwgh'];
$prev=$row['pid'];
}
回显“总计”。$olsgrandtotal.“$newgrandtotal.”
试试这个
$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>"
$oldsubtotal=0;
$newsubtotal=0;
$olsgrandtotal=0;
$newgrandtotal=0;
while($row=mysql\u fetch\u array($query)){
如果(isset($prev)&($prev!=$row['pid'])){
回显“总计”。$oldsubtotal。“$newsubtotal.”
回声“----”;
$oldsubtotal=0;
$newsubtotal=0;
}
回声“;
回显“$row['pid']”;
回声“$row['owgh']”;
回声“$row['nwgh']”;
回声“;
$oldsubtotal+=$row['owgh'];
$newsubtotal+=$row['nwgh'];
$olsgrandtotal+=$row['owgh'];
$newgrandtotal+=$row['nwgh'];
$prev=$row['pid'];
}
回显“总计”。$olsgrandtotal.“$newgrandtotal.”
while($row=mysql\u fetch\u array($query)){
回显“$row['pid']”;
回声“$row['owgh']”;
回声“$row['nwgh']”;
if(isset($before)和$before!=$row['pid'])){
回声“----”;
}
$before=$row['pid'];
}while($row=mysql\u fetch\u数组($query)){
回显“$row['pid']”;
回声“$row['owgh']”;
回声“$row['nwgh']”;
if(isset($before)和$before!=$row['pid'])){
回声“----”;
}
$before=$row['pid'];
}while($row=mysql\u fetch\u数组($query)){
回显“$row['pid']”;
回声“$row['owgh']”;
回声“$row['nwgh']”;
if(isset($before)和$before!=$row['pid'])){
回声“----”;
}
$before=$row['pid'];
}while($row=mysql\u fetch\u数组($query)){
回显“$row['pid']”;
回声“$row['owgh']”;
回声“$row['nwgh']”;
if(isset($before)和$before!=$row['pid'])){
回声“----”;
}
$before=$row['pid'];
}
最好的方法是在一个查询中完成,以避免多个查询的加载时间,并在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";
最好的方法是在单个查询中完成,以避免多个查询的加载时间,并避免在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";
最好的方法是在单个查询中完成,以避免多个查询的加载时间,并避免在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";
最好的方法是在单个查询中完成,以避免多个查询的加载时间,并避免在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";
这不是唯一的条件