unknown87 Posted October 22, 2008 Share Posted October 22, 2008 Hi, I am making a php game from scratch and I am stuck on this one thing: I need to generate 500 ramdon numbers and then store them in a database. Then I need to echo all the numbers back to the browser - one by one every 30 seconds untill all 500 numbers have been displayed to the user. Could someone guide me through on how I can achieve this? Thank in advance Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/ Share on other sites More sharing options...
Barand Posted October 22, 2008 Share Posted October 22, 2008 I'd use PHP to write a javascript array, then use JS set_interval() function to display them. But I'm not going to sit here for over 4 hours to test it. Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-672287 Share on other sites More sharing options...
R4nk3d Posted October 22, 2008 Share Posted October 22, 2008 im just pullin this out my ass. for($i = 0; i < 500; i++) { $num = rand(1,9999); print $num; $sql = "INSERT INTO `table` SET (number) VALUES ('$num')"; $result = mysql_query($sql,$connection) or die('Error :'.mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-672296 Share on other sites More sharing options...
Barand Posted October 22, 2008 Share Posted October 22, 2008 syntax is either INSERT INTO `table` (number) VALUES ('$num') or INSERT INTO `table` SET number = '$num' Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-672303 Share on other sites More sharing options...
neromir Posted October 22, 2008 Share Posted October 22, 2008 I'd use PHP to write a javascript array, then use JS set_interval() function to display them. But I'm not going to sit here for over 4 hours to test it. That's probably how I would do it-- it would be a good way to get the 30 seconds between numbers. (Not to mention I can't think of another good way to get new data to the page every 30 seconds). Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-672319 Share on other sites More sharing options...
R4nk3d Posted October 23, 2008 Share Posted October 23, 2008 syntax is either INSERT INTO `table` (number) VALUES ('$num') or INSERT INTO `table` SET number = '$num' sorry, im having such a bad day today. silliest mistake ive ever seen. thanks for correcting Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-672429 Share on other sites More sharing options...
unknown87 Posted October 23, 2008 Author Share Posted October 23, 2008 Could someone please help me with this error ---> Parse error: syntax error, unexpected T_INC, expecting ')' in C:\wamp\www\test.php on line 97 for($i = 0; i < 300; i++) <-------------- line 97 { $num = rand(1,9999); echo $num; $sql = INSERT INTO test (number) VALUES ('$num'); if (!$result) { die("Database query failed: " . mysql_error()); } Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-673019 Share on other sites More sharing options...
GKWelding Posted October 23, 2008 Share Posted October 23, 2008 check the line above for a missing ; or somethign similar... Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-673028 Share on other sites More sharing options...
Barand Posted October 23, 2008 Share Posted October 23, 2008 $i++ Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-673029 Share on other sites More sharing options...
DeanWhitehouse Posted October 23, 2008 Share Posted October 23, 2008 And $i < 300 Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-673037 Share on other sites More sharing options...
discomatt Posted October 23, 2008 Share Posted October 23, 2008 If you want to keep the numbers away from the user's eyes during execution, or to have the 'exposure' time identical among multiple clients, javascript is not the ideal solution. Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-673044 Share on other sites More sharing options...
unknown87 Posted October 23, 2008 Author Share Posted October 23, 2008 Thank you all for helping Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-673049 Share on other sites More sharing options...
unknown87 Posted October 23, 2008 Author Share Posted October 23, 2008 If you want to keep the numbers away from the user's eyes during execution, or to have the 'exposure' time identical among multiple clients, javascript is not the ideal solution. What do you suggest? Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-673052 Share on other sites More sharing options...
thefollower Posted October 23, 2008 Share Posted October 23, 2008 If you want to keep the numbers away from the user's eyes during execution, or to have the 'exposure' time identical among multiple clients, javascript is not the ideal solution. What do you suggest? You do a php script like: load first number (add 1 increment to a table and store timestamp) make a function to check 30 seconds has passed from timestamp if so .. load increase+1 and update timestamp and repeat until increment == 500 this won't be automated how ever you could then use something like cron to run such a script or a script that will run the php file whilst keeping the numbers coming out server side.. Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-673060 Share on other sites More sharing options...
DeanWhitehouse Posted October 23, 2008 Share Posted October 23, 2008 Ajax? Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-673061 Share on other sites More sharing options...
discomatt Posted October 23, 2008 Share Posted October 23, 2008 I would store the numbers in a database/file outside of webroot... The most basic way to do this would be to store the number beside a timestamp... incrementing by 30 seconds. Use a meta refresh to force the user to refresh the page every 30 seconds, then poll the database for any numbers where the timestamp is later than NOW(). Display results. You could use ajax along with this to save a bit of bandwidth, or to get around people who've disabled meta refresh. Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-673069 Share on other sites More sharing options...
The Little Guy Posted October 23, 2008 Share Posted October 23, 2008 JavaScript: function ajaxPost(){ var contentType = "application/x-www-form-urlencoded; charset=UTF-8"; var ajaxRequest; try{ ajaxRequest = new XMLHttpRequest(); } catch (e){ try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ alert("Your Browser Doesn't support AJAX."); return false; } } } return Array(ajaxRequest,contentType); } function myFunction(){ connect = ajaxPost(); // Get our values from the above function and save them in "connect" connect[0].onreadystatechange = function(){ //A ready state changed if(connect[0].readyState == 4){ // Ready state equals 4, request was sent back. document.getElementById('myDiv').innerHTML = connect[0].responseText; // Place the returned text in a div } } var va = '';//'value1='+val1+'&value2='+val2; // Create some post variables connect[0].open("POST", '/myFile.php', true); // Open a connection to a file (ext stats for extension) connect[0].setRequestHeader("Content-Type", connect[1]); // Set our header type (from previous function) connect[0].send(va); // send our POST values. } set_interval("myFunction()",30000); Html: <div id="myDiv"></div> myFile.php: echo date("g:i:s"); Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-673080 Share on other sites More sharing options...
R4nk3d Posted October 24, 2008 Share Posted October 24, 2008 Thank you all for helping Glad to help. sorry for the errors in my post. haha. was really tired. Quote Link to comment https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/#findComment-673392 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.