jonathan314
IS-IT--Management
- Jul 28, 2008
- 8
I got a program which should record each component of every kit in a DB and put it into a hash and then export into a file. For some reason it catch only the first component of its kit and then repeated for n times depending how many components its has.
The data looks like followings:
Kit Component LineKey Qty
221D0105B120K GW 000001 1
221D0105B120K S120OS7X5-AA 000002 0.5
221D0105B120K CR40B 000003 3.1
221D0105B120K HP40B 000004 2
221D0105B120K GG-A 000005 1
221D0105B120K NRG4OX-B 000006 1
221D0105B120K GBP 000007 0.8
221D0105B173K GW 000001 1
221D0105B173K S173OS7X5-AA 000002 0.6
221D0105B173K PLAT-OX-R 000003 0.9
221D0105B173K LABOR-RED 000004 4.5
221D0105B173K GNV 000005 6
221D0105B173K /C 000006 0
This is the code:
.........................................................
$SELECT = "SELECT IM_SalesKitDetail.SalesKitNo, \
IM_SalesKitDetail.ComponentItemCode, \
IM_SalesKitDetail.LineKey, \
IM_SalesKitDetail.QuantityPerAssembly \
FROM IM_SalesKitDetail";
if ($db->Sql($SELECT)) {
print "SQL failed.\n";
print "Error: " . $db->Error() . "\n";
} else {
while($db->FetchRow()) {
($kitid,$itemid,$lineKy,$qty) = $db->Data;
if (($kitid eq "") || ($itemid eq "")) {next;}
if ($FlagMap{$kitid} ne "KKK") {next;}
$cnt++;
$ItemMap{$kitid} .= $itemid . ',';
$QtyMap{$kitid} .= $qty . ',';
$LineKeyMap{$kitid} .= $lineKy . ',';
} #the while loop to walk through the data
print "Number of kit items: $cnt\n";
# Reorder the component for each kit
foreach $pk (sort keys %ItemMap) {
$items = $ItemMap{$pk};
@itemList = split ',', $items;
$qtys = $QtyMap{$pk};
@qtyList = split ',', $qtys;
$nLinekey = $LineKeyMap{$pk};
@nLinekeyList = split ',', $nLinekey;
$n = scalar(@itemList);
$cnt = 1;
while ($cnt <= $n) {
$found = 0;
for ($i = 0; $i <= $n; $i++) {
$KitMap{$pk} .= $itemList[$i] . "," . $qtyList[$i] . ",";
$cnt++;
$found = 1;
last;
}
if ($found == 0) {last;}
}
}
.......................................................
Please help to solve this issue.
Any suggestion is greatly appreciated.
The data looks like followings:
Kit Component LineKey Qty
221D0105B120K GW 000001 1
221D0105B120K S120OS7X5-AA 000002 0.5
221D0105B120K CR40B 000003 3.1
221D0105B120K HP40B 000004 2
221D0105B120K GG-A 000005 1
221D0105B120K NRG4OX-B 000006 1
221D0105B120K GBP 000007 0.8
221D0105B173K GW 000001 1
221D0105B173K S173OS7X5-AA 000002 0.6
221D0105B173K PLAT-OX-R 000003 0.9
221D0105B173K LABOR-RED 000004 4.5
221D0105B173K GNV 000005 6
221D0105B173K /C 000006 0
This is the code:
.........................................................
$SELECT = "SELECT IM_SalesKitDetail.SalesKitNo, \
IM_SalesKitDetail.ComponentItemCode, \
IM_SalesKitDetail.LineKey, \
IM_SalesKitDetail.QuantityPerAssembly \
FROM IM_SalesKitDetail";
if ($db->Sql($SELECT)) {
print "SQL failed.\n";
print "Error: " . $db->Error() . "\n";
} else {
while($db->FetchRow()) {
($kitid,$itemid,$lineKy,$qty) = $db->Data;
if (($kitid eq "") || ($itemid eq "")) {next;}
if ($FlagMap{$kitid} ne "KKK") {next;}
$cnt++;
$ItemMap{$kitid} .= $itemid . ',';
$QtyMap{$kitid} .= $qty . ',';
$LineKeyMap{$kitid} .= $lineKy . ',';
} #the while loop to walk through the data
print "Number of kit items: $cnt\n";
# Reorder the component for each kit
foreach $pk (sort keys %ItemMap) {
$items = $ItemMap{$pk};
@itemList = split ',', $items;
$qtys = $QtyMap{$pk};
@qtyList = split ',', $qtys;
$nLinekey = $LineKeyMap{$pk};
@nLinekeyList = split ',', $nLinekey;
$n = scalar(@itemList);
$cnt = 1;
while ($cnt <= $n) {
$found = 0;
for ($i = 0; $i <= $n; $i++) {
$KitMap{$pk} .= $itemList[$i] . "," . $qtyList[$i] . ",";
$cnt++;
$found = 1;
last;
}
if ($found == 0) {last;}
}
}
.......................................................
Please help to solve this issue.
Any suggestion is greatly appreciated.