Using javascript to display a random game?

Discussion in 'JavaScript' started by ForgottenCreature, May 2, 2006.

  1. #1
    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.
     
    ForgottenCreature, May 2, 2006 IP
  2. dexfantasy

    dexfantasy Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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
     
    dexfantasy, May 3, 2006 IP
  3. ForgottenCreature

    ForgottenCreature Notable Member

    Messages:
    7,473
    Likes Received:
    173
    Best Answers:
    0
    Trophy Points:
    260
    #3
    All games are in a mysql database, not in shockwave, nor flash.
    Most are just in text.
     
    ForgottenCreature, May 3, 2006 IP
  4. dexfantasy

    dexfantasy Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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>
    Code (markup):
    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
     
    dexfantasy, May 3, 2006 IP
  5. ForgottenCreature

    ForgottenCreature Notable Member

    Messages:
    7,473
    Likes Received:
    173
    Best Answers:
    0
    Trophy Points:
    260
    #5
    There's about 700 games in the database.

    No, the following information is stored in the mysql database:
    id, sitename, url, linktext, category, listed, and pgview
     
    ForgottenCreature, May 3, 2006 IP
  6. dexfantasy

    dexfantasy Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    dexfantasy, May 4, 2006 IP
  7. ForgottenCreature

    ForgottenCreature Notable Member

    Messages:
    7,473
    Likes Received:
    173
    Best Answers:
    0
    Trophy Points:
    260
    #7
    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
     
    ForgottenCreature, May 4, 2006 IP
  8. dexfantasy

    dexfantasy Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    dexfantasy, May 4, 2006 IP
  9. ForgottenCreature

    ForgottenCreature Notable Member

    Messages:
    7,473
    Likes Received:
    173
    Best Answers:
    0
    Trophy Points:
    260
    #9
    Thanks man, appreciate the help from everyone that has posted on this thread.
     
    ForgottenCreature, May 4, 2006 IP