Reputation: 985
For a web project I've been developing, I need to surf through an XML file, grab some SimpleXML objects and store them in an array, then get a random SimpleXML object and print some info about it.
I've got that part finished, and it works - but only when I load the page by itself.
When I try to load it in another HTML page through jQuery's $('#div').load('somescript.php');
method, I always end up getting the same results, dus to PHP's rand()
always yielding 0.
What could be the problem here? Running the exact same script in its own browser tab by itself will give me random results (which is what I want), but loading it inside another page with jQuery and Ajax gives me the same result.
What's causing the problem here?
Here is my code if it helps:
GetBandData.php
<?php
$ChosenGenre = "edm";
$AlreadyLoadedBands = False; // Set to True if we've already loaded the array of bands to show the user.
$BandsToShow = array(); // The final array of bands to show the user.
$XMLRoot = simplexml_load_file('artists.xml'); // The root of all XML queries.
if ($AlreadyLoadedBands == False){ // If the bands haven't already been loaded into an array...
foreach($XMLRoot->band as $ThisBand){ // Iterate through the XML database, looking at all the bands.
if ($ThisBand->genre == $ChosenGenre){ // If the band's genre and the user's chosen genre match up...
array_push($BandsToShow, $ThisBand); // Add the current band to the array of bands to look at.
}
}
$AlreadyLoadedBands = True; // "Okay, we've already loaded the bands. No need to do it again."
}
$RandomBand = rand(0, count($BandsToShow) - 1); // Set $RandomBand as a random integer between 0 and the amount of bands we've found.
// Set $BandImLookingAt's indexes to a bunch of data about the band in the $RandomBand-th index of $BandsToShow.
$BandImLookingAt = array(
"BandName" => $BandsToShow[$RandomBand]->name,
"BandSafeName" => $BandsToShow[$RandomBand]->codesafe_name,
);
echo "<pre>";
var_dump($BandImLookingAt);
echo "</pre>";
?>
artists.xml
<root>
<band>
<genre>edm</genre>
<name>Chainsaw Police</name>
<codesafe_name>chainsawpolice</codesafe_name>
</band>
<band>
<genre>edm</genre>
<name>Guerrilla Warfare</name>
<codesafe_name>guerrillawarfare</codesafe_name>
</band>
<band>
<genre>edm</genre>
<name>SPENDV</name>
<codesafe_name>spenda</codesafe_name>
</band>
<band>
<genre>edm</genre>
<name>Byzanite</name>
<codesafe_name>byzanite</codesafe_name>
</band>
<band>
<genre>edm</genre>
<name>Hawf</name>
<codesafe_name>hawf</codesafe_name>
</band>
</root>
index.html
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<script type="text/javascript">
function recp() {
$('#myStyle').load('GetBandData.php');
}
</script>
<a href="#" onClick="recp()" >One</a>
<a href="#" onClick="recp()" >Two</a>
<a href="#" onClick="recp()" >Three</a>
<div id='myStyle'>
</div>
Upvotes: 0
Views: 797
Reputation: 40639
Try this:
function recp() {
$('#myStyle').load('GetBandData.php?_'+Math.random());
}
Upvotes: 1
Reputation: 24645
try setting
$.ajaxSetup ({
// Disable caching of AJAX responses
cache: false
});
or tape on a random number to the GetBandData.php?{{random}}
Upvotes: 1