You are here:  » Multiple records with same variable name part 2


Multiple records with same variable name part 2

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

Hi David,

You were a great help to me a few months ago and I could use your help again. The code below successfully prints out the sections & count of the XML returned from the URL thanks to your code. What I need to do and I'm struggling on how to do it is to be able to print out each section in a different order. For example, I'd like to have the "price" section printed before the "location" section, etc. As it is now, the sections/counts print out in the order in which they are received in the XML feed.

I tried using arrays which I think is the correct way but I had no luck. Can you suggest a way of doing this?

<php
require("MagicParser.php");
$url = "{link saved}";
MagicParser_parse("$url","myRecordHandlerRefine","xml|FEED/ENTRY/");
  function myRecordHandlerRefine($record) {
  $i = 0;
  echo "<p>";
  $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/>";
   // I tried putting the value and count here in an array but could not print out the array out of the function
     $i++; // increment $i for next value
  }
while ($i < 10); # limits the number per section
}
?>

Thanks for any help you can provide!
Tim

Submitted by support on Wed, 2009-10-21 08:53

Hi Tim,

I've had a look at the XML and can see a neat way to do this. The trick will be to load each record into a global array during the parse, using an associative array keyed by the TITLE field, and then after the parse hand off the records individually to your original record handler function in the order required. For example;

<?php
require("MagicParser.php");
$url "{link saved}";
$records = array();
// parse feed using a new record handler functions to load the global $records array
MagicParser_parse("$url","myRecordHandler","xml|FEED/ENTRY/");
function 
myRecordHandler($record) {
  global 
$records;
  
$records[$record["TITLE"]] = $record;
}
// now display records with the original record handler function in the order required
myRecordHandlerRefine($records["SAMPLE - Price"]);
myRecordHandlerRefine($records["SAMPLE - Location"]);
// etc. etc.
function myRecordHandlerRefine($record) {
  
// your original record handler code from above
}
?>

Hope this helps!
Cheers,
David.

Submitted by timjeppesen on Wed, 2009-10-21 22:50

Hi David,

Thank you sooo much! That is perfect! It's exactly what I needed. I always forget about using "global" and wonder why my variables don't work outside a function. :-( It never occurred to me to do it the way you suggested. I was attempting to do it via arrays but like I said, forgot about globals.

Thanks again for your help.

Tim