Skip to content Skip to sidebar Skip to footer

How To Get Record In 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

Solution 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']; }

Solution 2:

The best way is to do it in a single query to avoid loading time of multiple queries and the handle the data in the php side. So you query must look like this one:

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

Ordering your results by the pid will help you manage the data in the php side like this:

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

and the style for this class will be:

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

Solution 3:

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

Post a Comment for "How To Get Record In While Loop"