Building Browsergames: displaying a user’s stats (PHP)

So we added stats to our registration process, but we haven’t actually added anything to show players what their stats are – that’s what we’ll be doing today.

One thing that almost every browsergame has(but ours is missing right now) is some sort of currency – whether’s that gold, lira, or something else entirely. So we’re going to add that to our stats table first:

INSERT INTO stats(display_name,short_name) VALUES ('Gold','gc');

Now that that’s done, we can modify the template for our index page to display a user’s stats for them – their Attack, Defence, Magic, and Gold. Here’s the modifications we’ll make to our template file:

6
7
8
9
10
11
	<ul>
		<li>Attack: <strong>{$attack}</strong></li>
		<li>Defence: <strong>{$defence}</strong></li>
		<li>Magic: <strong>{$magic}</strong></li>
		<li>Gold in hand: <strong>{$gold}</strong></li>
	</ul>

And then all we’ll do is modify our index.php file so that it retrieves those stats for the player, and displays them in our template. The first thing that we will need to do in that case is retrieve the players UserID based on the player’s username, which our login page stored into $_SESSION. We can safely use the player’s username to retrieve their ID because our registration page makes sure that usernames are unique – which means there will only be one username per user ID, and we won’t need to worry about retrieving too many rows when we only want one. Here’s the code we’d use to retrieve the user ID:

14
15
16
17
18
19
20
21
22
 
require_once 'config.php';		// our database settings
$conn = mysql_connect($dbhost,$dbuser,$dbpass)
	or die('Error connecting to mysql');
mysql_select_db($dbname);
$query = sprintf("SELECT id FROM users WHERE UPPER(username) = UPPER('%s')",
			mysql_real_escape_string($_SESSION['username']));
$result = mysql_query($query);
list($userID) = mysql_fetch_row($result);

And now that we have the proper User ID, we can display their stats:

23
24
25
26
27
28
 
require_once 'stats.php';
$smarty->assign('attack',getStat('atk',$userID));
$smarty->assign('magic',getStat('mag',$userID));
$smarty->assign('defence',getStat('def',$userID));
$smarty->assign('gold',getStat('gc',$userID));

And, if you log in and visit your index page, you should see the four stat values – Attack, Defence, and Magic should all be 5, and Gold will be 0 – because we never set it anywhere. If you’d like, you can customize your registration page to give your player some starting gold as well – just add it below the code that sets up the other stats for the player.

Here’s what the code behind our index page looks like now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
 
require_once 'smarty.php';
 
session_start();
$smarty->assign('name', $_SESSION['username']);
 
require_once 'config.php';		// our database settings
$conn = mysql_connect($dbhost,$dbuser,$dbpass)
	or die('Error connecting to mysql');
mysql_select_db($dbname);
$query = sprintf("SELECT id FROM users WHERE UPPER(username) = UPPER('%s')",
			mysql_real_escape_string($_SESSION['username']));
$result = mysql_query($query);
list($userID) = mysql_fetch_row($result);
 
require_once 'stats.php';
$smarty->assign('attack',getStat('atk',$userID));
$smarty->assign('magic',getStat('mag',$userID));
$smarty->assign('defence',getStat('def',$userID));
$smarty->assign('gold',getStat('gc',$userID));
 
$smarty->display('index.tpl');
 
?>

And that’s all there is to it! Next time we’ll start adding in some of the buildings that we mentioned in our brief design document from earlier.

Wish there was more?

I'm considering writing an ebook - click here.

.

Luke is the primary editor of Building Browsergames, and has written a large portion of the articles that you read here. He generally has no idea what to say when asked to write about himself in the third person.

Thursday, June 5th, 2008 buildingbrowsergames, code, php
  • elnomadico

    Figured it out! Thanks =)) AWESOME tutorials!

blog comments powered by Disqus

About

Building Browsergames is a blog about browsergames(also known as PBBG's). It's geared towards the beginner to intermediate developer who has an interest in building their own browsergame.

Sponsors

Got Something to Say?

Send an e-mail to luke@buildingbrowsergames.com, or get in touch through Twitter at http://twitter.com/bbrowsergames