Hi,
I am trying to insert a random entry from an xml file with following snippet.
It works fine as a standalone php file.
But as a snippet it only echoes the html but there seems to be no output from the (randomized) array.
Is MODx something which gets in conflict with the script below?
Has anyone a hint for me what to change?
The mentioned xml file is a static file which i have put on the server.
It’s not set up as a ressource within MODx. (Would be even better, but the script tells me that it couldn’t open the file if i do so.)
Thanx a lot in advance (I am a little desperate right now)!
<?php
//Create variables telling the PHP class how to parse the XML file and tell it where the XML file is
$xml_file = "testimonials.xml";
$xml_kurz_key = "*TESTIMONIALS*TESTIMONIAL*KURZ";
$xml_lang_key = "*TESTIMONIALS*TESTIMONIAL*LANG";
$xml_lfdnummer_key = "*TESTIMONIALS*TESTIMONIAL*LFDNUMMER";
$xml_kunde_key = "*TESTIMONIALS*TESTIMONIAL*KUNDE";
$xml_datum_key = "*TESTIMONIALS*TESTIMONIAL*DATUM";
$featured_array = array();
$counter = 0;
function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}
function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
function contents($parser, $data) {
//Add each of your variables here, as well as a new case for each block from the XML file
global $current_tag, $xml_kurz_key, $xml_lang_key, $xml_lfdnummer_key, $xml_kunde_key, $xml_datum_key, $counter, $featured_array;
switch($current_tag){
case $xml_kurz_key:
$featured_array[$counter]->kurz = $data;
break;
case $xml_lang_key:
$featured_array[$counter]->lang = $data;
break;
case $xml_lfdnummer_key:
$featured_array[$counter]->lfdnummer = $data;
break;
case $xml_kunde_key:
$featured_array[$counter]->kunde = $data;
break;
case $xml_datum_key:
$featured_array[$counter]->datum = $data;
$counter++;
break;
}
}
$xml_parser = xml_parser_create('UTF-8');
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xml_file, "r") or die("Could not open file");
$data = fread($fp, filesize($xml_file)) or die("Could not read file");
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
for($y=0;$y<=0;$y++){
$featured_array = array_values($featured_array);
$arrayamt = (count($featured_array)-1);
$x = rand(0,$arrayamt);
echo "<p class=\"testimonialText\"><a href=\"/#anchor";
echo $featured_array[$x]->lfdnummer;
echo "\" title=\"";
echo $featured_array[$x]->kurz;
echo "\">„";
echo $featured_array[$x]->kurz;
echo " ...“ mehr</a></p>\n";
// echo $featured_array[$x]->lang;
echo "<p class=\"testimonialAuthor\">";
echo $featured_array[$x]->kunde;
// echo " // ";
// echo $featured_array[$x]->datum;
echo "</p>\n";
unset($featured_array[$x]);
}
?>