<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iPhone Programming Tutorials &#187; leaderboard</title>
	<atom:link href="http://www.icodeblog.com/tag/leaderboard/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.icodeblog.com</link>
	<description>Conquering the mobile universe</description>
	<lastBuildDate>Tue, 06 Dec 2011 23:02:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>iPhone Coding Tutorial â€“ Creating an Online Leaderboard For Your Games</title>
		<link>http://www.icodeblog.com/2009/10/29/iphone-coding-tutorial-creating-an-online-leaderboard-for-your-games/</link>
		<comments>http://www.icodeblog.com/2009/10/29/iphone-coding-tutorial-creating-an-online-leaderboard-for-your-games/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 16:14:50 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iPhone Coding]]></category>
		<category><![CDATA[iphone game]]></category>
		<category><![CDATA[leaderboard]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1426</guid>
		<description><![CDATA[As you may have seen, there are quite a few services out there offering free leaderboards.  These are great and all, but sometimes you want to have complete control over your data.  I have put together a complete tutorial detailing step by step how you can create your own online leaderboard.  This will also give you a very simple introduction to interfacing with web services.]]></description>
			<content:encoded><![CDATA[<p>As you may have seen, there are quite a few services out there offering free leaderboards. Â These are great and all, but sometimes you want to have complete control over your data. Â I have put together a complete tutorial detailing step by step how you can create your own online leaderboard for use in your iPhone games.  For those of you who don&#8217;t know, a leaderboard is essentially a high scores list.  This tutorial will also give you a very simple introduction to interfacing with web services.</p>
<p>The first part of this tutorial will discuss how to build the server. Â We will be doing this using PHP and MYSQL. Â I will also assume you have some working knowledge of PHP and MYSQL (if not, use the Google machine to get up to speed).</p>
<p>Since this tutorial is so long, I have decided to split it up into pages.  You will notice the page number at the bottom.  Please use them to navigate between parts of this post.  I feel that I have to make this explicit as I will undoubtably get some dude in the comments saying &#8220;Where is the rest of the tutorial&#8221;.</p>
<p>Skill level: <font color="red">Advanced</font></p>
<h2>Creating The Database</h2>
<p>When creating a leaderboard, you will need some way of storing the data. Â We will be using MYSQL for our storage. Â You can also be lame and simply use flat files. Â This would add quite a bit of code in the long run as you would have to write all of the sorting and pagination functionality yourself. Don&#8217;t do it.</p>
<p>One thing to note about my php server files.  I know they could be cleaned up a little and optimized (you could create a config.php that contains all the db interface code), but the goal of this tutorial is not to show you how to code killer PHP.  It&#8217;s to show you how to create code that you can connect your iPhone apps to.</p>
<p>I like to create one file that does a complete flash of the database. Â That way, when I&#8217;m testing or switch from staging to production, it is a very simple process. Â So, with that being said, here is my code for create_db.php.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
        <span style="color: #666666; font-style: italic;">// create_db.php</span>
	<span style="color: #009933; font-style: italic;">/** MySQL database name */</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_NAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009933; font-style: italic;">/** MySQL database username */</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_USER'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009933; font-style: italic;">/** MySQL database password */</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_PASSWORD'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009933; font-style: italic;">/** MySQL hostname */</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_HOST'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_ENV</span><span style="color: #009900;">&#123;</span>DATABASE_SERVER<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$table</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;highscores&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Initialization</span>
	<span style="color: #000088;">$conn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span>DB_HOST<span style="color: #339933;">,</span>DB_USER<span style="color: #339933;">,</span>DB_PASSWORD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span>DB_NAME<span style="color: #339933;">,</span> <span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Error checking</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Could not connect '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Drop existing table if exists</span>
	<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DROP TABLE <span style="color: #006699; font-weight: bold;">$table</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$retval</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;CREATE TABLE <span style="color: #006699; font-weight: bold;">$table</span>(
		id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
		udid VARCHAR(45),
		name VARCHAR(25),
		score FLOAT(10,2),
		date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
	)&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$retval</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Database created...&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Could not create database &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>I&#8217;m not going to go into too much detail about this code, however I will give a high level description of what&#8217;s going on.  One thing to note about this code is it assumes you already have the database created.</p>
<p>First, we are defining variables that contain our database information.  Make sure you replace my empty strings with your database info. After we connect to the database, we drop the table if it already exists.  This is useful if you want to wipe out your test data.  Finally, we create the scores table.  Navigate to this file in your browser to run it and create the scores table.  Pretty easy right?</p>
<p><font color="red">You will want to make sure to remove this file from your server after running it to avoid people resetting your database.</font></p>
<p>Now that our database table has been created, it&#8217;s time to implement the web service code to publish new scores to our leaderboard.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.icodeblog.com/2009/10/29/iphone-coding-tutorial-creating-an-online-leaderboard-for-your-games/feed/</wfw:commentRss>
		<slash:comments>47</slash:comments>
		</item>
	</channel>
</rss>

