I write this third message because I've got another question and maybe it's easier for people to find solutions on the forum.
I'm realy sorry, I tried by myself, red many forum topics ... but shame on me ... I can't find a solution !!
I've got a XML feed (same one as describe in other topic) with many real estate ,with many field to describe each one. One of this field is a kind of Region one. There is something like 10 regions (areas) in the field (for 500 items).
I what I need is to list the categories (regions), but of course without duplicating each one. And if it's possible counting the number of items per Region !!!
Actualy I did the basic scheme
<?php
require("MagicParser.php");
function myRecordHandler($record)
{
$region= utf8_decode($record["BIENDESC/LREGION"]);
print "".$region." <br />";
}
// parse the xml feed
MagicParser_parse("myfile.xml","myRecordHandler","xml|BACKSLASH/BIEN/");
?>
Thank you David. I'll write what I finaly done if it can help someone. The use of this piece of script was for a select form field.
<?php
require("MagicParser.php");
array ($regions); // Don't know why but don't work without ()
function myRecordHandlerform($record)
{
global $regions;
// Conditions because I don't want the script to return items with blank answers or with "A déterminer"
if ($record["BIENDESC/LREGION"]==FALSE) return;
if ($record["BIENDESC/LREGION"]=="A déterminer") return;
$region = $record["BIENDESC/LREGION"];
$regions[$region]++;
}
MagicParser_parse("myfile.xml","myRecordHandlerform","xml|BACKSLASH/BIEN/");
foreach($regions as $region => $count)
{
// Definition of $region2 to write the URLs and use + instead of space
$region2 = $region ;
$region2 = str_replace(" ","+",$region2);
print "<option value=\"".$region2."\">".$region." (".$count.")</option> \n";
}
?>
It seems to work for me, I hope that the code is correct
Oh sorry I forgot to mention that I use also a cache system for all my pages and use it also for the generated XML file for category listing
That fonction with my xml file require lot of time to sort a result of maximum 2 categories ! More than 5 seconds added to the base of the script : parsing the items with another array, that was realy realy long : 9s .
So I used this category listing function in another page to generate a xml file that I parse with basic use in the original page where I need that category menu. With that getting my pages need now 5s. It's long but it seem that doin' an array before re-parsing is long anyway!
Hi there!
This is another situation where it's best to parse your data into a global array, and then display it after the parse. In this case, creating an associative array where the category is the key and the value is the counter would be a good solution. Have a go with something like this:
<?php
require("MagicParser.php");
array $regions;
function myRecordHandler($record)
{
global $regions;
$region = utf8_decode($record["BIENDESC/LREGION"]);
$regions[$region]++;
}
MagicParser_parse("myfile.xml","myRecordHandler","xml|BACKSLASH/BIEN/");
foreach($regions as $region => $count)
{
print $region." (".$count.")<br />";
}
?>
Hope this helps!
Cheers,
David.