0 Favourites

Scores on website

  • Is there any way to save scores on my website?

  • Yes there is - I had a similar problem a couple hours ago.

    Check out the ajax tutorial here:

    http://www.scirra.com/tutorials/61/ajax-example-with-construct-2/page-1

    Say you wanted to request savescores.php - you would need two pass two values in - one being the username for identification purposes, and the second being their score.

    The PHP would look something like:

    <?php
    
    /*
         You will need to create the database, then create the "scores" table by running the SQL query below.
         
         CREATE TABLE `scores` (
         `id` INT NOT NULL AUTO_INCREMENT ,
         `name` VARCHAR( 20 ) NOT NULL ,
         `score` INT NOT NULL ,
         PRIMARY KEY ( `id` )
         ) ENGINE = InnoDB;
    
    */
    
    $db = "your_database";//Your database name
    $dbu = "yourdb_user";//Your database username
    $dbp = "yourpassword";//Your database users' password
    $host = "localhost";//MySQL server - usually localhost
    
    $dblink = mysql_connect($host,$dbu,$dbp);
    $seldb = mysql_select_db($db);
    
    if(isset($_GET['name']) && isset($_GET['score'])){
    
         //Lightly sanitize the GET's to prevent SQL injections and possible XSS attacks
         $name = strip_tags(mysql_real_escape_string($_GET['name']));
         $score = strip_tags(mysql_real_escape_string($_GET['score']));
         $sql = mysql_query("INSERT INTO `$db`.`scores` (`id`,`name`,`score`) VALUES ('','$name','$score');");
         
         if($sql){
         
              //The query returned true - now do whatever you like here.
              echo 'Your score was saved. Congrats!';
              
         }else{
         
              //The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
              echo 'There was a problem saving your score. Please try again later.';
              
         }
         
    }else{
         echo 'Your name or score wasnt passed in the request. Make sure you add ?name=NAME_HERE&score=1337 to the tags.';
    }
    
    mysql_close($dblink);//Close off the MySQL connection to save resources.
    ?>
    
    

    EDIT: To retrieve the scores, just run mysql_fetch_array (or object, depending on your preference) and loop through the returned results. It's up to you for how you want them displayed. I suggest doing an ORDER BY `scores`.`score` DESC LIMIT 10 query so you can do a "Top 10" leaderboard.

  • I have a simple scoring system in the test game I created along with the tutorial. I'm going to notate how to add a scoring system that will expand the tutorial a bit. Since the physics stuff came out, I'd like to update the game too.

    The reality is that it's mostly PHP that does the bulk of the work for you.

  • How can the PHP be created from the game?

  • How can the PHP be created from the game?

    Daniel,

    The php page is created on the server. It is already made.

    @Pilfer, thanks for posting that code snippet.

  • Hi Ghengis

    I am a complete novice in programing. but have be strugling with

    a highscore/top10

    Could you PLEASE tell how its done, or place a sample xcap

    <img src="smileys/smiley1.gif" border="0" align="middle" />

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • Hi Ghengis

    I am a complete novice in programing. but have be strugling with

    a highscore/top10

    Could you PLEASE tell how its done, or place a sample xcap

    <img src="smileys/smiley1.gif" border="0" align="middle" />

    I've been really busy with my day job but have started cobbling together an example. I know there was another member who provided an example as well. If I get a couple of hours in the next week I will post a tutorial.

  • 1000 thanks .... Will be so glad

    Have a Nice weekend

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
178 43,843
h1k3's avatar
h1k3

You do not have permission to view this topic

Unread hot topic
39 14,077
norekbeat's avatar
norekbeat