You are here:  » grouping


grouping

Submitted by andras on Tue, 2009-04-28 21:33 in

please consider:

.....
<item>
<cat>category1</cat>
<desc>item1_cat1</desc>
</item>
<item>
<cat>category1</cat>
<desc>item2_cat1</desc>
</item>
<item>
<cat>category2</cat>
<desc>item1_cat2</desc>
</item>
<item>
<cat>category3</cat>
<desc>item1_cat3</desc>
</item>
<item>
<cat>category3</cat>
<desc>item2_cat3</desc>
</item>
.....

would it be possible to generate a list only of those items with property 'category1' and provide hyperlinks to display those items with properties 'category2' and 'category3', respectively?

an outline for approach may suffice, but if this is out of the realm of magic parser support, please feel free to direct me elsewhere.

thanks,
andras.

Submitted by support on Wed, 2009-04-29 07:16

Hi andras,

Sure - the basic trick here is to selectively show records based on the value of 1 (or more) fields, and then propagate a value through in the URL in your hyperlinks which is further used to control what is displayed - perhaps even using a separate myRecordHandler() function. For example:

<?php
  $categories 
= array();
  function 
myCategoryRecordHandler($record)
  {
    global 
$categories;
    
// using the category as the key of an associative array
    // means we only get unique category values!
    
$categories[$record["CAT"]] = 1;
  }
  function 
myItemRecordHandler($record)
  {
    if (
$record["CAT"] <> $_GET["cat"]) return;
    print 
"<p>".$item["DESC"]."</p>";
  }
  if (
$_GET["cat"])
  {
    
MagicParser_parse("items.xml","myItemRecordHandler","xml|ITEMS/ITEM/");
  }
  else
  {
    
MagicParser_parse("items.xml","myCategoryRecordHandler","xml|ITEMS/ITEM/");
    
ksort($categories); // sort categories into alphabetical order
    
foreach($categories as $category => $v)
    {
      print 
"<p><a href='?cat=".urlencode($category)."'>".$category."</a></p>";
    }
  }
?>

Hope this helps!
Cheers,
David.

Submitted by andras on Wed, 2009-04-29 20:48

that'll do ;^)

THANK YOU.