You are here:  » Getting options

Support Forum



Getting options

Submitted by dougb on Tue, 2008-02-05 16:26 in

I want to return formatted options from this xml file (http://www.claddaghshop.com/text.xml) like so:

Code: SOL-2661M

OptionLists:

Choose your Birth Stone, January - Garnet, February - Amethyst, March - Aquamarine, April - Crystal,May - Emerald,June - Light Amethyst,July - Siam,August - Peridot,September - Sapphire,October - Rose,November - Topaz,December - Blue Zircon

Size, 4,5,6,7,8,9

Code:555-4136

OptionLists:
Ladies Size, 4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9

Gents Size, 9,9.5,10,10.5,11,11.5,12,12.5,13

Some products have more than two option lists some have none. I've only been able to gent the two option listed like so:

  require("MagicParser.php");
set_time_limit(0);
function myRecordHandler($record)
{
// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match("/SOL-2661M/i", $record["CODE"])) {
echo $record["CODE"]."<br>";
$i = 0;
while(1){
if ($i) $postfix1 = "@".$i;
if (!isset($record["OPTIONLISTS/OPTIONLIST".$postfix1])) break;
echo $record["OPTIONLISTS/OPTIONLIST".$postfix1."-NAME"]."<br>";
$i++;
};
  }
}
   MagicParser_parse("http://www.irishop.com/objinfo.xml","myRecordHandler","xml|STOREEXPORT/PRODUCTS/PRODUCT/");

Submitted by support on Tue, 2008-02-05 16:48

Hi Doug,

In this scenario where you have more than 2 levels of repeated elements, it's generally easier to use another technique whereby you loop through the $record array, extracting each of the element types you're interested in into an array.

In this case, it can be done by looking for "/OPTIONLIST" and "-NAME" which tells us that we have a new option list, and then by looking for "/OPTIONLIST" and "VALUE" which tells us that we have a value. These items can then be stored in local variables, and added to the array.

It's probably easier to see what I mean by looking at the code - here is a script that just shows how to construct the arrays, displaying the resulting array using print_r (and using content type text/plain for clarity):

View Output

<?php
  header
("Content-Type: text/plain");
  require(
"MagicParser.php");
  function 
myRecordHandler($record)
  {
    
$options = array();
    foreach(
$record as $k => $v)
    {
      if (
strpos($k,"/OPTIONLIST") && strpos($k,"-NAME"))
      {
        
$currentOptionList $v;
      }
      if (
strpos($k,"/OPTIONLIST") && strpos($k,"VALUE"))
      {
        
$options[$currentOptionList][] = $v;
      }
    }
    
print_r($options);
    print 
"\n\n";
  }
  
MagicParser_parse("http://www.claddaghshop.com/text.xml","myRecordHandler","xml|STOREEXPORT/PRODUCTS/PRODUCT/");
?>

Secondly, here is a bit more code showing how to use the new $options array to extract each of the sub-arrays and display then as an HTML drop-down box...

View Output

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    
$options = array();    
    print 
"<h2>".$record["DESCRIPTION"]."</h2>";
    foreach(
$record as $k => $v)
    {
      if (
strpos($k,"/OPTIONLIST") && strpos($k,"-NAME"))
      {
        
$currentOptionList $v;
      }
      if (
strpos($k,"/OPTIONLIST") && strpos($k,"VALUE"))
      {
        
$options[$currentOptionList][] = $v;
      }
    }
    
// now create select boxes for each of the option lists
    
foreach($options as $name => $values)
    {
      print 
$name.": ";
      print 
"<select>";
      foreach(
$values as $value)
      {
        print 
"<option>".$value."</option>";
      }
      print 
"</select>";
      print 
"<br /><br />";
    }
    print 
"<hr />";
  }
  
MagicParser_parse("http://www.claddaghshop.com/text.xml","myRecordHandler","xml|STOREEXPORT/PRODUCTS/PRODUCT/");
?>

Hope this helps!
Cheers,
David.

Submitted by dougb on Tue, 2008-02-05 17:08

Thanks David that's great!