foreach nested loops (complete newb Q)
Posted: Tue Sep 13, 2011 2:28 am
Hi, I'm extremely new to this.
I've got an array of product codes with values of some numeric qty i.e.
prodCode: 6722 QTY: 1
prodCode: 6724 QTY: 1
This is array is obtained via POST from an order form (and it works, meaning I can echo the array).
I want to link the product codes to their descriptions so I can ech the order form back to the client.
The descriptions are in a text doc in the form: prodcode description
i.e. 6722 Hydrocolloid Blister Care
6724 Toe Spreader
This also seems to work, meaning I can create an array of lines of text from the document, which i can echo on screen.
pseudo code:
foreach order form_prodCode
foreach products_List_prodCode
if form_prodCode is the same as products_List_prodCode then
echo code, description and qty
break
(end if)
(end foreach loop)
(end foreach loop)
Unfortunately i can't get it to work, if i remove the break statement below strpos seems to find the code string just about everywhere. With the break it finds two occurences (as it should) but it displays the wrong products and the strpos doesn't make anysense to me at all.
I get the following output everytime for
$pos, $code, $desc, QTy:
0, 6722, BP1240 BP ANKLE STEP IN SMALL, QTY: 2
27, 6724, 6136 1.2.3 PREMIUM FIRST AID LARGE, QTY: 1
Actual code:
Apologies if it's really basic, I'm trying to teach myself PHP from the manual.
Steve
I've got an array of product codes with values of some numeric qty i.e.
prodCode: 6722 QTY: 1
prodCode: 6724 QTY: 1
This is array is obtained via POST from an order form (and it works, meaning I can echo the array).
I want to link the product codes to their descriptions so I can ech the order form back to the client.
The descriptions are in a text doc in the form: prodcode description
i.e. 6722 Hydrocolloid Blister Care
6724 Toe Spreader
This also seems to work, meaning I can create an array of lines of text from the document, which i can echo on screen.
pseudo code:
foreach order form_prodCode
foreach products_List_prodCode
if form_prodCode is the same as products_List_prodCode then
echo code, description and qty
break
(end if)
(end foreach loop)
(end foreach loop)
Unfortunately i can't get it to work, if i remove the break statement below strpos seems to find the code string just about everywhere. With the break it finds two occurences (as it should) but it displays the wrong products and the strpos doesn't make anysense to me at all.
I get the following output everytime for
$pos, $code, $desc, QTy:
0, 6722, BP1240 BP ANKLE STEP IN SMALL, QTY: 2
27, 6724, 6136 1.2.3 PREMIUM FIRST AID LARGE, QTY: 1
Actual code:
Code: Select all
$data = file_get_contents("prodDB.txt"); //read the file
$lineOfData = explode("\n", $data); //create array separate by new line
foreach($allCodes as $code => $num)
{
foreach($lineOfData as $desc)
{
$pos = strpos($desc, $code);
if ($pos === false)
{
//prod code not in desc, do nothing
}
else
{
echo "$pos, $code, $desc, QTY: $num <br />";
break;
}
}
}
?>
Apologies if it's really basic, I'm trying to teach myself PHP from the manual.
Steve