javascript - PHP not generating random number on Ajax reload -
for web project i've been developing, need surf through xml file, grab simplexml objects , store them in array, random simplexml object , print info it.
i've got part finished, , works - when load page itself. when try load in html page through jquery's $('#div').load('somescript.php'); method, end getting same results, dus php's rand() yielding 0.
what problem here? running exact same script in own browser tab give me random results (which want), loading inside page jquery , ajax gives me same result.
what's causing problem here?
here code if helps:
getbanddata.php
<?php $chosengenre = "edm"; $alreadyloadedbands = false; // set true if we've loaded array of bands show user. $bandstoshow = array(); // final array of bands show user. $xmlroot = simplexml_load_file('artists.xml'); // root of xml queries. if ($alreadyloadedbands == false){ // if bands haven't been loaded array... foreach($xmlroot->band $thisband){ // iterate through xml database, looking @ bands. if ($thisband->genre == $chosengenre){ // if band's genre , user's chosen genre match up... array_push($bandstoshow, $thisband); // add current band array of bands at. } } $alreadyloadedbands = true; // "okay, we've loaded bands. no need again." } $randomband = rand(0, count($bandstoshow) - 1); // set $randomband random integer between 0 , amount of bands we've found. // set $bandimlookingat's indexes bunch of data band in $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>
try setting
$.ajaxsetup ({ // disable caching of ajax responses cache: false }); or tape on random number getbanddata.php?{{random}}
Comments
Post a Comment