Jump to content

problem with storing items in numbered array


jet-plane

Recommended Posts

I'm really hoping that someone will help me with this, as my head started to hurt..

I tried every single thing that crossed my worried mind and still nothing.

The problem is that the script is parsing and displaying everything but the 0th item in the array.

And I don't know why is that. This script does following, downloads the page with currencies,

extract xml and parse that with xml parser and then displays everything in a table.

I tried debugging it with echo (as seen in the script) but it just confuses me more.

Its displaying EUR and the array has 10 items, but for some reason it doesn't store EUR (which is 0th item) in the array..

well here is the script:

<?php

class xml_valuta {
var $valuta, $paritet, $kupovni, $prodajni;
}

class kursna_lista {

var $valuta_array = array();
var $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){
	global $current_tag, $counter, $valuta_array;
		$xml_valuta_key = "*KURSNA_LISTA*VALUTA*SIFRA_ALFA3";
	$xml_kupovni_key = "*KURSNA_LISTA*VALUTA*KUPOVNI";
	$xml_prodajni_key = "*KURSNA_LISTA*VALUTA*PRODAJNI";
	$xml_paritet_key = "*KURSNA_LISTA*VALUTA*PARITET";

	switch($current_tag){
		case $xml_valuta_key:
			$valuta_array[$counter] = new xml_valuta();
			$valuta_array[$counter]->valuta = $data;
			echo $valuta_array[$counter]->valuta,$counter,$current_tag;
			break;
		case $xml_paritet_key:
			$valuta_array[$counter]->paritet = $data;
			break;
		case $xml_kupovni_key:
			$valuta_array[$counter]->kupovni = $data;
			break;
		case $xml_prodajni_key:
			$valuta_array[$counter]->prodajni = $data;
			$counter++;
			break;
	}
} 

function getXML(){
	ini_set('user_agent', 'Mozilla Firefox');
	$date=date("d.m.Y");
	$url='http://www.nbs.yu/internet/latinica/scripts/kl.html?datum=';
	$url.=$date.'&broj=br.&godina='.date("Y").'&vrsta=1&eksport=xml';
	$xml = file_get_contents($url);
	preg_match("@(<kursna_lista>)(.*?)(</kursna_lista>)@si", $xml, $matches);
	$ret = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
	$ret .= $matches[0];
	return $ret;
}

function getKurs(){ /* ($content,$conf){ */
	global $current_tag, $valuta_array;
	$xml_kurs = $this->getXML();
	$datum = split("<[/]?datum_formiranja>",$xml_kurs);

	$this->xml_parser  =  xml_parser_create();
	xml_set_object ( $this->xml_parser, $this );
	xml_set_element_handler($this->xml_parser, array (&$this,"startTag"), array (&$this, "endTag"));
	xml_set_character_data_handler($this->xml_parser, array (&$this,"contents"));
	xml_parse($this->xml_parser, $xml_kurs);
	xml_parser_free($this->xml_parser);
echo "valuta:" . $valuta_array[0]->valuta, count($valuta_array);
	$content = "<hr/><b>Kursna Lista" ."</b><table><tbody><tr><td>Valuta:</td><td>Kupovni:</td><td>Prodajni:</td></tr>";
	for($x=0;$x<count($valuta_array);$x++){
		$content .= "<tr><td>" . $valuta_array[$x]->paritet . " " . $valuta_array[$x]->valuta . "</td>";
		$content .= "<td>" . $valuta_array[$x]->kupovni . "</td><td>" . $valuta_array[$x]->prodajni .  "</td></tr>";
		}
	$content .= "</tbody></table>Datum formiranja: ".$datum[1];
	return $content;
	}
}
$kurs = new kursna_lista();
echo $kurs->getKurs();
?>

It's writen in class because I intend to use it in the typo3 ..

 

the array which gives me a headache is $valuta_array .. and the item not stored would be $valuta_array[0] which is always empty for some reason.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.