You are here:  » Filtering data


Filtering data

Submitted by felixbrabant on Wed, 2015-02-11 19:06 in

Hi,

I need to apply filters to a csv file. I need to do 2 things :

- Select only rows where the 7th field says "Open"
- Ommit empty rows (531-535 in my example code)

As I understand it, I just need to tell the script something like "if 'Field7' equals 'Open' go on. else do nothing"

I tried a few things, but I'm not so good with PHP syntax. (http://www.magicparser.com/node/414)

Can anyone help me edit the code to make it work?

Thanks!

print '<table><tr><th>#</th><th>Project</th></tr>';
function torontoOngoingProjects($record)
{
print '<tr><td>';
print $record['FIELD1'];
print '</td>';
print '<td>';
print $record['FIELD4'];
print '</td></tr>';
}
MagicParser_parse('CSV URL','torontoOngoingProjects','csv|44|0|34|1');
print '</table>';

CSV Example

Docket #,DATE,AGENCY,PRODUCT,MEDIUM,Producer,Status
527,Feb.06.15,Bla bla bla Agency,Some product,TV/Web/Cinema,Tom,Cancelled
528,Feb.06.15,Other agency,Some other product,Web,Tom,Closed
529,Feb.06.15,Agency 2000,Awesome Product,Writing Session,Tom,Open
530,Feb.09.15,Other Agency,Yet another product,TV/Web,Tom,Open
531,,,,,Select,Select Status
532,,,,,Select,Select Status
533,,,,,Select,Select Status
534,,,,,Select,Select Status
535,,,,,Select,Select Status

Submitted by support on Wed, 2015-02-11 19:16

Hello Felix, and welcome to the forum!

I will follow up separately with regards to the format and record structure, but I can help you quickly based on Magic Parser interpreting your feed starting at row 2 and ignoring the header row, which is why $record keys are "FIELD1", "FIELD2" etc.

If you only wish to process a record if FIELD7 is "Open", then you can put a test at the top of your myRecordHandler() function like this:

function torontoOngoingProjects($record)
{
  if ($record["FIELD7"] != "Open") return;
  ... rest of code ...

Empty rows look like they can be identified if FIELD2 is empty, so you could extend the above as follows (inserting the empty row check first) :

function torontoOngoingProjects($record)
{
  if ($record["FIELD2"] == "") return;
  if ($record["FIELD7"] != "Open") return;
  ... rest of code ...

Hope this helps!
Cheers,
David
--
MagicParser.com

Submitted by felixbrabant on Wed, 2015-02-11 19:27

Hey!

Thanks for replying so quickly!

I tried that, and it displays nothing. Again, my knowledge of php is limited. I must have forgotten / not understood something?

Thanks

print '<table><tr><th>#</th><th>Project</th></tr>';
function torontoOngoingProjects($record)
{
  if ($record["FIELD2"] == "") return;
  if ($record["FIELD7"] != "Open") return;
print '<tr><td>';
print $record['FIELD1'];
print '</td>';
print '<td>';
print $record['FIELD4'];
print '</td></tr>';
MagicParser_parse('CSV CONTENT','torontoOngoingProjects','csv|44|0|34|1');
print '</table>';
}

Submitted by support on Wed, 2015-02-11 20:22

Hi,

I'm not sure if your sample code lost something between your source file and posting, but as above the call to MagicParser_parse() is enclosed within the record handler function so that might be all it is - with your placeholders preserved, have a go with:

print '<table><tr><th>#</th><th>Project</th></tr>';
function torontoOngoingProjects($record)
{
  if ($record["FIELD2"] == "") return;
  if ($record["FIELD7"] != "Open") return;
  print '<tr><td>';
  print $record['FIELD1'];
  print '</td>';
  print '<td>';
  print $record['FIELD4'];
  print '</td></tr>';
}
MagicParser_parse('CSV CONTENT','torontoOngoingProjects','csv|44|0|34|1');
print '</table>';

Notice how the call to MagicParser_parse and printing of the table closure is outside of the record handler function ("torontoOngoingProjects" in your code").

If you're still not sure, if it would be OK to submit your source CSV document as a Get It Coded request (use the default option of Display contents as an HTML document) and I'll check it out for you, and also address the other issue of format detection...

Hope this helps!
Cheers,
David
--
MagicParser.com