tags:

views:

50

answers:

1

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..

+1  A: 

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.

sberry2A
Looks like the A/B/C columns are just bitfields representing the contents of the 'Code' database table. If code=A, then A=1,B=0,C=0, etc... No idea what the S could be, other than (obviously from the given sample) a zero field
Marc B
@Marc B:you can see at my linked question.
klox