Php array processing solution

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.