mysql - Switch Case Looping PHP -
i frustated, i've tried many method didn't find expexted result. have data in table database:
| partid | hasilproduksi |qtyproduksi| |----------------------|------------------|-----------| | blap-fg152-pf-kgx | repair | 1 | | awdx-fg002-hn-kgx | reject | 90 | | hmxx-fg022-eg-kgx | | 100 | | ecjx-fg018-av-mcm | | 111 |
and want create report result :
| | actual | | partid | ---------------------| | |good | repair | reject| |----------------------|-----|--------|-------| | blap-fg152-pf-kgx | 0 | 1 | 0 | | awdx-fg002-hn-kgx | 0 | 0 | 90 | | hmxx-fg022-eg-kgx | 100 | 0 | 0 | | ecjx-fg018-av-mcm | 111 | 0 | 0 |
and i'm using switch case method result not expected result. , code :
$sqlc = "select * $db_dthp idbukti='$id_bukti'"; $qc = mysqli_query($conn, $sqlc); $c = 0; $good=0; $reject=0; $repair=0; while ($rc = mysqli_fetch_assoc($qc)) { switch ($rc['hasilproduksi']) { case 'good': $good += (int) $rc['qtyproduksi']; $datagood += (int) $rc['qtyproduksi']; break; case 'reject': $reject += (int) $rc['qtyproduksi']; $datareject += (int) $rc['qtyproduksi']; break; case 'repair': $repair += (int) $rc['qtyproduksi']; $datarepair += (int) $rc['qtyproduksi']; break; default: break; } $c++; } $res['result'][$i]['data'][$b]['hp'][0]['good'] = number_format($good,0,'.','.'); $res['result'][$i]['data'][$b]['hp'][0]['reject'] = number_format($reject,0,'.','.'); $res['result'][$i]['data'][$b]['hp'][0]['repair'] = number_format($repair,0,'.','.');
can me?
if use other query, have data , don't need switch in php. case in query:
$sqlc="select partid, case when hasilproduksi='good' qtyproduksi else 0 end good, case when hasilproduksi='repair' qtyproduksi else 0 end repair, case when hasilproduksi='reject' qtyproduksi else 0 end reject $db_dthp idbukti='$id_bukti';"
Comments
Post a Comment