You are here:  » Only show certain records

Support Forum



Only show certain records

Submitted by badger on Mon, 2007-01-08 15:48 in

Hi David, im using MP to create a simple directory of links from an XML file.

I only want to display the records where parent="root", ive pasted my code below.

Thought this would work?  if (($link["PARENT"] == "ROOT")){ }

XML file:

<?xml version="1.0" encoding="iso-8859-1"?>
<links>
  <link>
    <name>Rings</name>
    <url>http://www.designer-jewellery-store.com/rings/</url>
   <parent>root</parent>
  </link>
  <link>
    <name>Earrings</name>
    <url>http://www.designer-jewellery-store.com/earrings/</url>
  <parent>root</parent>
  </link>
  <link>
    <name>Watches</name>
    <url>http://www.designer-jewellery-store.com/watches/</url>
  <parent>root</parent>
  </link>
</links>

And here is my php:

<?php
  require("MagicParser.php");
  function myRecordHandler($link)
  {
if (($link["PARENT"] == "ROOT"))
{
print "<a href='".$link["URL"]."'>".$link["NAME"]."</a>&nbsp;";
}
else
{
print "no root categories <br />";
}
  }
  MagicParser_parse("MagicParserTest.xml","myRecordHandler","xml|LINKS/LINK/");
?>

Thanks for any comments

Submitted by support on Mon, 2007-01-08 18:52

Hi,

You're almost there. The main problem I can see is that you are printing that there are no root categories within your record handler function - however at that point you don't know if there are going to be any further (or have been any previous) root categories - so this is not the place to make this decision.

Your code to only display a link if the parent is root is still correct; all you need to do is create a global variable to count the number of root links displayed, and if it is still zero after the call to MagicParser_parse() then you can print the message that no root links exist. For example: (based on your code)

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($link)
  {
    global 
$counter;
    
// I think root will be lower case - check this with your XML
    
if (($link["PARENT"] == "root"))
    {
      
// increment counter of root links
      
$counter++;
      print 
"<a href='".$link["URL"]."'>".$link["NAME"]."</a>&nbsp;";
    }
  }
  
MagicParser_parse("MagicParserTest.xml","myRecordHandler","xml|LINKS/LINK/");
  if (!
$counter)
  {
    print 
"no root categories <br />";
  }
?>

Hope this helps,
Cheers,
David.

Submitted by badger on Mon, 2007-01-08 19:01

Thanks for that David, however what i was really trying to figure out was why $link["PARENT"] == "root" doesnt equal true?

It should be true for all 3 items in the the XML file.

thanks

Submitted by support on Mon, 2007-01-08 19:30

Hi badger;

It certainly should do - although based on your pasted XML it will be "root" in lower case and not upper case as in your first example. Have you tried print_r($link) to confirm the value of each item in the array?

Cheers,
David.

Submitted by badger on Mon, 2007-01-08 20:06

I have it as lower case. And the output from print_r($link) is

Array ( [LINK] => [NAME] => Rings [URL] => http://www.designer-jewellery-store.com/rings/ [PARENT] => root ) Array ( [LINK] => [NAME] => Gold Rings [URL] => http://www.designer-jewellery-store.com/gold-rings/ [PARENT] => rings ) Array ( [LINK] => [NAME] => Earrings [URL] => http://www.designer-jewellery-store.com/earrings/ [PARENT] => root ) Array ( [LINK] => [NAME] => Watches [URL] => http://www.designer-jewellery-store.com/watches/ [PARENT] => root )

This is really confusing me?!?

Submitted by support on Mon, 2007-01-08 20:24

Hi,

It may be white space or other unprintable (within HTML characters). The first thing I would try is this:

if (trim($link["PARENT"]) == "root")

If this doesn't help, perhaps you could email me a sample of your XML (reply to your forum registration email) and i'll take a look for you...

Cheers,
David.

Submitted by badger on Mon, 2007-01-08 20:26

That's is :)

Thanks a bunch, this was driving me mad!!