Jump to content

importing xml: easier way to get data into variables?


pake

Recommended Posts

I'm importing data from xml to my php-page. Since I'm a php-n00b I don't know how to make this easier (more dynamic) and I'm here to ask help (in order to learn). It's easier to explain what I want by showing my script so here's my code atm:

<?php
	// load xml
	$xml = 'matches.xml';
	echo "xml loaded: $xml <br /><br />"; //debug

	$dom = new DOMDocument;
	$dom->load($xml);
	if (!$dom) {
		echo 'Error while parsing the XML ($xml)';
		exit;
	}

	// get the data from xml to the variable $XMLdata
	$XMLdata = simplexml_import_dom($dom);

	//echo $XMLdata->name[0]; //debug
	$link1 	= $XMLdata->link[0];
	$day1 	= $XMLdata->day[0];
	$time1 	= $XMLdata->time[0];
	$place1 = $XMLdata->place[0];
	$match1 = $XMLdata->match[0];
	list($home1,$away1) = explode ( ' - ' , $match1 ) ;
	$score1 = $XMLdata->score[0];

	$link2 	= $XMLdata->link[1];
	$day2 	= $XMLdata->day[1];
	$time2 	= $XMLdata->time[1];
	$place2 = $XMLdata->place[1];
	$match2 = $XMLdata->match[1];
	list($home2,$away2) = explode ( ' - ' , $match2 ) ;
	$score2 = $XMLdata->score[1];

	$link3 	= $XMLdata->link[2];
	$day3 	= $XMLdata->day[2];
	$time3 	= $XMLdata->time[2];
	$place3	= $XMLdata->place[2];
	$match3	= $XMLdata->match[2];
	list($home3,$away3) = explode ( ' - ' , $match3 ) ;
	$score3	= $XMLdata->score[2];

	$link4 	= $XMLdata->link[3];
	$day4 	= $XMLdata->day[3];
	$time4 	= $XMLdata->time[3];
	$place4	= $XMLdata->place[3];
	$match4	= $XMLdata->match[3];
	list($home4,$away4) = explode ( ' - ' , $match4 ) ;
	$score4	= $XMLdata->score[3];

	// ...continues

	// add rows to the table
	for ($i=1; $i<=25; $i++) {
		$link 	= "link".$i;
		$day 	= "day".$i;
		$time 	= "time".$i;
		$place 	= "place".$i;
		$home 	= "home".$i;
		$away 	= "away".$i;
		$score 	= "score".$i;
		echo '<tr>';
		echo '<td width="20%" align="right"><a href="#'.$$link.'" onclick="loadContent(\'#content\', \'matcht_raportti.php?id=\''.$$link.'\')">'.$$day.'</a></td>';
		echo '<td width="8%" align="center"><a href="#'.$$link.'" onclick="loadContent(\'#content\', \'matcht_raportti.php?id=\''.$$link.'\')">'.$$time.'</td>';
		echo '<td width="20%" align="left"><a href="#'.$$link.'" onclick="loadContent(\'#content\', \'matcht_raportti.php?id=\''.$$link.'\')">'.$$place.'</td>';
		echo '<td width="20%" align="left"><a href="#'.$$link.'" onclick="loadContent(\'#content\', \'matcht_raportti.php?id=\''.$$link.'\')">'.$$home.'</td>';
		echo '<td width="20%" align="left"><a href="#'.$$link.'" onclick="loadContent(\'#content\', \'matcht_raportti.php?id=\''.$$link.'\')">'.$$away.'</td>';
		echo '<td align="center"><a href="#'.$$link.'" onclick="loadContent(\'#content\', \'matcht_raportti.php?id=\''.$$link.'\')">'.$$score.'</td>';
		echo '</tr>';
	}

?>

 

As you can see it's not wise to fetch the xml nodes one by one manually. How could I get all the data from the xml in one loop? So how should this part below be?

$linkki1 	= $XMLdata->linkki[0];
$paiva1 	= $XMLdata->paiva[0];
$kello1 	= $XMLdata->kello[0];
$paikka1 	= $XMLdata->paikka[0];
$ottelu1 	= $XMLdata->ottelu[0];
list($koti1,$vieras1) = explode ( ' - ' , $ottelu1 ) ;
$tulos1 	= $XMLdata->tulos[0];

 

If there's anything else you would change I'm all ears...  :D  I know the for-loop isn't too classy either but at least it works. I'm open for suggestions regarding to that too.

Link to comment
Share on other sites

Hehe, found an easier way to do this - though it required a small change in my xml too.

 

<?php
	// load xml for the match details
	$xml = 'matches.xml';
	echo "xml loaded: $xml <br /><br />"; //debug

	$dom = new DOMDocument;
	$dom->load($xml);
	if (!$dom) {
		echo 'Error while parsing the XML ($xml)';
		exit;
	}

	// get the data from xml to the variable $XMLdata
	$XMLdata = simplexml_import_dom($dom);

	foreach($XMLdata->matchdetails as $matchdetails)
	{
		$link 	= $matchdetails->link;
		$date 	= $matchdetails->date;
		$time 	= $matchdetails->time;
		$place	= $matchdetails->place;
		$match	= $matchdetails->match;
		list($home,$away) = explode ( ' - ' , $match );
		$score	= $matchdetails->score;

		echo '<tr>';
		echo '<td width="20%" align="right"><a href="#'.$link.'" onclick="loadContent(\'#content\', \'ottelut_raportti.php?id=\''.$link.'\')">'.$date.'</a></td>';
		echo '<td width="8%" align="center"><a href="#'.$link.'" onclick="loadContent(\'#content\', \'ottelut_raportti.php?id=\''.$link.'\')">'.$time.'</td>';
		echo '<td width="20%" align="left"><a href="#'.$link.'" onclick="loadContent(\'#content\', \'ottelut_raportti.php?id=\''.$link.'\')">'.$place.'</td>';
		echo '<td width="20%" align="left"><a href="#'.$link.'" onclick="loadContent(\'#content\', \'ottelut_raportti.php?id=\''.$link.'\')">'.$home.'</td>';
		echo '<td width="20%" align="left"><a href="#'.$link.'" onclick="loadContent(\'#content\', \'ottelut_raportti.php?id=\''.$link.'\')">'.$away.'</td>';
		echo '<td align="center"><a href="#'.$link.'" onclick="loadContent(\'#content\', \'ottelut_raportti.php?id=\''.$link.'\')">'.$score.'</td>';
		echo '</tr>';
	}

?>
</table>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.