Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/129662-generating-500-ramdon-numbers/
Share on other sites

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).

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

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..

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.

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");

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.