简体   繁体   中英

MySql and PHP - Fetch next row in a while loop

I have a MySQL query which selects scan details from the database. Those details are placed in a report. It was requested to have the reports displaying in two columns (almost two tables) on one page, to preserve the space that it takes.

What I am currently doing is displaying the table header:

 $report .= ">
                    <table cellspacing='0' class='at_report'>
                        <thead>
                            <tr><th colspan='8'>Area ".$row['area_name']."</th></tr>
                        </thead>
                        <tr>
                            <td class='simple_header' style='width:7%;'><div class='nosplit'>No.</div></td>
                            <td class='simple_header' style='width:8%;'><div class='nosplit'>Barcode</div></td>
                            <td class='simple_header' style='width:40%;'><div class='nosplit'>Product</div></td>
                            <td class='simple_header' style='width:5%;'><div class='nosplit'>Qty</div></td>
                            <td class='simple_header' style='width:7%;'><div class='nosplit'>Cost Value</div></td>
                            <td class='simple_header' style='width:16%;'><div class='nosplit'>Created By</div></td>
                            <td class='simple_header' style='width:10%;'><div class='nosplit'>Time</div></td>
                            <td class='simple_header' style='width:7%;'><div class='nosplit'>No.</div></td>
                            <td class='simple_header' style='width:8%;'><div class='nosplit'>Barcode</div></td>
                            <td class='simple_header' style='width:40%;'><div class='nosplit'>Product</div></td>
                            <td class='simple_header' style='width:5%;'><div class='nosplit'>Qty</div></td>
                            <td class='simple_header' style='width:7%;'><div class='nosplit'>Cost Value</div></td>
                            <td class='simple_header' style='width:16%;'><div class='nosplit'>Created By</div></td>
                            <td class='simple_header' style='width:10%;'><div class='nosplit'>Time</div></td>
                        </tr>
                         ";

Then I fetch the results from the database:

 while ($srow = $scan_res->fetch(PDO::FETCH_ASSOC))

Then I display the results in the table:

 $report .= "<tr ".$row_style.">
                                <td><div class='nosplit'>".$n."</div></td>
                                <td><div class='nosplit'>".$srow['barcode']."</div></td>
                                <td><div class='nosplit'>".$product_nd_short."</div></td>
                                <td style='text-align:center;'><div class='nosplit'>".$rounded_qty."</div></td>
                                <td style='text-align:center;'><div class='nosplit'>".$cur_symbol.$cn.$mcost_estimated."</div></td>
                                <td style='text-align:center;'><div class='nosplit'>".((strlen($srow['stocktake_staff_name'])>16)? substr($srow['stocktake_staff_name'], 0, 16)."..." : $srow['stocktake_staff_name'])."</div></td>
                                <td style='text-align:center;'><div class='nosplit'>".$dt->format('H:i:s')."</div></td></tr>";

My question is, how do I go to the next set of results in the while loop, so that I can add another column with set of results in the table? I tried this:

$report .= "<tr ".$row_style.">
                                <td><div class='nosplit'>".$n."</div></td>
                                <td><div class='nosplit'>".$srow['barcode']."</div></td>
                                <td><div class='nosplit'>".$product_nd_short."</div></td>
                                <td style='text-align:center;'><div class='nosplit'>".$rounded_qty."</div></td>
                                <td style='text-align:center;'><div class='nosplit'>".$cur_symbol.$cn.$mcost_estimated."</div></td>
                                <td style='text-align:center;'><div class='nosplit'>".((strlen($srow['stocktake_staff_name'])>16)? substr($srow['stocktake_staff_name'], 0, 16)."..." : $srow['stocktake_staff_name'])."</div></td>
                                <td style='text-align:center;'><div class='nosplit'>".$dt->format('H:i:s')."</div></td>";       
                $row_counter++;     
                $n++;
                   $report .= "
                                <td><div class='nosplit'>".$n."</div> </td>
                                <td><div class='nosplit'>".$srow['barcode']."</div></td>
                                <td><div class='nosplit'>".$product_nd_short."</div></td>
                                <td style='text-align:center;'><div class='nosplit'>".$rounded_qty."</div></td>
                                <td style='text-align:center;'><div class='nosplit'>".$cur_symbol.$cn.$mcost_estimated."</div></td>
                                <td style='text-align:center;'><div class='nosplit'>".((strlen($srow['stocktake_staff_name'])>16)? substr($srow['stocktake_staff_name'], 0, 16)."..." : $srow['stocktake_staff_name'])."</div></td>
                                <td style='text-align:center;'><div class='nosplit'>".$dt->format('H:i:s')."</div></td> 
                            </tr>";

But all it does is duplicating the result from the first set.

Thanks!

May be you need something like this.

You want to place the 2 result in a same row.

$n = 1;
while($srow = $scan_res->fetch(PDO::FETCH_ASSOC)){
    if($n % 2 != 0)
        $report .= "<tr ".$row_style.">";

    $report .= "<td><div class='nosplit'>".$n."</div></td>
              <td><div class='nosplit'>".$srow['barcode']."</div></td>
              <td><div class='nosplit'>".$product_nd_short."</div></td>
              <td style='text-align:center;'><div class='nosplit'>".$rounded_qty."</div></td>
              <td style='text-align:center;'><div class='nosplit'>".$cur_symbol.$cn.$mcost_estimated."</div></td>
              <td style='text-align:center;'><div class='nosplit'>".((strlen($srow['stocktake_staff_name'])>16)? substr($srow['stocktake_staff_name'], 0, 16)."..." : $srow['stocktake_staff_name'])."</div></td>
              <td style='text-align:center;'><div class='nosplit'>".$dt->format('H:i:s')."</div></td>";
    if($n % 2 == 0)
        $report .= "</tr>";
    $n++;
}

Ehm,

its a bit confusing your question, but i think i know what you mean....

while ($srow = $scan_res->fetch(PDO::FETCH_ASSOC))
{
    $n++;
    $report .= "<tr><td><div class='nosplit'>".$n."</div></td>
                <td><div class='nosplit'>".$srow['barcode']."</div></td>
                <td><div class='nosplit'>".$product_nd_short."</div></td>
                <td style='text-align:center;'><div class='nosplit'>".$rounded_qty."</div></td>
                <td style='text-align:center;'><div class='nosplit'>".$cur_symbol.$cn.$mcost_estimated."</div></td>
                <td style='text-align:center;'><div class='nosplit'>".((strlen($srow['stocktake_staff_name'])>16)? substr($srow['stocktake_staff_name'], 0, 16)."..." : $srow['stocktake_staff_name'])."</div></td>
                <td style='text-align:center;'><div class='nosplit'>".$dt->format('H:i:s')."</div></td></tr>";
}

i haven't tried running it ... but it should do what you want.

  • make sure you have the statements you want to repeat inside the curly braces {} after your while statement
  • make sure to start row with and end it with
  • you only use barcode and stock_staff_name variables from your data rows ... other values you seem to have pre set ... so those will always be the same ($rounded_qty, $dt, $product_nd_short .... so if you would like this to be from your row ... then read them from your row in your while statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM