Warning: Trying to access array offset on value of type bool in /var/www/vhosts/tomelliott.com/httpdocs/wp-content/themes/tomelliott/single.php on line 12

Spry XML Dataset: Selecting a Single Row

28 January, 2012 by Tom Elliott

I’ve been working a with various Spry functionality and xml feed integration recently. Being able to select a single row from a Spry XML Dataset sounds like it should be easy but the solution wasn’t immediately obvious. Hopefully the below will save you some time.

OK, so let’s say you’ve got a nice XML feed displaying on your site, in a repeat region showing X number of rows but you want to enable a click through to a single articlefeed item showing more information, a ‘Master Detail Page Set’ if you like.

The process to do this involves:

  1. Link to a detail page from the repeat region, passing the unique row ID
  2. Filtering the unique, specific record from the xml using the passed ID

1. Link to the detail page

The Spry generated repeat region code may look a little like this:

{title}

{articleDate}

{articleContent}

To identify the unique row, we use {ds_RowID}. Adding this to a link – in this case the article heading, we can select and pass the unique Row ID. The code will then look more like:

{articleDate}

{articleSnippet}

2. Selecting a single row from the Spry dataset

The next stage is to select the unique row from the xml using the row ID on the detail page. We can use the same feed file (providing it has the full information we want) and add a few lines of JavaScript below the main dataset declaration:

var ds1 = new Spry.Data.XMLDataSet("feed.xml", "rss/item");

ds1.addObserver({onPostLoad: function(ds, type) {
 ds1.setCurrentRow(X); 
} 
});

The addObserver line listens until the xml or data has been loaded before setting the current row. ‘X’ is the row ID which we have passed as a URL parameter and from PHP, this can generated by:

ds1.setCurrentRow(); 

Finally, we need to make sure the spry region is not a repeat region (which will still display all rows). i.e. something like:

{title}

{articleDate}

{articleContent}

You should now see all the single row details, filtered from the full xml