iPhone Coding Tutorial – Creating an Online Leaderboard For Your Games3

Displaying The Scores Table

As mentioned before, we will be displaying a table on a webpage. The url that the webview will access will be something like this

http://icodeblog.com/ws/get_scores.php?sort=score%20DESC

There are quite a few parameters that can be used with my script that will determine how the table will be displayed. The parameters and their descriptions are in the table below:

VariableDescription
typeThis value will determine what data the table will display. The possible values for it are global, device, and name. Global will simply show the global high scores list containing all of the users. Device is specific to the calling users device. It will only display high scores for a given udid. Finally, we have name, which will only display high scores for a given username.
offsetUsed for paging the SQL results. It will be the first parameter in the ORDER BY clause of the SQL select statement. Ex: An offset of 10 will tell the system to show records starting with the 10th.
countUsed for paging the SQL results. It will be the second parameter in the ORDER BY clause of the SQL select statement. This is basically the number of results to show in the table.
sortTells the table how to sort. If this isn’t specified, the sorting is default set to “score DESC”. This will order by scores from highest to lowest.
udidThe UDID of the device viewing the leaderboard. This variable is required if you specify type=device
nameThe name of the device viewing the leaderboard. This variable is required if you specify type=name

As you can see, you have quite a bit of control over how the results get displayed. You may want to offer multiple high score views to your user (global, personal, etc…). You can also fancy this up quite a bit and give the users the ability to view the high scores of other users on the list.

Here is the code for get_scores.php

<?php
    // get_scores.php
	/** MySQL database name */
	define('DB_NAME', '');
	/** MySQL database username */
	define('DB_USER', '');
	/** MySQL database password */
	define('DB_PASSWORD', '');
	/** MySQL hostname */
	define('DB_HOST', $_ENV{DATABASE_SERVER});
 
	$table = "highscores";
 
	// Initialization
	$conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
	mysql_select_db(DB_NAME, $conn);
 
	// Error checking
	if(!$conn) {
		die('Could not connect ' . mysql_error());
	}
 
	$type   = isset($_GET['type']) ? $_GET['type'] : "global";
	$offset = isset($_GET['offset']) ? $_GET['offset'] : "0";
	$count  = isset($_GET['count']) ? $_GET['count'] : "10";
	$sort   = isset($_GET['sort']) ? $_GET['sort'] : "score DESC";
 
	// Localize the GET variables
	$udid  = isset($_GET['udid']) ? $_GET['udid'] : "";
	$name  = isset($_GET['name']) ? $_GET['name']  : "";
 
	// Protect against sql injections
	$type   = mysql_real_escape_string($type);
	$offset = mysql_real_escape_string($offset);
	$count  = mysql_real_escape_string($count);
	$sort   = mysql_real_escape_string($sort);
	$udid   = mysql_real_escape_string($udid);
	$name   = mysql_real_escape_string($name);
 
	// Build the sql query
	$sql = "SELECT * FROM $table WHERE ";
 
	switch($type) {
		case "global":
			$sql .= "1 ";
			break;
		case "device":
			$sql .= "udid = '$udid' ";
			break;
		case "name":
			$sql .= "name = '$name' ";
			break;
	}
 
	$sql .= "ORDER BY $sort ";
	$sql .= "LIMIT $offset,$count ";
 
	$result = mysql_query($sql,$conn);
 
	if(!$result) {
		die("Error retrieving scores " . mysql_error());
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
	</head>
	<body>
		<?php
			// Display the table	
			echo '<table style="width:100%">
					<thead>
						<tr>
							<th>Name</th>
							<th>Score</th>
						</tr>
					</thead>
					<tbody>';
		    while ($row = mysql_fetch_object($result)) {
				echo '<tr>
						<td>
						'.$row->name.'
						</td>
						<td>
						'.$row->score.'
						</td>
					  </tr>';
			}
			echo '</tbody>
				</table>';	
			mysql_free_result($result);
			mysql_close($conn);
		?>
	</body>
</html>

 

After initialization, we build the SQL query based on the parameters that were specified. This will determine what data will get displayed. Notice that after the code to query the database, there is some HTML. This is because we want to output a full HTML page for our table. You can use this opportunity to ad things such as styling, images, and advertising.

Within the body, we simply loop over the results returned from MYSQL and output the score data. And that’s it! You now have a fully functional high score server.

With our server complete, there are only two things left to do: submit score data in our iphone application and display the leaderboard.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值