You are here:  » Loop Through Duplicate Field Names


Loop Through Duplicate Field Names

Submitted by jag959 on Sat, 2009-05-23 19:36 in

Hello David,

I am trying to present several different product reviews for the same 1 product. I would also like specific product details repeated on the same line for each review.

{link saved}

format string: xml|ITEMSEARCHRESPONSE/ITEMS/

{link saved} You'll see there is only one product result when there should be more, and none of the specifics about the product are present because I am not parsing properly somehow.

For example, for books with multiple reviews, the product information would be at the format string levels:

-----------------------------------

Title:
ITEM/ITEMATTRIBUTES/TITLE

1st review:
ITEM/CUSTOMERREVIEWS/REVIEW/CONTENT

2nd review:
ITEM/CUSTOMERREVIEWS/REVIEW/CONTENT@1

Then there are more titles...

Title2:
ITEM/ITEMATTRIBUTES/TITLE@2

1st Review:
ITEM/CUSTOMERREVIEWS/REVIEW/CONTENT@5

2nd review:
ITEM/CUSTOMERREVIEWS/REVIEW/CONTENT@6

-----------------------------------

Here is a sample of the PHP/HTML (I am currently using) for displaying results...

<?php
 
// Output on screen
  
print "<div id='main'>\n";
  print 
"<div id='content-main'>\n";
  print 
"<div class='section'>\n";
  print 
"<table class='posts-table'>\n";
  print 
"<tbody>\n";
  print 
"<tr>\n";
  print 
"<td class='date'>\n";
  print 
"<p><a href='".$item["ITEM/DETAILPAGEURL"]."' target='blank'><img src='".$item["ITEM/MEDIUMIMAGE/URL"]."' width='".$item["ITEM/MEDIUMIMAGE/WIDTH"].$item["ITEM/MEDIUMIMAGE/WIDTH-UNITS"]."' height='".$item["ITEM/MEDIUMIMAGE/HEIGHT"].$item["ITEM/MEDIUMIMAGE/HEIGHT-UNITS"]."' /></a></p>\n";
  print 
"<p><strong>Author:</strong> ".$item["ITEM/ITEMATTRIBUTES/AUTHOR"]."</p>\n";
  print 
"<p><strong>Publication Date:</strong> ".$item["ITEM/ITEMATTRIBUTES/PUBLICATIONDATE"]."</p>\n";
  print 
"<p><strong>Publisher:</strong> ".$item["ITEM/ITEMATTRIBUTES/PUBLISHER"]."</p>\n";
  print 
"<p><strong># Pages:</strong> ".$item["ITEM/ITEMATTRIBUTES/NUMBEROFPAGES"]."</p>\n";
  print 
"<p><strong>EAN #:</strong> ".$item["ITEM/ITEMATTRIBUTES/EAN"]."</p>\n";
  print 
"<p><strong>ISBN#:</strong> ".$item["ITEM/ITEMATTRIBUTES/ISBN"]."</p>\n";
  print 
"</td>\n";
  print 
"<td class='title'>\n";
  print 
"<p><a href='".$item["ITEM/DETAILPAGEURL"]."' target='blank'>".$item["ITEM/ITEMATTRIBUTES/TITLE"]."</a></p>\n";
  print 
"</td>\n";
  print 
"<td class='summary'>\n";
  print 
"<p><strong>Review:</strong><br />".$item["ITEM/CUSTOMERREVIEWS/REVIEW/CONTENT"]."</p><br />\n";
  print 
"</td>\n";
  print 
"<td class='info'>\n";
  print 
"<p><strong>Reviewer:</strong><br />".$item["ITEM/CUSTOMERREVIEWS/REVIEW/REVIEWER/NAME"]."</p><br />\n";
  print 
"<p><strong>Review Date:</strong><br />".$item["ITEM/CUSTOMERREVIEWS/REVIEW/DATE"]."</p><br />\n";
  print 
"<p><a href='".$item["ITEM/DETAILPAGEURL"]."' target='blank'>Click here for more Information</a></p>\n";
  print 
"</td>\n";
  print 
"</tr>\n";
  print 
"</tbody>\n";
  print 
"</table>\n";
  print 
"</div>\n";
  print 
"</div>\n";
  print 
"</div>\n";
?>

-----------------------------------

Thanks!

- Jason Gregory

Submitted by support on Sat, 2009-05-23 20:18

Hello Jason,

As the primary record you are interested in is each item, I would use a deeper Format String - in other words:

xml|ITEMSEARCHRESPONSE/ITEMS/ITEM/

This would give you easy access to the single level fields such as the title, using:

$item["ITEMATTRIBUTES/TITLE"]

For the repeating items such as reviews, I would recommend the loop method in this case,
whereby you increment a counter variable and use it to construct the postfix that Magic
Parser adds to duplicate fields names, i.e. @1, @2, etc. Within each itteration, you
check whether a field exists with that postfix, and if so, process the fields with
that postfix as required.

As an example, have a look at the following script which uses your Amazon URL to display
the each title, and the review rating and reviewer of each review.

Click here to see this
script running on this server.

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($item)
  {
    print 
"<p>".$item["ITEMATTRIBUTES/TITLE"]."</p>";
    print 
"<ul>";
    
$i 0;
    
$p "";
    while(
1)
    {
      if (
$i$p "@".$i;
      if (!isset(
$item["CUSTOMERREVIEWS/REVIEW".$p])) break;
      print 
"<li>Rated: ".$item["CUSTOMERREVIEWS/REVIEW/RATING".$p]." by ".$item["CUSTOMERREVIEWS/REVIEW/REVIEWER/NAME".$p]."</li>";
      
$i++;
    }
    print 
"</ul>";
    print 
"<hr />";
  }
  
$url "{link saved}";
  
MagicParser_parse($url,"myRecordHandler","xml|ITEMSEARCHRESPONSE/ITEMS/ITEM/");
?>

Let me know if you're not sure how to apply the same technique to the other
types of repeating record...

Hope this helps!
Cheers,
David.

Submitted by jag959 on Sun, 2009-05-24 03:59

David,

As always, you're quick to respond and a tremendous help. It worked like a charm.

Thanks so very much!

- Jason