You are here:  » Sorting data


Sorting data

Submitted by Chris Mc on Tue, 2009-04-14 13:25 in

Hello

I have been trying to use Magic Parser to create a table to compare credit cards. The basic table works fine, but I would like to give people the opportunity to sort the data by various criteria, and I've not been able to figure out how to do this. I'd be very grateful for any help.

My code is:

<?php
require("MagicParser.php");
print 
"<table width='900px' valign='top' border='1'>";
function 
myRecordHandler($record)
{
global 
$fee;
$fee [] = $record['Annual fee'];
global 
$counter;
if (
$counter++ % 2)
{print 
"<tr class='row0'>";}
else {print 
"<tr class='row1'>";}
print 
"<td valign='top' width='100px'>";
print 
"".$record['Name']."";
print 
"</td>";
print 
"<td valign='top' width='100px'>";
print 
"".$record['Annual fee']."";
print 
"</td>";
print 
"<td valign='top' width='100px'>";
print 
"".$record['AER']."";
print 
"</td>";
print 
"<td valign='top' width='500px'>";
if (!empty(
$record['Introductory offer'])) print "&bull;&nbsp; ".$record['Introductory offer']." <br>";
if (!empty(
$record['Features1'])) print "&bull;&nbsp; ".$record['Features1']." <br>";
if (!empty(
$record['Features2'])) print "&bull;&nbsp; ".$record['Features2']." <br>";
if (!empty(
$record['Features3'])) print "&bull;&nbsp; ".$record['Features3']." <br>";
if (!empty(
$record['Features4'])) print "&bull;&nbsp; ".$record['Features4']." <br>";
if (!empty(
$record['Features5'])) print "&bull;&nbsp; ".$record['Features5']." <br>";
print 
"</td>";
print 
"<td width='100px'>";
print 
"<a href='".$record['More']."' target='_blank'>More...</a>";
}
MagicParser_parse("creditcards070409.psv.txt","myRecordHandler","csv|124|1|0");
sort ($fee);
print 
"</tr>";
print 
"</table>";
?>

And the psv file is:

{code saved}

The PHP file above attempts to sort this data by Annual Fee (ie I want the record with the lowest annual fee to appear at the top of the table followed by the second lowest fee etc etc). It doesn't work and I can't figure out what I've done wrong. I'd be very grateful if you could point me in the right direction.

Many thanks

Chris Mc

Submitted by support on Tue, 2009-04-14 13:51

Hi Chris,

The trick here is to load the items into a global array ($records), then sort $records based on your criteria - PHP's usort() function is good for this, and then finally display the records using a foreach(). Here's a working example based on your datafeed:

<?php
require("MagicParser.php");
// global array to hold records
$records = array();
function 
myRecordHandler($record)
{
  global 
$records;
  
$records[] = $record;
}
// parse datafeed to populate $records
MagicParser_parse("creditcards070409.psv.txt","myRecordHandler","csv|124|1|0");
// sort $records by Annual fee column using usort
// http://uk.php.net/manual/en/function.usort.php
// decimalise() function returns a value from formatted price fields
function decimalise($price)
{
  
$price str_replace(",",".",$price);
  
$price preg_replace('/[^0-9\.]/e','',$price);
  
$price sprintf("%.2f",$price);
  return 
$price;
}
function 
sortByAnnualFee($a,$b)
{
  if (
decimalise($a['Annual fee']) == decimalise($b['Annual fee'])) {
      return 
0;
  }
  return (
decimalise($a['Annual fee']) < decimalise($b['Annual fee'])) ? -1;
}
usort($records,"sortByAnnualFee");
// finally display the now sorted records using foreach()
print "<table width='900px' valign='top' border='1'>";
foreach(
$records as $record)
{
  if (
$counter++ % 2)
  { print 
"<tr class='row0'>";}
  else { print 
"<tr class='row1'>";}
  print 
"<td valign='top' width='100px'>";
  print 
"".$record['Name']."";
  print 
"</td>";
  print 
"<td valign='top' width='100px'>";
  print 
"".$record['Annual fee']."";
  print 
"</td>";
  print 
"<td valign='top' width='100px'>";
  print 
"".$record['AER']."";
  print 
"</td>";
  print 
"<td valign='top' width='500px'>";
  if (!empty(
$record['Introductory offer'])) print "&bull;&nbsp; ".$record['Introductory offer']." <br>";
  if (!empty(
$record['Features1'])) print "&bull;&nbsp; ".$record['Features1']." <br>";
  if (!empty(
$record['Features2'])) print "&bull;&nbsp; ".$record['Features2']." <br>";
  if (!empty(
$record['Features3'])) print "&bull;&nbsp; ".$record['Features3']." <br>";
  if (!empty(
$record['Features4'])) print "&bull;&nbsp; ".$record['Features4']." <br>";
  if (!empty(
$record['Features5'])) print "&bull;&nbsp; ".$record['Features5']." <br>";
  print 
"</td>";
  print 
"<td width='100px'>";
  print 
"<a href='".$record['More']."' target='_blank'>More...</a>";
  print 
"</td>";
}
print 
"</table>";
?>

Here's the output running on this server:

http://www.magicparser.com/examples/creditcards.php

Hope this helps!
Cheers,
David.

Submitted by Chris Mc on Tue, 2009-04-14 14:17

David

Thank you very much. (I've spent ages trying to do this.)

Chris Mc