You are here:  » Counting records


Counting records

Submitted by loet on Thu, 2007-10-04 23:30 in

Hello David,

I hope you can helpme with this one.
I get this response from xml-rpc in xml:

XML file

I am using the script to get the data on the page.
Everything works OK and I am getting the data on my page.
But I want to put it in a formatted table with nested collumns.
Therefore I have to get a count of the value period or $periode but I cannot figure it out.
I know it is 3x 1w, 2x 2w and 1x 3w.

Can you help me out on this?
Below is the script I use to get the data on screen:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
    {
    
$periode $record["VALUE/STRING"];
    
$a_datum $record["VALUE/STRING@1"];
    
$a_tijd1 $record["VALUE/STRING@2"];
    
$a_tijd2 $record["VALUE/STRING@3"];
    
$v_datum $record["VALUE/STRING@4"];
    
$v_tijd $record["VALUE/STRING@5"];
    
$bor $record["VALUE/STRING@6"];
    
$huur $record["VALUE/STRING@7"];
    
$huur_excl $record["VALUE/STRING@8"];
    
$cs $record["VALUE/STRING@9"];
    echo 
"Boekinsperiode : ".$periode;
    echo 
"<br />Aankomstdatum : ".substr_replace($a_datum"", -55);
    
//echo "Aankomst van : ".$a_tijd1." tot : ".$a_tijd2;
    //echo $a_tijd2;
    
echo "Vertrekdatum : ".substr_replace($v_datum"", -55);
    
//echo $v_tijd;
    
echo $bor;
    echo 
"Huurprijs aanbieding: ".$huur;
    echo 
"Huurprijs : ".$huur_excl;
    
//echo $cs ;
    
echo "<br />-----------------<br />";
}    
 
//$data = ""; // string variable contianing the data to parse
  
MagicParser_parse("data.xml","myRecordHandler","xml|METHODRESPONSE/PARAMS/PARAM/VALUE/STRUCT/MEMBER/");
  
?>

Loet

Submitted by support on Fri, 2007-10-05 07:52

Hello Loet,

In situations like this, you really need to process the file twice. Firstly to count the number 1W, 2W etc., and the second one to display the information as you are already doing.

To count hose fields, I would suggest using a simple array, where the "1W", "2W" etc. is the key, with the value being the count. Then you can count them like this:

<?php
  
function myCountRecordHandler($record)
  {
    global 
$count// make sure the array is declared as global
    
$periode $record["VALUE/STRING"];
    
$count[$periode] = $count[$periode] + 1;
  }
  
MagicParser_parse("data.xml","myCountRecordHandler","xml|METHODRESPONSE/PARAMS/PARAM/VALUE/STRUCT/MEMBER/");
  
// here, the count of each will be in the $count array, for example:
  
print $count["1W"]
  
// with the sample data that should print "3".
?>

The above is in addition to your existing code, which comes next, where you can access the count of each of the periode fields using the $count array as shown in the example...

Hope this helps!
Cheers,
David.

Submitted by support on Fri, 2007-10-05 13:00

Hello Loet,

The trick here is not to try and make your table within the record handler function.

What you need to do is load the data into a global array in a format that makes it easy to display in the required table format later on, when you have finished parsing.

I have written a demo for you using your data to show what I mean. The easiest way to see how it works is to look through, so see what you think and let me know if you have any questions about how it works. The script uses the foreach operator, so you might want to see how that works if you are not familiar with it.

Here is the output running on this server

Here is the source:

<?php
  
require("MagicParser.php");
  
// global array to hold periodes
  
$periodes = array();
  function 
myRecordHandler($record)
  {
    global 
$periodes;
    
$periode $record["VALUE/STRING"];
    
// load this periode into a temporary array
    
$p = array();
    
$p["pa_datum"] = $record["VALUE/STRING@1"];
    
$p["a_tijd1"] = $record["VALUE/STRING@2"];
    
$p["a_tijd2"] = $record["VALUE/STRING@3"];
    
$p["v_datum"] = $record["VALUE/STRING@4"];
    
$p["v_tijd"] = $record["VALUE/STRING@5"];
    
$p["bor"] = $record["VALUE/STRING@6"];
    
$p["huur"] = $record["VALUE/STRING@7"];
    
$p["huur_excl"] = $record["VALUE/STRING@8"];
    
$p["cs"] = $record["VALUE/STRING@9"];
    
// and add to the periodes array indexed by periode (1W, 2W etc.)
    
$periodes[$periode][] = $p;
    
//echo "Boekinsperiode : ".$periode;
    //echo "<br />Aankomstdatum : ".substr_replace($a_datum, "", -5, 5);
    //echo "Aankomst van : ".$a_tijd1." tot : ".$a_tijd2;
    //echo $a_tijd2;
    //echo "Vertrekdatum : ".substr_replace($v_datum, "", -5, 5);
    //echo $v_tijd;
    //echo $bor;
    //echo "Huurprijs aanbieding: ".$huur;
    //echo "Huurprijs : ".$huur_excl;
    //echo $cs ;
  
}
  
// load the data into an array suitable for display
  
MagicParser_parse("data.xml","myRecordHandler","xml|METHODRESPONSE/PARAMS/PARAM/VALUE/STRUCT/MEMBER/");
  
// now display the contents of $periods array
  
print "<table border='1'>";
  foreach(
$periodes as $periode => $p)
  {
    print 
"<tr>";
    print 
"<th>".$periode."</th>";
    foreach(
$p as $record)
    {
      print 
"<td>";
      print 
$record["pa_datum"];
      print 
" / ";
      print 
$record["v_datum"];
      print 
" ";
      print 
$record["huur_excl"];
      print 
"</td>";
    }
    print 
"</tr>";
  }
  print 
"</table>";
?>

Secondly, thank you for trying Price Tapestry as well earlier. Because that includes Magic Parser, there is no need for you to have paid for this as well, so I will refund your Magic Parser purchase!

All the best,
David.

Submitted by loet on Tue, 2007-10-09 19:39

Hello David,

I am stuck again. I think I am not that good in arrays yet. I do not seem to get it.
I have looked at the above code you have written.
I have changed it to my needs and that is working wonderfull.
So I looked if I could change it a bit to show some other information.
I have another xml with all the booking data for 1 house for about 1 year.
What I want is to sort that for every period so I can always show the lowest price for each period.
Something like:
1 week for 439
1 weekend from 400 now 300
1 midweek for 250, etc.

I started with something like this:

<?php
require("./inc/MagicParser.php");
 // global array to hold periodes
  $periodes = array();
  function laagsteprijs($record)
  {
    global $periodes;
    $periode = $record["VALUE/STRING"];
$p = array();
//Only the records with 1w
//if ($record["VALUE/STRING"] == "1w") return;
$p["periode"] = $record["VALUE/STRING"];
$p["huur"] = $record["VALUE/STRING@7"];
        $p["huur_excl"] = $record["VALUE/STRING@8"];
$periodes[$periode][] = $p;
print_r($p);
  }
  // load the data into an array suitable for display
  MagicParser_parse("http://www.aloha-internetdiensten.nl/prijzen.xml","laagsteprijs","xml|METHODRESPONSE/PARAMS/PARAM/VALUE/STRUCT/MEMBER/");
?>

That gives me an array like this but I cannot figuere it out how to sort it.
Array
(
    [periode] => 1w
    [huur] => 478
    [huur_excl] => 478
)
Array
(
    [periode] => 1w
    [huur] => 478
    [huur_excl] => 478
)
Array
(
    [periode] => 1w
    [huur] => 509
    [huur_excl] => 509
)
Array
(
    [periode] => 1w
    [huur] => 125
    [huur_excl] => 125
)
Array
(
    [periode] => 1w
    [huur] => 200
    [huur_excl] => 200
)

I tried many examples from the php manual for aome days now but cannot get it.
Do you have a hint for me to set me the right way?
Thanks in advance,
Loet

Submitted by support on Tue, 2007-10-09 19:55

Hello Loet,

The output that you are displaying are the single arrays, being printed from the laagsteprijs function.

If you want something to sort, you need to add them to one big array.

In your code, that is being done (the global $periodes array), but as you are only doing the 1W periode, there would only be one element in that array when the parser had finished (the last one).

To sort them, you need to decide what to make that main index ($periode in the above example), then you can use array_sort on $periodes.

Do you know what the separate values are for 1 Week, 1 Weekend, Midweek etc.?

I think you need to create an array with one value for each, and then in each record you see if it is the cheapest, and set it if so...

Cheers,
David.

Submitted by loet on Wed, 2007-10-10 19:53

Hi David,

I was testing again and can make 2 different arrays now, one like this:

Array
(
    [0] => Array
        (
            [1w] => Array
                (
                    [huur] => 478
                    [huur_excl] => 478
                )
        )etc.
and he other like this:
Array
(
    [1w] => Array
        (
            [0] => Array
                (
                    [huur] => 478
                    [huur_excl] => 478
                )
            [1] => Array
                (
                    [huur] => 478
                    [huur_excl] => 478
                )
)
    [2w] => Array
        (
            [0] => Array
                (
                    [huur] => 921
                    [huur_excl] => 921
                )
)

There are in total 6 different price periods, 1w, 2w, 3w, wk, lw, mw.
I also thought that if I could select the lowest price for 1 period I could put it in a function to use it for every period.

Loet