Wordpress Themes - Find jobs - Wordpress Theme - Business in 2007 - Myspace Layouts

PDA

View Full Version : Using javascript to display a random game?


ForgottenCreature
May 2nd 2006, 11:01 pm
I want to know how to use javascript to display a random game, its coded in mysql, and php.

How could this be done? All I know about javascript is the document.write thing, so I'd love to know if someone could help me.

dexfantasy
May 3rd 2006, 8:19 am
ForgottenCreature,

Do you mean that you have a collection of games on your server and you want one of them to be selected from the collection? Your description is a little brief.

The simplest way display a random game that you have stored someplace on your server is to just use PHP to pull up a random game. You could for example, create an array of game locations and use the built in functions rand() and time() to seed and create a random index for the array.

If you’re looking for a bit more client-side flexibility… for example, say you wanted display a random game on the page in reaction to a user event without a page load… then javascript would come in handy. The implementation of this will vary depending on what format your games are in… Are they shockwave/flash/javascript/etc?

Dex

ForgottenCreature
May 3rd 2006, 10:30 am
All games are in a mysql database, not in shockwave, nor flash.
Most are just in text.

dexfantasy
May 3rd 2006, 11:31 am
I still don’t know what you mean. By “Game” do you mean just a glob of text/html stored in a mysql database?

Assuming the above is correct…

How many games are we talking about here? If there aren’t too many, you could just iterate over each record in your mysql database and echo a javascript array (using literal format for clarity) which can be read and presented with your script. For example,

<script>
//code generated by php
var games = ["this is a glob of text that represents game 1", "game 2", "game 3"];
//end of code generated by php

function showRandomGame( where ) {
document.getElementById(where).innerHTML = games[Math.floor(Math.random()*games.length)];
}
</script>

<input type="button" value="get random game" onclick="showRandomGame( 'mygame' );"/>
<div id="mygame"></div>

If there are a ton of games and you don’t want your users to have to wait for each one to load into your array, you will need to either: a. Force your user to refresh their browser to view a new random game (in which case you might as well just use PHP and no javascript), or b. Resort to a more sophisticated technique such as Ajax.

Dex

ForgottenCreature
May 3rd 2006, 9:54 pm
There's about 700 games in the database.



I still don’t know what you mean. By “Game” do you mean just a glob of text/html stored in a mysql database?
No, the following information is stored in the mysql database:
id, sitename, url, linktext, category, listed, and pgview

dexfantasy
May 4th 2006, 8:39 am
From the limited information you’ve given me about what you are looking for, I think this might be a better question for the PHP forum. Sending a user to a random location or loading a random game will be most straight-forward if done on the server-side… Unless you need some sort of client-side functionality which you have not described to me.

ForgottenCreature
May 4th 2006, 7:47 pm
Thanks man, I'll try that forum.

What I wanted to was use javascript for webmasters to put the code on their website and it would display a random game from my website

dexfantasy
May 4th 2006, 9:12 pm
This may not be the best approach, but what popped into my mind immediately was just a simple

<script src="http://path/to/js/file.php"></scipt>

If your "games" are just text, your file.php could simply grab a random record from your games database, parse every line in the file as a document.write([put line here...]), and be good to go.

ForgottenCreature
May 4th 2006, 9:32 pm
Thanks man, appreciate the help from everyone that has posted on this thread.