You are here:  » Display Content based upon criteria match


Display Content based upon criteria match

Submitted by crusnac on Thu, 2010-12-30 20:04 in

Here is my XML file.

<?xml version="1.0" encoding="UTF-8"?>
<state type="array">
<stateName>Oregon</stateName>
<cities stateName="Oregon">
<city>
    <name>Adair Village</name>
    <content>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ac dolor et neque auctor eleifend sed vel felis. Praesent erat mi, ultrices ut porta vitae, elementum id purus. Mauris pellentesque odio vel ligula pharetra gravida. Praesent pretium interdum enim eget ullamcorper. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras pellentesque magna eu orci adipiscing dapibus. Aliquam congue, ipsum ac viverra mattis, ipsum nulla adipiscing ipsum, id tempus magna leo vel nibh. In hac habitasse platea dictumst. Vestibulum ac nisl eu metus dapibus tempor quis quis nunc. Pellentesque viverra nibh quis leo suscipit ornare. Curabitur nec ante tortor. Sed eget convallis odio.
    </content>
</city>
<city>
    <name>Adams</name>
    <content>Vestibulum sit amet turpis purus. Donec ac turpis tortor, ut laoreet turpis. Fusce nec rutrum mi. Aenean est lectus, auctor sed volutpat a, feugiat sed ipsum. Nullam non nisl orci, congue hendrerit tortor. Sed ut nisl orci, sollicitudin lobortis massa. Praesent et sapien lorem. Sed et accumsan risus. Suspendisse nec massa orci. Duis vestibulum accumsan venenatis. Quisque vel felis orci. Donec imperdiet purus eleifend est laoreet porta. Donec mollis tincidunt lobortis. Duis egestas sollicitudin nibh id faucibus. Cras vitae pharetra libero.
    </content>
</city>
<city>
    <name>Adrian</name>
    <content>Etiam ut eros a odio lobortis pharetra. Aliquam fringilla, felis ac laoreet placerat, turpis metus ornare velit, nec adipiscing ante arcu at nulla. Proin sollicitudin nibh sed sem volutpat iaculis. Quisque bibendum nunc et est dictum eget euismod metus volutpat. Ut fringilla tortor eget nibh vehicula convallis. Quisque sit amet neque sit amet velit consectetur viverra. Phasellus sodales tempor tellus aliquet euismod. Mauris libero purus, tincidunt at suscipit at, dictum ac justo. Nunc sit amet consequat odio. Cras quis purus sit amet massa fermentum eleifend. Etiam sapien nibh, semper et posuere sed, ullamcorper vitae velit. Vestibulum lacinia mollis molestie. Mauris non lorem eu justo sollicitudin fringilla. Etiam ac rhoncus odio. In hac habitasse platea dictumst. Nunc dictum risus id lectus faucibus in malesuada libero dapibus. Suspendisse ultricies vulputate mi, ut ullamcorper dui posuere ut. Nunc porttitor porta erat, ut convallis massa pretium vitae.
    </content>
</city>
<city>
    <name>Brookings</name>
    <content>Integer ipsum arcu, luctus ut malesuada non, tempus eget erat. In hac habitasse platea dictumst. Mauris pellentesque venenatis lectus, a venenatis mi hendrerit non. Fusce tincidunt tempor laoreet. Mauris tellus ligula, vestibulum eget aliquam ut, consectetur quis tellus. Suspendisse nec justo mauris, vel malesuada ipsum. Duis leo lectus, placerat sed congue at, porttitor vitae dui. Phasellus magna nisi, faucibus volutpat egestas condimentum, mattis sagittis lorem. Fusce vitae diam quam, ut tempus magna. Donec pulvinar nibh ut tellus sagittis pellentesque. Vestibulum vitae ultrices felis. Phasellus molestie malesuada interdum.
    </content>
</city>
</cities>
</state>
</xml>

I want to display the content area based upon the match of the city. What is the most appropriate way to do that?

Submitted by support on Tue, 2011-01-04 09:45

Hi crusnac,

An IF test within your myRecordHandler function would be the easiest way to do this, for example if the city name comes in the URL as ?city=Brookings, the following code would display the content field only for that city:

  function myRecordHandler($record)
  {
    // ignore record if not the city passed in the URL
    if ($record["NAME"] <> $_GET["city"]) return;
    // rest of myRecordHandler function below e.g.
    print "<p>".$record["CONTENT"]."</p>";
  }

Hope the helps!
Cheers,
David.

Submitted by bilenberg on Thu, 2011-02-17 06:12

Im using this method to parce a specific item from a xml file that looks like this:

...
...
<item id="1266427" itemType="kivihomepagetransfer">
    <property name="country_id">
        <value>4402</value>
    </property>
...
...
    <property name="extra_realtytype_id">
        <value>tontti</value>
    </property>
...
...
</item>
...

With this string in the myRecordHandler function:
...
    if ($record["ITEM-ID"] <> $_GET["ITEM-ID"]) return;
...

But now I would need to show all the items matching a specific "extra_realtype_id" (in this case "tontti").
I have been playing with this codestring, but I can´t figure it out. Any ideas?

Submitted by support on Thu, 2011-02-17 09:20

Hi,

The best thing to do here would be to construct an associative array of property[name] = value pairs, and then once that has been created you can use an IF statement on it. Have a go with something like this, which works based on the way Magic Parser resolves duplicate field name within $record by postfixing @1, @2, etc.

  function myRecordHandler($record)
  {
    $property = array();
    $i = 0;
    $p = "";
    while(1) {
      if ($i) $p = "@".$i;
      if (!$record["PROPERTY".$p."-NAME"]) break;
      $name = $record["PROPERTY".$p."-NAME"];
      $value = $record["PROPERTY".$p."/VALUE"];
      $property[$name] = $value;
      $i++;
    }
    if ($property["extra_realtytype_id"] <> "tontti") return;
    // rest of my record handler function here
  }

Hope this helps!
David
--
MagicParser.com

Submitted by bilenberg on Thu, 2011-02-17 21:29

I tried this out in several different ways... the closest I get to a result without getting a error message is this, but it will only give me a blank page:

<?php
  header("Content-Type: text/html; charset=UTF-8");
  require("kivixml/MagicParser.php");
  $count = 0;
  function myRecordHandler($record)
  {
    $property = array();
    $i = 0;
    $p = "";
    while(1) {
      if ($i) $p = "@".$i;
      if (!$record["PROPERTY".$p."-NAME"]) break;
      $name = $record["PROPERTY".$p."-NAME"];
      $value = $record["PROPERTY".$p."/VALUE"];
      $property[$name] = $value;
      $i++;
    }
    if ($property["itemgroup_id"] <> "tontit") return;
    $fields = array();
    $currentName = "";
    foreach($record as $key => $value)
    {
      if (strpos($key,"NAME"))
      {
        $currentName = $value;
      }
      if (strpos($key,"VALUE"))
      {
        $fields[$currentName] .= $value." ";
      }
    }
    // now we can just print the fields we want
    print "<b><a href='http://nymanlkv.fi/sv/details?ITEM-ID=".$record["ITEM-ID"]."'>".$fields["town"].", ".$fields["quarteroftown"].", ".$fields["realtytype_id"]."</b></a>";
    print "<table width='600px' border='0'>";
    print "<tr>";
    print "<td valign='top' align='right' width='150px'><img src=".$fields["image_url"]." width='150px'></td>";
    print "<td valign='top' width='450px'>".$fields["presentation"]."<br><b>".$fields["price"]." euro</b></td></tr>";
    print "</table><br>";
  }
  $filename = "http://www.nymanlkv.fi/thexml.php";
  if (!MagicParser_parse($filename,"myRecordHandler","xml|TRANSFERDATA/ITEM/"))
  {
    print MagicParser_getErrorMessage();
  }
?>

Submitted by support on Fri, 2011-02-18 09:08

Hi,

I just realised checking the XML that not every field name has a value, so the initial method of populating the $property array and then checking for itemgroup_id wouldn't of worked i'm afraid, however exactly the same check can be applied after the $fields array has been created, which doesn't rely on every name having a value... have a go with just this:

<?php
  header
("Content-Type: text/html; charset=UTF-8");
  require(
"kivixml/MagicParser.php");
  
$count 0;
  function 
myRecordHandler($record)
  {
    
$fields = array();
    
$currentName "";
    foreach(
$record as $key => $value)
    {
      if (
strpos($key,"NAME"))
      {
        
$currentName $value;
      }
      if (
strpos($key,"VALUE"))
      {
        
$fields[$currentName] .= $value." ";
      }
    }
    if (
$fields["itemgroup_id"] <> "tontit") return;
    
// now we can just print the fields we want
    
print "<b><a href='http://nymanlkv.fi/sv/details?ITEM-ID=".$record["ITEM-ID"]."'>".$fields["town"].", ".$fields["quarteroftown"].", ".$fields["realtytype_id"]."</b></a>";
    print 
"<table width='600px' border='0'>";
    print 
"<tr>";
    print 
"<td valign='top' align='right' width='150px'><img src=".$fields["image_url"]." width='150px'></td>";
    print 
"<td valign='top' width='450px'>".$fields["presentation"]."<br><b>".$fields["price"]." euro</b></td></tr>";
    print 
"</table><br>";
  }
  
$filename "http://www.nymanlkv.fi/thexml.php";
  if (!
MagicParser_parse($filename,"myRecordHandler","xml|TRANSFERDATA/ITEM/"))
  {
    print 
MagicParser_getErrorMessage();
  }
?>

Hope this helps,
Cheers,
David.

Submitted by bilenberg on Fri, 2011-02-18 10:40

Hi,

it´s still the same... when I put the if ($fields)-line in the code, it will only give me a blank page. And I have tried to use different field and return values to doubbelcheck that there actually are some content matching the criteria.

Submitted by support on Fri, 2011-02-18 10:50

Hi,

In the above code, in place of:

        $fields[$currentName] .= $value." ";

...assuming that multiple fields of the same name are not required, REPLACE that with:

        $fields[$currentName] = $value;

I think that should be all it is, as it was otherwise going to contain a value of "tontit " - e.g. with a trailing space which would have stopped the test from working...

Hope this helps!
Cheers,
David.

Submitted by bilenberg on Fri, 2011-02-18 10:59

Thank you very much again... Now it works like a charm!