You are here:  » Multiple records with same variable name


Multiple records with same variable name

Submitted by timjeppesen on Wed, 2009-06-10 00:21 in

Hi,

First, great script and worth every penny!

I've been trying to print out variable that has the same name but is appended with the @ to distinguish it. I'm trying the the loop method but my PHP skills are not the best :-)

The specific variables I want are "ENTRY/V:ATTRIBUTE/V:VALUE" and "ENTRY/V:ATTRIBUTE/V:VALUE-COUNT" and the same variables with @1, @2 and so on.

What I want is to print to the screen for example:

Location:
Sacramento, CA (24)
Folsom, CA (18)
Roseville, CA (11)
etc...

Trim:
Limited Edition (26)
Sport (15)
Unknown (29)
etc...

Here is the truncated XML I'm working with:

FEED
ENTRY
ENTRY/ID http://data.example.com/attributes/location/-/cars
ENTRY/TITLE SAMPLE - Location
ENTRY/TITLE-TYPE text
ENTRY/UPDATED 2009-06-08T21:55:35-07:00
ENTRY/AUTHOR
ENTRY/AUTHOR/NAME www.example.com
ENTRY/V:ATTRIBUTE
ENTRY/V:ATTRIBUTE-ID location
ENTRY/V:ATTRIBUTE-TYPE text
ENTRY/V:ATTRIBUTE/V:VALUE Sacramento, CA
ENTRY/V:ATTRIBUTE/V:VALUE-ID Sacramento--CA
ENTRY/V:ATTRIBUTE/V:VALUE-COUNT 24
ENTRY/V:ATTRIBUTE/V:VALUE@1 Folsom, CA
ENTRY/V:ATTRIBUTE/V:VALUE@1-ID Folsom--CA
ENTRY/V:ATTRIBUTE/V:VALUE@1-COUNT 18
ENTRY/V:ATTRIBUTE/V:VALUE@2 Roseville, CA
ENTRY/V:ATTRIBUTE/V:VALUE@2-ID Roseville--CA
ENTRY/V:ATTRIBUTE/V:VALUE@2-COUNT 11
ENTRY/V:ATTRIBUTE/V:VALUE@3 Placerville, CA
ENTRY/V:ATTRIBUTE/V:VALUE@3-ID Placerville--CA
ENTRY/V:ATTRIBUTE/V:VALUE@3-COUNT 9
ENTRY/V:ATTRIBUTE/V:VALUE@4 Auburn, CA
ENTRY/V:ATTRIBUTE/V:VALUE@4-ID Auburn--CA
ENTRY/V:ATTRIBUTE/V:VALUE@4-COUNT 4
ENTRY/V:ATTRIBUTE/V:VALUE@5 Cameron Park, CA
ENTRY/V:ATTRIBUTE/V:VALUE@5-ID Cameron-Park--CA
ENTRY/V:ATTRIBUTE/V:VALUE@5-COUNT 1
ENTRY/V:ATTRIBUTE/V:VALUE@6 Antelope, CA
ENTRY/V:ATTRIBUTE/V:VALUE@6-ID Antelope--CA
ENTRY/V:ATTRIBUTE/V:VALUE@6-COUNT 1
ENTRY/V:ATTRIBUTE/V:VALUE@7 Citrus Heights, CA
ENTRY/V:ATTRIBUTE/V:VALUE@7-ID Citrus-Heights--CA
ENTRY/V:ATTRIBUTE/V:VALUE@7-COUNT 1
ENTRY/V:ATTRIBUTE/V:VALUE@8 Elverta, CA
ENTRY/V:ATTRIBUTE/V:VALUE@8-ID Elverta--CA
ENTRY/V:ATTRIBUTE/V:VALUE@8-COUNT 1

ENTRY@1
ENTRY/ID@1 http://data.example.com/attributes/trim/-/cars
ENTRY/TITLE@1 SAMPLE - Trim
ENTRY/TITLE@1-TYPE text
ENTRY/UPDATED@1 2009-06-08T21:55:34-07:00
ENTRY/AUTHOR@1
ENTRY/AUTHOR/NAME@1 www.example.com
ENTRY/V:ATTRIBUTE@1
ENTRY/V:ATTRIBUTE@1-ID trim
ENTRY/V:ATTRIBUTE@1-TYPE text
ENTRY/V:ATTRIBUTE/V:VALUE@9 Limited Edition
ENTRY/V:ATTRIBUTE/V:VALUE@9-ID Limited+Edition
ENTRY/V:ATTRIBUTE/V:VALUE@9-COUNT 26
ENTRY/V:ATTRIBUTE/V:VALUE@10 Sport
ENTRY/V:ATTRIBUTE/V:VALUE@10-ID Sport
ENTRY/V:ATTRIBUTE/V:VALUE@10-COUNT 15
ENTRY/V:ATTRIBUTE/V:VALUE@11 unknown
ENTRY/V:ATTRIBUTE/V:VALUE@11-ID unknown
ENTRY/V:ATTRIBUTE/V:VALUE@11-COUNT 29

and so on..

Thanks for any help you can give!
Tim

Submitted by support on Wed, 2009-06-10 07:43

Hi Tim,

Firstly, it looks like it would be better to parse this feed at a deeper level, so that each ENTRY is handed to your myRecordHandler function independently, rather than just receiving the entire XML document in a single array - as this means that subsequent records continue to need to use the @1, @2 etc. distinction. In other words, you would use the Format String in your call to MagicParser_parse() as follows:

MagicParser_parse("filename.xml","myRecordHandler","xml|FEED/ENTRY/");

With that in place, you can access an handle each V:VALUE and V:VALUE-COUNT as follows (within your myRecordHandler function):

$i = 0;
do {
  if ($i) $postfix = "@".$i;
  if (!isset($record["V:VALUE".$postfix])) break;
  $value = $record["V:VALUE".$postfix];
  $count = $record["V:VALUE".$postfix."-COUNT"];
  // use $value and $count here!
  //
  //
  // increment $i for next value
  $i++;
}

Hope this helps!
Cheers,
David.

Submitted by timjeppesen on Wed, 2009-06-10 22:13

Hi David,

Thanks for the quick reply.

Unless I put in a while for the do loop, the code above doesn't work.

It is getting the right number of attributes but not increasing the counter.

Anyway, here is my code that is not working correctly:

  $i = 0;
do {
   if ($i) $postfix = "@".$i;
echo "<br/>$i " .$postfix. "<br/>"; // just to see if it prints out the correct @number
   if (!isset($record["V:VALUE".$postfix])) break;
  $value = $record["V:VALUE".$postfix];
   $count = $record["V:VALUE".$postfix."-COUNT"];
     echo "city=" . $value . " count= " . $count;
  // increment $i for next value
    $i++;
   }
while ($i < 15); // I think 12 or 13 is the max number of attributes

I must be missing something :-)

Thanks!
Tim

Submitted by timjeppesen on Wed, 2009-06-10 22:54

Hi David,

I believe I figured it out, here is the code I used, I had the record attribute wrong.

  $i = 0;
  echo "<br/>";
  $title = $record["V:ATTRIBUTE-ID"];
  echo $title . "<br/>";
do {
   if ($i) $postfix = "@".$i;
// echo "<br/>$i " .$postfix. "<br/>"; // just to see if it prints out the correct @number
   if (!isset($record["V:ATTRIBUTE/V:VALUE".$postfix])) break;
  $value = $record["V:ATTRIBUTE/V:VALUE".$postfix];
   $count = $record["V:ATTRIBUTE/V:VALUE".$postfix."-COUNT"];
     echo $value . " " . $count . "<br/>";
  // increment $i for next value
    $i++;
   }
while ($i < 15);

It's working like a charm now! Thank you very much for your help.

Tim