You are here:  » Questions Re Comma Seperated Values


Questions Re Comma Seperated Values

Submitted by ROYW1000 on Mon, 2010-08-23 09:49 in

Hi

If I have a XML Feed that contains comma seperated values for example a address how can I map these to the database. For example: First Name, Surname, Address line 1 etc.

I also have another feed that contains product information and it has accessories. For example the fields are itemID, name, description, accessories and the accessories are comma seperated.

What I need to do is stick the itemID, name and description into the Item Table (To create the Product Record) and then for the associated I need to lookup the itemID for the accessory from the current database the Item Table and and then insert this itemID if it does not already exists from the looked up value into the associated table. But if the item is already in the assoicated table dont update it against that itemID dont update it.

For example itemID 1 is in the feed and it name is 'Test 10', its Description is 'Test Description' and its associated item are 'Test 12' and 'Test 13'.

Test 12 item number in the system for example might be itemID 12 and Test 13 might be itemID 13.

So into the associated Table it need to insert the following:

itemID associatedID

1 12
1 13

Thanks
Roy

Submitted by support on Mon, 2010-08-23 10:07

Hi Roy,

Let's say you've got a field in $record, parsed from your XML called ACCESSORIES, which contains a comma separated list of accessory values. You can extract that list into an array using explode(). You can then loop through each accessory using foreach(), to create your database table of accessories associated with the itemID (in $record["ITEMID"] I presume). For example;

  $itemId = $record["ITEMID"];
  // create item record with ID $itemID here...
  $accessories = explode(",",$record["ACCESSORIES"]);
  foreach($accessories as $accessory)
  {
    // create accessory record for $accessory, associated (foreign key) to $itemId
  }

The same should work for your address field, if you need to split out the individual fields, e.g.

  $AddressParts = explode(",",$record["ADDRESS"]);
  $FirstName = $AddressParts[0];
  $Surname= $AddressParts[1];
  etc.

Hope this helps!
Cheers,
David.