dswain Posted March 24, 2007 Share Posted March 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/44143-simplexml-multiple-files-on-a-single-php-page/ Share on other sites More sharing options...
ShogunWarrior Posted March 24, 2007 Share Posted March 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/44143-simplexml-multiple-files-on-a-single-php-page/#findComment-214357 Share on other sites More sharing options...
dswain Posted March 24, 2007 Author Share Posted March 24, 2007 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 =) Quote Link to comment https://forums.phpfreaks.com/topic/44143-simplexml-multiple-files-on-a-single-php-page/#findComment-214379 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.