You are here:  » process childs on some records


process childs on some records

Submitted by pinkdigital on Thu, 2008-07-03 08:35 in

We have a data feed that we are processing for a client.

Some records are like these in the feed and we can process them correctly

<PRODUCT ITEM='1234' NAME='Bra Set'>
<STOCK>In Stock</STOCK>
</PRODUCT>

other items are like this...

<PRODUCT ITEM='7789' NAME='Bra Set 2'>
<STOCK Size="Small" >In Stock</STOCK>
<STOCK Size="Medium" >In Stock</STOCK>
<STOCK Size="Large" >In Stock</STOCK>
</PRODUCT>

How can we process this so we can use the product item number and the stock size to search the database to mark these are in or out of stock?

Submitted by support on Tue, 2008-07-08 09:00

Hi,

In this case, the first thing to do of course is to make sure that your database supports variants (sizes) of each product; so effectively they are different product records.

The way Magic Parser resolves duplicate field names is to append each additional field with @2, @3... etc., so in this particular case, the second example is going to result in the following field names being part of the $record array passed to your myRecordHandler function:

STOCK
STOCK-SIZE

STOCK@1
STOCK@1-SIZE

STOCK@2
STOCK@2-SIZE

Now, the best way to handle this is to loop through $record using a counter variable to check for multiple size records, for example:

$i = 0;
while(1) {
  if ($i) $postfix = "@".$i;
  if (!isset($record["STOCK".$postfix]) break;
  $size = $record["STOCK".$postfix."-SIZE"];
  $instock = $record["STOCK".$postfix];
  // at this point, use the common values in $record, i.e. $record["PRODUCT-ITEM"]
  // along with the $size and $instock values - the code will get here for each
  // different size/instock combination
}

Hope this helps!
Cheers,
David.

Submitted by pinkdigital on Tue, 2008-07-08 09:19

I have got it working using this code...

$i = 0;
while(1) {
if ($i) $postfix = "@".$i;
if (!isset($record["STOCK".$postfix])) break;
$size = $record["STOCK".$postfix."-SIZE"];
$instock = $record["STOCK".$postfix];
print $size .":" .$instock."<br />";
$i++;
}

However it isnt looping throught the stock sizes. It is only showing the last size.

So for the following XML it shows the product item, name and the Stock Size=Small. It isnt looping through stock to grab the other two sizes.

<PRODUCT ITEM='r645' NAME='Leather Belt'>
<STOCK Size="Large" >In Stock</STOCK>
<STOCK Size="Medium" >In Stock</STOCK>
<STOCK Size="Small" >In Stock</STOCK>
</PRODUCT>

Submitted by support on Tue, 2008-07-08 09:41

Hi,

The code looks OK - can you confirm that it is working against the same XML?

I have tried this code in an example script:

View Output

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    
$i 0;
    while(
1) {
      if (
$i$postfix "@".$i;
      if (!isset(
$record["STOCK".$postfix])) break;
      
$size $record["STOCK".$postfix."-SIZE"];
      
$instock $record["STOCK".$postfix];
      print 
$size .":" .$instock."<br />";
      
$i++;
    }
  }
  
$xml =
"<PRODUCTS><PRODUCT ITEM='7789' NAME='Bra Set 2'>
<STOCK Size='Small' >In Stock</STOCK>
<STOCK Size='Medium' >In Stock</STOCK>
<STOCK Size='Large' >In Stock</STOCK>
</PRODUCT></PRODUCTS>"
;
  
MagicParser_parse("string://".$xml,"myRecordHandler","xml|PRODUCTS/PRODUCT/");
?>

Cheers,
David.

Submitted by pinkdigital on Wed, 2008-07-09 17:38

for some reason the script above doesnt work at all with my version of Magic Parser. It fails with no error message.

Submitted by support on Wed, 2008-07-09 17:43

Hi,

Could you perhaps email me the test script you created, and a link to where it is running on your server...

Reply to your reg code or forum registration email is the easiest way to get me...

Cheers,
David.