Php array processing

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


Reply to discussion (solution)

I don't understand what you want

$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 .'
'; }}



, The green part indicates the quantity. * The number of the Purple part corresponds to the previous part.
For example, if the first number is 120, the subsequent result is 3206*12 3207*63 3218*45.
Because 120 = 12 + 63 + 45.
The result of the second Purple Line is 3218*135. (180-45)

This is the result I want.

$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 .'
'; }}


If the number is set to 120, the result is as follows:
3206*12
3207*63
3218*45
3218*135
Can I record 3218*135 in the next while loop?
That is, the first loop is
3206*12
3207*63
3218*45
The next cycle is
3218*135

This is what I want to get.


Can it be like the example on the third floor?
The first line in Purple should get the result.
3206*12
3207*63
3218*45
The second line of Purple should get the result.
3218*135 (this result is recorded in the next while loop)
Instead of getting this result:
First line
3206*12
3207*63
3218*45
3218*135
Row 2
3206*12
3207*63
3218*60
3218*120

You first cache the calculated data to the array and then output the data from the array.

You first cache the calculated data to the array and then output the data from the array.
But the results are all output cyclically. how can we get them in the array? Solution.

While ($ var = mysql_fetch_array ($ result, MYSQL_ASSOC ))
{
$ Res [] = $ var ['lotno']. '*'. ($ var ['count']);
}

Isn't this in 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.