Php array processing $ SQL & nbsp ;=& nbsp; "SELECT & nbsp; B. lotno & nbsp; as & nbsp; lotno, count (B. lotno) & nbsp; as & nbsp; count & nbsp; FROM & nbsp; 'kw _ mana php array processing
$sql = "SELECT b.lotno as lotno,count(b.lotno) as count FROM `kw_manage` as a inner join `vp_barcode_2d_y` as b on a.packageno = b.packageno where a.cust_no = '".$newstr."' group by lotno order by lotno asc";
$result = mysql_query($sql);
while($var = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo $var['lotno'].' * '.($var['count']).'
';
}
Expected result:
3206*12
3207*63
3218*180
Can the following processing results be performed? When the sum of count values is 100
3206*12
3207*63
3218*25
Display data out of total 100
3218*155 share:
------ Solution --------------------
$num = 0;
while($var = mysql_fetch_array($result,MYSQL_ASSOC))
{
if($num + $var['count'] <= 100) {
echo $var['lotno'].' * '.($var['count']).'
';
$num = $num + $var['count'];
}else {
echo $var['lotno'].' * '.(100 - $num).'
';
$num = $num + $var['count'] - 100;
echo $var['lotno'].' * '. $num .'
';
}
}
------ Solution --------------------
You first cache the calculated data to the array and then output the data from the array.