Jump to content

SimpleXML- Multiple files on a single PHP Page


dswain

Recommended Posts

I have a page which is I need to load multiple XML files in and then output certain data to. Using simplexml_load_file works fine for either of the streams (there are two in total so far) but they both cannot be used at the same time.

 

<?php
#Rewriting this Last.fm feed handler with SimpleXML. So much easier... it's asinine.
include ("header.php");
#Setting up some vars.
$username = 'dswain';
$total_items = 6;
if (!isset($counter)) {
	resetCounter();
}

function resetCounter() {
	$counter = 0;
}
?>

<html>
<head>
<title><?php echo $username . "'s last.fm data"; ?></title>
</head>
<body>
<table width='65%' align='center'>
<tr><td align='left'><h1>Top Artists</h1></td></tr>

<td>
<table width='50%' align='left'>
	<tr>
		<td><b>Artist</b></td>
		<td><b>Playcount</b></td>
		<td align='center'><b>URL<b></td>
	</tr>
	<?php
		$top_artists = simplexml_load_file("http://ws.audioscrobbler.com/1.0/user/$username/weeklyartistchart.xml");
		while ($counter != $total_items) {
			echo "<tr>";
			echo "<td>" . $top_artists->artist[$counter]->name . "</td>";
			echo "<td align='center'>" . $top_artists->artist[$counter]->playcount . "</td>";
			echo "<td>" . $top_artists->artist[$counter]->url . "</td>";
			echo "</tr>";
			$counter++;
		}
	?>
</table>
</td>

<td>
<table width='50%' align='right'>
	<tr>
		<td><b>Artist</b></td>
		<td><b>Song</b></td>
	</tr>
	<?php
		resetCounter();
		$recent_tracks = simplexml_load_file("http://ws.audioscrobbler.com/1.0/user/$username/recenttracks.xml");
		while ($counter != $total_items) {
			echo "<tr>";
			echo "<td>" . $recent_tracks->track[$counter]->artist . "</td>";
			echo "<td>" . $recent_tracks->track[$counter]->name . "</td>";
			echo "</tr>";
			$counter++;
		}
	?>
</table>
</td>

</table>
</body>
</html>

 

Basically, if I comment out one of the sections where it is trying to open the XML file, the other one will work fine, but not both at the same time. It seems like I would need to unload the simplexml data and then open up the new file, but I couldn't find any particular methods to do this in the PHP docs. http://swainnet.zapto.org/rss_more.php is the actual page which this code is being run from. Thanks in advanced, and sorry if this isn't very clear. Kind of awkward for me to explain.

Link to comment
Share on other sites

Programming languages have something called scope which distates how long variables live and how you can access them. Because of PHP's structure if you create a variable anywhere in the page it is available down through the rest of the page.

 

However, there is function scope which means that the variables in a function exist only inside the function and variables outside it are not involved.

 

So, your function:

function resetCounter() {
	$counter = 0;
}

Creates the variable $counter inside the function but does nothing else.

 

What you need to do is tell PHP that you want to use a global variable inside the function and then set it to 0:

function resetCounter() {
     global $counter;	
                $counter = 0;
}

 

I hope that rather detailed explanation helps.

Link to comment
Share on other sites

Oh man, wow I'm sorry I'm an idiot. The thought didn't even occur to me to globalize the $counter variable. Talk about a duh moment! Thanks a lot!

 

By the way, that was a very good explanation. I did know about globalizing variables but I wouldn't have thought to have called it the scope of the variable. Again, wow haha a real duh moment. Looks good now =)

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.