hey all.i'm newbie at this problem.i have this data in table result:
item range_code class
red 123x0001-123x0500 A
blue 123x0021-123x0100 //if null read zero
green 123x0001-123x0300 b
i want the result like:
item qty S A B C
red 500 0 1 0 0
blue 80 0 0 0 0
green 300 0 0 1 0
i have tried this code but still not work:
$sql= 'SELECT item, range_code as qty, class FROM result GROUP BY item, qty';
$result=mysql_query($sql);
while ($row=mysql_fetch_assoc($result)){
preg_match_all('/\d+(?=-|$)/g',$row['qty'],$matches);
echo intval($matches[0][1])-intval($matches[0][0])+1;
开发者_如何学编程}
i still confuse for this problem. please help..
Not sure if this is doing what you want... if not, clarify if there are any edge cases that don't match, but based on your examples this should work.
<?php
$code = "123x0001-123x0500";
preg_match("/^\d+x(\d+)-\d+x(\d+)$/", $code, $matches);
echo intval($matches[2]) - intval($matches[1]) + 1;
?>
OUTPUT:
500
.
<?php
$codes = array("123x0001-123x0500", "123x0021-123x0100", "123x0001-123x0300");
function getDiff($range) {
preg_match("/^\d+x(\d+)-\d+x(\d+)$/", $range, $matches);
return intval($matches[2]) - intval($matches[1]) + 1;
}
foreach ($codes as $code) {
echo getDiff($code) . "\n";
}
?>
OUTPUT
500
80
300
Not sure how the S, A, B, C values are supposed to be computed. Perhaps you can elaborate on that.
精彩评论