You are here:  » sort by points in array


sort by points in array

Submitted by mto on Thu, 2007-03-29 11:15 in

Hi there,

Like to know how I can sort the array when the array looks like this:

$record["SITE/NAME@0"];
$record["SITE/URL@0"];
$record["SITE/DOMAIN@0"];
$record["SITE/POINTS@0"];

$record["SITE/NAME@1"];
$record["SITE/URL@1"];
$record["SITE/DOMAIN@1"];
$record["SITE/POINTS@1"];

$record["SITE/NAME@2"];
$record["SITE/URL@2"];
$record["SITE/DOMAIN@2"];
$record["SITE/POINTS@2"];

How can I sort this on the POINTS?

Submitted by support on Thu, 2007-03-29 11:35

Hi,

Firstly, you may be ending up with the @1, @2 etc. resolved field names because Magic Parser's autodetection has not picked out the correct record level within your XML document, so the first question is - are you using a Format String, or have you used autodetection?

For example, if you are currently using something like:

xml|FORMAT/STRING/

...you could probably extract each SITE record on its own by adding another level, for example:

xml|FORMAT/STRING/SITE/

If this is not the case, and you have multiple records in this XML document that each contain information about multiple sites; then the way to go about handling this is to create a loop that adds the @1, @2 etc., checking for values each time and then using them as required. For example:

  function myRecordHandler($record)
  {
    $i = 0;
    while(1) {
      if ($i) $postfix = "@".$i;
      if (!isset($record["SITE".$postfix])) break;
      $name = $record["SITE/NAME".$postfix];
      $url = $record["SITE/URL".$postfix];
      $domain = $record["SITE/DOMAIN".$postfix];
      $points = $record["SITE/POINTS".$postfix];
      $i++;
    }
  }

At this point, you could, for example, load each site record into a global array of sites:

  $sites = array();
  function myRecordHandler($record)
  {
    global $sites;
    $i = 0;
    while(1) {
      if ($i) $postfix = "@".$i;
      if (!isset($record["SITE".$postfix])) break;
      $site["name"] = $record["SITE/NAME".$postfix];
      $site["url"] = $record["SITE/URL".$postfix];
      $site["domain"] = $record["SITE/DOMAIN".$postfix];
      $site["points"] = $record["SITE/POINTS".$postfix];
      // add this $site to the global $sites array
      $sites[] = $site;
      $i++;
    }
  }
  // parse your file or URL as normal
  MagicParser_parse(...);
  // now you can use the $sites array
  print_r($sites);

Hope this helps!
Cheers,
David.

Submitted by Janet Kellman on Wed, 2007-07-11 15:06

David, thanks for your post.
I'm have similar problem. I have

$cook["COOK/CAT@1"];
$cook["COOK/SUBCAT@1"];
$cook["COOK/NAME@1"];
$cook["COOK/TITLE@1"];
etc.

I try your function
function myRecordHandler($record)
{
$i = 0;
while(1) {
if ($i) $postfix = "@".$i;
if (!isset($record["SITE".$postfix])) break;
$name = $record["SITE/NAME".$postfix];
$url = $record["SITE/URL".$postfix];
$domain = $record["SITE/DOMAIN".$postfix];
$points = $record["SITE/POINTS".$postfix];
$i++;
}
}
(I replace "site" and others to my values like cook)
But it didn't work for me. Why ?
Thanks in advance.

Janet Kellman, software reviews

Submitted by support on Wed, 2007-07-11 15:45

Hi Janet,

The problem above was resolved by parsing with a different "format string" to the one automatically detected by Magic Parser. In order to work this out in your situation i'd need to see the XML that you are working with.

If you could you email me your XML file as an attachement, or alternatively a link from where I can download it i'll take a look for you. Reply to your reg code or forum registration email is the best way to contact me..

Cheers,
David.