You are here:  » Filtering out data.


Filtering out data.

Submitted by KarlB on Wed, 2009-06-10 14:11 in

Hi David,

I wonder if you could help me with this problem. I am trying to filter records that contain a certain value, I have tried the example posted here without success http://www.magicparser.com/node/875

My code is

<?php
  
require("MagicParser.php");
  
$records = array();
  function 
myRecordHandler($record)
  {
    global 
$records;
    
$records[] = $record;
  }
     
$url "{link saved}";
    
MagicParser_parse($url,"myRecordHandler","xml|DATASET/DIFFGR:DIFFGRAM/NEWDATASET/AC/");
  if (
$record["DT1"]=="Mintex") return;
    foreach(
$records as $record)
  {
  echo 
'<pre>';
    
print_r ($record["DT1"]);
    echo 
'<pre>';
  }
?>

Could you obscure the url please if this gets posted live.

Many thanks, Karl

Submitted by support on Wed, 2009-06-10 14:25

Hi Karl,

It looks from your code as if you're trying to filter out records where DT1 is "Mintex". It's close, but the line to ignore the record appears outside of the record handler. Other than that, it looks fine... Have a go with this:

<?php
  
require("MagicParser.php");
  
$records = array();
  function 
myRecordHandler($record)
  {
    global 
$records;
    if (
$record["DT1"]=="Mintex") return;
    
$records[] = $record;
  }
  
$url "{link saved}";
  
MagicParser_parse($url,"myRecordHandler","xml|DATASET/DIFFGR:DIFFGRAM/NEWDATASET/AC/");
  foreach(
$records as $record)
  {
    echo 
'<pre>';
    
print_r ($record["DT1"]);
    echo 
'<pre>';
  }
?>

Cheers,
David.

Submitted by KarlB on Wed, 2009-06-10 14:51

Thanks for that, but I'm afraid it is still returning the same results.

I did try and post some a snippet of the xml but it wont display properly, but if you copy the url from my previous message in to a browser you can veiw it.

Do you think it could be to do with this line?

"xml|DATASET/DIFFGR:DIFFGRAM/NEWDATASET/AC/"

I've just tried a few variations, but the results remain the same except when I remove the /AC of the end and ammend

if ($record["AC/DT1"]=="Mintex") return;

But then it only returns one result which is still unfiltered.

Many thanks, Karl

Submitted by support on Wed, 2009-06-10 14:57

Hi Karl,

Could you email me the URL and I'll take a look - it is probably just down to getting the correct Format String and then value of key to use into $record...

Cheers,
David.

Submitted by KarlB on Wed, 2009-06-10 15:07

Thanks David, email on its way.

Regards, Karl

Submitted by support on Wed, 2009-06-10 15:37

Hi Karl,

Thanks - I checked the data. The format string is fine, but since the field contains information other than the string you are trying to filter against, it would need to be done using strpos() rather than looking for an exact match, for example:

<?php
  
require("MagicParser.php");
  
$records = array();
  function 
myRecordHandler($record)
  {
    global 
$records;
    if (
strpos($record["DT1"],"Mintex")!==FALSE) return;
    
$records[] = $record;
  }
  
$url "{link saved}";
  
MagicParser_parse($url,"myRecordHandler","xml|DATASET/DIFFGR:DIFFGRAM/NEWDATASET/AC/");
  foreach(
$records as $record)
  {
    echo 
'<pre>';
    
print_r ($record["DT1"]);
    echo 
'<pre>';
  }
?>

Hope this helps!
Cheers,
David.

Submitted by KarlB on Wed, 2009-06-10 16:05

David,

That worked a treat!

Many thanks for your speedy assistance.

Best regards, Karl