PHP array Processing
$sql = "Select B.lotno as Lotno,count (B.lotno) as Count from ' kw_manage ' as a inner joins ' vp_barcode_2d_y ' as B on A.packa Geno = 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 ']). '
';
}
Get results:
3206 * 12
3207 * 63
3218 * 180
Can you do the following results? When count corresponds to the sum of 100, the display
3206 * 12
3207 * 63
3218 * 25
Data other than sum 100 is displayed
3218 * 155
Share to:
------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 computed data into an array, and then output from the array