<?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>Building Browsergames &#187; php</title>
	<atom:link href="http://buildingbrowsergames.com/category/code/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://buildingbrowsergames.com</link>
	<description>Ever wanted to build a browsergame?</description>
	<lastBuildDate>Mon, 29 Mar 2010 14:00:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Plant Wars: Postmortem</title>
		<link>http://buildingbrowsergames.com/2009/03/20/plant-wars-postmortem/</link>
		<comments>http://buildingbrowsergames.com/2009/03/20/plant-wars-postmortem/#comments</comments>
		<pubDate>Fri, 20 Mar 2009 14:00:05 +0000</pubDate>
		<dc:creator>plantwars</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[monetization]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[postmortem]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=795</guid>
		<description><![CDATA[This is Jon from Plant Wars, which is yet another PBBG (what else would I be doing here?). I started the game around 9 months ago as I was searching for a job after graduating with a bachelor&#8217;s in Computer Science. I had only learned a minimal amount of PHP for a class project earlier [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft" style="width: 273px"><img title="Plant Wars Logo" src="http://www.plantwars.com/images/logomedium.jpg" alt="Plant Wars Logo" width="263" height="332" /><p class="wp-caption-text">Plant Wars Logo</p></div>
<p>This is Jon from <a href="http://www.plantwars.com" target="_blank">Plant Wars</a>, which is yet another PBBG (what else would I be doing here?). I started the game around 9 months ago as I was searching for a job after graduating with a bachelor&#8217;s in Computer Science. I had only learned a minimal amount of PHP for a class project earlier in the year, so I definitely have used the process as a learning experience. Still, I&#8217;m pretty sure I can tell you more about what <strong>not</strong> to do than what <strong>to</strong> do. As with any web-based game, we are constantly developing and are far from being done.</p>
<p>First of all, set up a test site with its own test database. Keep it up to date so that you&#8217;re not tempted to cheat and just make this tiny change on the live site first. I cheated when I implemented the password change feature. The query it used when someone changed his password was &#8220;UPDATE Users SET Password=md5($newpassword)&#8221;. Notice something missing? That &#8220;WHERE Id=$_SESSION["Id"]&#8221; clause is just so easy to forget. That was a mess that would have been worth any level of inconvenience in maintaining the test site to avoid.</p>
<p>Secondly, I&#8217;d recommend using a framework, such as the <a href="http://framework.zend.com/" target="_blank">Zend Framework</a> for PHP. This is because I didn&#8217;t and still don&#8217;t. I don&#8217;t even have data access objects. Sure, I store some commonly used methods in files that I include on every page. Alas, I&#8217;m still in the habit of embedding queries in the pages directly. Separation of logic and presentation? Yeah, that&#8217;d be nice.. At my day job, I use the <a href="http://struts.apache.org/">Struts framework</a> with Java &#8211; and while it does make the initial development take somewhat longer in the case of Struts, it is definitely worth it. The maintainability is increased incredibly by the proper separation of concerns. Once you learn a framework, you should generally find that your productivity increases. The initial learning curve is worth the sacrifice at the beginning for the long term benefit.</p>
<p>Perhaps the most important thing I did right was to have my friend Daniel help me out whenever possible. Going at such an endless project alone is intimidating, and having someone else to shoulder some of the burden is essential. Coming home and seeing a new feature implemented that you didn&#8217;t have to lift a finger for is exhilarating. Plus, then you have someone to brainstorm with and to just talk with about the game. Your girlfriend (or mom, if that&#8217;s the case) may pretend to care, but she&#8217;s probably tired of hearing about it.</p>
<p>In the same vein, it&#8217;s essential to have some trustworthy staff members. Grant these people moderation powers on your game&#8217;s forum and give them the ability to check the logs for cheaters. Write a nice administration panel for them (and yourself) that will streamline the process of checking for cheaters. For example, one button click to see all users who have shared an IP address. Examining log files may be your thing, but it&#8217;s time that you should be spending developing. Ensure that your staff is &#8211; once again for emphasis &#8211; trustworthy, as well as good role models for your community.</p>
<p>If you have staff that you don&#8217;t know personally help on the site&#8217;s development, don&#8217;t give them access to the live database. Restrict them to the test site &#8211; another good reason for its existence. Merge their code yourself. I made the mistake of granting access to the live database to my first staff member. I got off easy when he just made a page to bump his stats up artificially.</p>
<p>My final recommendation is regarding monetization: if you would like to at least make enough cash to pay for your hosting costs (which we usually manage &#8211; if barely), ensure that people can donate for some advantage in your game. For example, with <a href="http://www.plantwars.com" target="_blank">Plant Wars</a>, donors gain fertilizer (which is spent to train, fight, etc) at twice the rate of non-donors. I charge a low price of $2/month, and I&#8217;m not sure if I would recommend going that low. My initial price was $5 and that received lots of complaints and no donors, but now that the game has progressed, I&#8217;m more inclined to believe that anyone who donates currently would be inclined to do so even if it were a couple bucks more expensive. Use your best judgment. Also, I allow anything that can be bought with real money to be sold player-to-player so that a) there is more motivation for people to give me real money and b) people who can&#8217;t give real money still have the opportunity to gain the same benefits with increased activity.</p>
<p>People hate clicking on ads. It is worth the minimal time investment necessary to sign up with some sort of pay-per-action network, such as <a href="http://www.cpalead.com/apply.php?ref=7364" target="_blank">CPALead</a> (disclosure: referral link). This allows players to fill out an obnoxious survey for an in-game reward, while you get some money.</p>
<p>Come check out the <a href="http://blog.plantwars.com" target="_blank">Plant Wars blog</a> and you can also <a href="http://twitter.com/plantwars" target="_blank">follow us on Twitter</a>! (As a related, post-final recommendation, open up communications from your game as much as possible. It increases the likelihood that someone will find you from a social networking site or a search engine.)</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2009/03/20/plant-wars-postmortem/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Building Browsergames: Buying Armor (PHP)</title>
		<link>http://buildingbrowsergames.com/2008/09/22/buying-armor-php/</link>
		<comments>http://buildingbrowsergames.com/2008/09/22/buying-armor-php/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 14:00:17 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=401</guid>
		<description><![CDATA[While we&#8217;ve built and integrated a weapons system into our game, we&#8217;re still missing something that (most) other games have &#8211; armor! Today we will be laying the initial groundwork down for our armor system.
To begin with, check out a copy of the source code for our tutorial:

svn checkout http://building-browsergames-tutorial.googlecode.com/svn/trunk/php/pbbg tutorial -r 26

With that done, [...]]]></description>
			<content:encoded><![CDATA[<p>While we&#8217;ve built and integrated a weapons system into our game, we&#8217;re still missing something that (most) other games have &#8211; armor! Today we will be laying the initial groundwork down for our armor system.</p>
<p>To begin with, check out a copy of the source code for our tutorial:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">svn checkout http://building-browsergames-tutorial.googlecode.com/svn/trunk/php/pbbg tutorial -r 26</pre></div></div>

<p>With that done, we&#8217;re ready to get started.</p>
<p>Based on our poll from earlier, the majority chose that there should be 5 armor slots &#8211; head, torso, legs, right arm, and left arm. That&#8217;s a lot of slots &#8211; but not very hard to add to our <em>stats</em> table:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span>short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Armor - Head'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'ahead'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Armor - Torso'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'atorso'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Armor - Legs'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'alegs'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Armor - Right Arm'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'aright'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Armor - Left Arm'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'aleft'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Item Armor Slot'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'aslot'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>You may have noticed that there&#8217;s an extra stat being added there &#8211; <strong>Item Armor Slot</strong>. This stat was added so that we can keep track of which armor slot an item should go in &#8211; we&#8217;ll take advantage of it later.</p>
<p>To begin with, we&#8217;ll need an Armor Shop page. We&#8217;ll start with a template, called <strong>armor-shop.tpl</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;The Armor Shop&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;p&gt;Welcome to the Armor Shop.&lt;/p&gt;
	&lt;p&gt;&lt;a href='index.php'&gt;Back to main&lt;/a&gt;&lt;/p&gt;
	&lt;h3&gt;Current Armor:&lt;/h3&gt;
	&lt;ul&gt;
		&lt;li&gt;
			Head:
			{if $ahead ne ''}
				{$ahead}
				&lt;form action='armor-shop.php' method='post'&gt;
					&lt;input type='hidden' name='sell' value='ahead' /&gt;
					&lt;input type='submit' value='Sell' /&gt;
				&lt;/form&gt;
			{else}
				None
			{/if}
		&lt;/li&gt;
		&lt;li&gt;
			Torso:
			{if $atorso ne ''}
				{$atorso}
				&lt;form action='armor-shop.php' method='post'&gt;
					&lt;input type='hidden' name='sell' value='atorso' /&gt;
					&lt;input type='submit' value='Sell' /&gt;
				&lt;/form&gt;
			{else}
				None
			{/if}
		&lt;/li&gt;
		&lt;li&gt;
			Legs:
			{if $alegs ne ''}
				{$alegs}
				&lt;form action='armor-shop.php' method='post'&gt;
					&lt;input type='hidden' name='sell' value='alegs' /&gt;
					&lt;input type='submit' value='Sell' /&gt;
				&lt;/form&gt;
			{else}
				None
			{/if}
		&lt;/li&gt;
		&lt;li&gt;
			Right Arm:
			{if $aright ne ''}
				{$aright}
				&lt;form action='armor-shop.php' method='post'&gt;
					&lt;input type='hidden' name='sell' value='aright' /&gt;
					&lt;input type='submit' value='Sell' /&gt;
				&lt;/form&gt;
			{else}
				None
			{/if}
		&lt;/li&gt;
		&lt;li&gt;
			Left Arm:
			{if $aleft ne ''}
				{$aleft}
				&lt;form action='armor-shop.php' method='post'&gt;
					&lt;input type='hidden' name='sell' value='aleft' /&gt;
					&lt;input type='submit' value='Sell' /&gt;
				&lt;/form&gt;
			{else}
				None
			{/if}
		&lt;/li&gt;
&nbsp;
	&lt;/ul&gt;
	&lt;p&gt;You may purchase any of the armor listed below.&lt;/p&gt;
	{if $error ne ''}
		&lt;p style='color:red'&gt;{$error}&lt;/p&gt;
	{/if}
	{if $message ne ''}
		&lt;p style='color:green'&gt;{$message}&lt;/p&gt;
	{/if}
	&lt;ul&gt;
		{foreach from=$armor key=id item=i}
			&lt;li&gt;
				&lt;strong&gt;{$i.name}&lt;/strong&gt; - &lt;em&gt;{$i.price} gold coins&lt;/em&gt;
				&lt;form action='armor-shop.php' method='post'&gt;
					&lt;input type='hidden' name='armor-id' value='{$i.id}' /&gt;
					&lt;input type='submit' value='Buy' /&gt;
				&lt;/form&gt;
		{/foreach}
	&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>If that code looks similar to you at all, that&#8217;s because it is &#8211; it&#8217;s essentially a copy of the weapon shop code. While I personally prefer to write templates for each individual piece of functionality, you could probably refactor the two templates into one &#8211; but I will leave that as an exercise for you, the reader.</p>
<p>There will probably be a lot more armor in our game than weapons &#8211; after all, there are 5 slots to fill with unique item types, as opposed to the single unique item type needed for weapons. We&#8217;ll retrieve a random list of 10 pieces of armor for our shop to display using this query:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">DISTINCT</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">,</span> price <span style="color: #993333; font-weight: bold;">FROM</span> items <span style="color: #993333; font-weight: bold;">WHERE</span> type <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Armor'</span> <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> RAND<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">10</span>;</pre></div></div>

<p>We&#8217;ve figured out how to retrieve the armor, <strong>and</strong> how to display it &#8211; so it&#8217;s time to write the logic that will actually do our heavy lifting for us. We&#8217;re going to keep all of our code inside <strong>armor-shop.php</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'smarty.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'config.php'</span><span style="color: #339933;">;</span>		<span style="color: #666666; font-style: italic;">// our database settings</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><span style="color: #000088;">$dbhost</span><span style="color: #339933;">,</span><span style="color: #000088;">$dbuser</span><span style="color: #339933;">,</span><span style="color: #000088;">$dbpass</span><span style="color: #009900;">&#41;</span>
	or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error connecting to mysql'</span><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><span style="color: #000088;">$dbname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// retrieve player's ID</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT id FROM users WHERE UPPER(username) = UPPER('<span style="color: #009933; font-weight: bold;">%s</span>')&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'stats.php'</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// player stats</span>
&nbsp;
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT DISTINCT(id), name, price FROM items WHERE type = 'Armor' ORDER BY RAND() LIMIT 10;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$armor</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$armor</span><span style="color: #339933;">,</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$stats</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'atorso'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'ahead'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'alegs'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'aright'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'aleft'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$stats</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT name FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'armor'</span><span style="color: #339933;">,</span><span style="color: #000088;">$armor</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">display</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'armor-shop.tpl'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This code is also similar to the code for the weapons shop &#8211; however, you might notice one fairly large change. There doesn&#8217;t seem to be any code to display the user&#8217;s current armor!</p>
<p>This isn&#8217;t actually true, however &#8211; all we&#8217;ve done is convert our retrieval logic to be a little more generic. The 10 lines of code between line 25 and line 35 of armor-shop.php are all the code we need to display however many stats we want &#8211; although we&#8217;re currently only displaying 5.</p>
<p>We start off by defining the keys for our stats &#8211; we will use those to both retrieve the stat values, <strong>and</strong> set the appropriate values within our smarty template. Then, we loop through each of our stat keys, and retrieve their information. We can get away with this because the keys that we set up in our template and the keys that we used within our stats system are the same; if they weren&#8217;t, you would have to do something a little more complex to gain this amount of flexibility. After retrieving the stat&#8217;s information, we assign it to our template &#8211; and we&#8217;re displaying the user&#8217;s equipped armor, in 10 lines of code!</p>
<p>However cool that may be, however, we haven&#8217;t gotten to the main <strong>point</strong> of the armor shop yet &#8211; actually buying armor! Therefore, we&#8217;ll now add to our code in <strong>armor-shop.php</strong> so that it will respond appropriately when a user clicks on the &#8216;Buy&#8217; button next to a piece of armor for sale. Before we can do that though, we will need to create <strong>armor-stats.php</strong>, so that we can retrieve stats for individual pieces of armor:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'stats-dry.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> getArmorStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$armorID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> getStatDRY<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Item'</span><span style="color: #339933;">,</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$armorID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>With that done, we can now write the code to purchase armor:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'armor-stats.php'</span><span style="color: #339933;">;</span>		<span style="color: #666666; font-style: italic;">// armor stats</span>
	<span style="color: #000088;">$armorID</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'armor-id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT price FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$armorID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cost</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$gold</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$gold</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$cost</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$slot</span> <span style="color: #339933;">=</span> getArmorStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'aslot'</span><span style="color: #339933;">,</span><span style="color: #000088;">$armorID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$equipped</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$slot</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$equipped</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			setStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$slot</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #000088;">$armorID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gold</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$cost</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'message'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'You purchased and equipped the new armor.'</span><span style="color: #009900;">&#41;</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: #666666; font-style: italic;">// they already have something equipped - display an error</span>
			<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'You are already wearing a piece of that kind of armor! You will need to sell your current armor before you can buy new armor.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'You cannot afford that piece of armor.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And with that piece of code added, users can now purchase armor. As you can see, we are using the <em>aslot</em> stat that we added earlier to determine which armor slot a specific piece of armor should go in &#8211; this way, we can reduce the amount of code that we need to write to handle multiple armor slots.</p>
<p>Right now, players can buy armor &#8211; but they can&#8217;t sell it. Let&#8217;s add the code to handle users selling their armor:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sell'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$armorSlot</span> <span style="color: #339933;">=</span> getArmorStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'aslot'</span><span style="color: #339933;">,</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sell'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$armorID</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$armorSlot</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT price FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$armorID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$price</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$gold</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gold</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$price</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	setStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$armorSlot</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</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: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span></pre></td></tr></table></div>

<p>And with that piece of code added, users can now buy and sell their armor. The single last change that we need to make is adding the &#8216;Armor Shop&#8217; link to <strong>index.tpl</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>19
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;p&gt;&lt;a href='armor-shop.php'&gt;The Armor Shop&lt;/a&gt;&lt;/p&gt;</pre></td></tr></table></div>

<p>And that&#8217;s that! Here is all of the code for our new armor shop:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'smarty.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'config.php'</span><span style="color: #339933;">;</span>		<span style="color: #666666; font-style: italic;">// our database settings</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><span style="color: #000088;">$dbhost</span><span style="color: #339933;">,</span><span style="color: #000088;">$dbuser</span><span style="color: #339933;">,</span><span style="color: #000088;">$dbpass</span><span style="color: #009900;">&#41;</span>
	or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error connecting to mysql'</span><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><span style="color: #000088;">$dbname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// retrieve player's ID</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT id FROM users WHERE UPPER(username) = UPPER('<span style="color: #009933; font-weight: bold;">%s</span>')&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'stats.php'</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// player stats</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'armor-stats.php'</span><span style="color: #339933;">;</span>		<span style="color: #666666; font-style: italic;">// armor stats</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sell'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$armorSlot</span> <span style="color: #339933;">=</span> getArmorStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'aslot'</span><span style="color: #339933;">,</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sell'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$armorID</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$armorSlot</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT price FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$armorID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$price</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$gold</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gold</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$price</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$armorSlot</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</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: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>	
		<span style="color: #000088;">$armorID</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'armor-id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT price FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$armorID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cost</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$gold</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$gold</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$cost</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$slot</span> <span style="color: #339933;">=</span> getArmorStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'aslot'</span><span style="color: #339933;">,</span><span style="color: #000088;">$armorID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$equipped</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$slot</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$equipped</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				setStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$slot</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #000088;">$armorID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gold</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$cost</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'message'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'You purchased and equipped the new armor.'</span><span style="color: #009900;">&#41;</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: #666666; font-style: italic;">// they already have something equipped - display an error</span>
				<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'You are already wearing a piece of that kind of armor! You will need to sell your current armor before you can buy new armor.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'You cannot afford that piece of armor.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT DISTINCT(id), name, price FROM items WHERE type = 'Armor' ORDER BY RAND() LIMIT 10;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$armor</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$armor</span><span style="color: #339933;">,</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$stats</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'atorso'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'ahead'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'alegs'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'aright'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'aleft'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$stats</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT name FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'armor'</span><span style="color: #339933;">,</span><span style="color: #000088;">$armor</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">display</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'armor-shop.tpl'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>And here&#8217;s the code for the template(<strong>armor-shop.tpl</strong>):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;The Armor Shop&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;p&gt;Welcome to the Armor Shop.&lt;/p&gt;
	&lt;p&gt;&lt;a href='index.php'&gt;Back to main&lt;/a&gt;&lt;/p&gt;
	&lt;h3&gt;Current Armor:&lt;/h3&gt;
	&lt;ul&gt;
		&lt;li&gt;
			Head:
			{if $ahead ne ''}
				{$ahead}
				&lt;form action='armor-shop.php' method='post'&gt;
					&lt;input type='hidden' name='sell' value='ahead' /&gt;
					&lt;input type='submit' value='Sell' /&gt;
				&lt;/form&gt;
			{else}
				None
			{/if}
		&lt;/li&gt;
		&lt;li&gt;
			Torso:
			{if $atorso ne ''}
				{$atorso}
				&lt;form action='armor-shop.php' method='post'&gt;
					&lt;input type='hidden' name='sell' value='atorso' /&gt;
					&lt;input type='submit' value='Sell' /&gt;
				&lt;/form&gt;
			{else}
				None
			{/if}
		&lt;/li&gt;
		&lt;li&gt;
			Legs:
			{if $alegs ne ''}
				{$alegs}
				&lt;form action='armor-shop.php' method='post'&gt;
					&lt;input type='hidden' name='sell' value='alegs' /&gt;
					&lt;input type='submit' value='Sell' /&gt;
				&lt;/form&gt;
			{else}
				None
			{/if}
		&lt;/li&gt;
		&lt;li&gt;
			Right Arm:
			{if $aright ne ''}
				{$aright}
				&lt;form action='armor-shop.php' method='post'&gt;
					&lt;input type='hidden' name='sell' value='aright' /&gt;
					&lt;input type='submit' value='Sell' /&gt;
				&lt;/form&gt;
			{else}
				None
			{/if}
		&lt;/li&gt;
		&lt;li&gt;
			Left Arm:
			{if $aleft ne ''}
				{$aleft}
				&lt;form action='armor-shop.php' method='post'&gt;
					&lt;input type='hidden' name='sell' value='aleft' /&gt;
					&lt;input type='submit' value='Sell' /&gt;
				&lt;/form&gt;
			{else}
				None
			{/if}
		&lt;/li&gt;
&nbsp;
	&lt;/ul&gt;
	&lt;p&gt;You may purchase any of the armor listed below.&lt;/p&gt;
	{if $error ne ''}
		&lt;p style='color:red'&gt;{$error}&lt;/p&gt;
	{/if}
	{if $message ne ''}
		&lt;p style='color:green'&gt;{$message}&lt;/p&gt;
	{/if}
	&lt;ul&gt;
		{foreach from=$armor key=id item=i}
			&lt;li&gt;
				&lt;strong&gt;{$i.name}&lt;/strong&gt; - &lt;em&gt;{$i.price} gold coins&lt;/em&gt;
				&lt;form action='armor-shop.php' method='post'&gt;
					&lt;input type='hidden' name='armor-id' value='{$i.id}' /&gt;
					&lt;input type='submit' value='Buy' /&gt;
				&lt;/form&gt;
		{/foreach}
	&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>If you&#8217;re having trouble getting some armor into your shop to play around with, here&#8217;s a quick SQL query you can run to insert some sample armors:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> items<span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">,</span>type<span style="color: #66cc66;">,</span>price<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Sample Helmet'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Armor'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> entity_stats<span style="color: #66cc66;">&#40;</span>stat_id<span style="color: #66cc66;">,</span>entity_id<span style="color: #66cc66;">,</span>value<span style="color: #66cc66;">,</span>entity_type<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> stats <span style="color: #993333; font-weight: bold;">WHERE</span> short_name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'aslot'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> items <span style="color: #993333; font-weight: bold;">WHERE</span> name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Sample Helmet'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'ahead'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Item'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> items<span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">,</span>type<span style="color: #66cc66;">,</span>price<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Sample Torso'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Armor'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> entity_stats<span style="color: #66cc66;">&#40;</span>stat_id<span style="color: #66cc66;">,</span>entity_id<span style="color: #66cc66;">,</span>value<span style="color: #66cc66;">,</span>entity_type<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> stats <span style="color: #993333; font-weight: bold;">WHERE</span> short_name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'aslot'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> items <span style="color: #993333; font-weight: bold;">WHERE</span> name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Sample Torso'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'atorso'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Item'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> items<span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">,</span>type<span style="color: #66cc66;">,</span>price<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Sample Legs'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Armor'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> entity_stats<span style="color: #66cc66;">&#40;</span>stat_id<span style="color: #66cc66;">,</span>entity_id<span style="color: #66cc66;">,</span>value<span style="color: #66cc66;">,</span>entity_type<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> stats <span style="color: #993333; font-weight: bold;">WHERE</span> short_name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'aslot'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> items <span style="color: #993333; font-weight: bold;">WHERE</span> name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Sample Legs'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'alegs'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Item'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> items<span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">,</span>type<span style="color: #66cc66;">,</span>price<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Sample Right Arm'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Armor'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> entity_stats<span style="color: #66cc66;">&#40;</span>stat_id<span style="color: #66cc66;">,</span>entity_id<span style="color: #66cc66;">,</span>value<span style="color: #66cc66;">,</span>entity_type<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> stats <span style="color: #993333; font-weight: bold;">WHERE</span> short_name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'aslot'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> items <span style="color: #993333; font-weight: bold;">WHERE</span> name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Sample Right Arm'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'aright'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Item'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> items<span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">,</span>type<span style="color: #66cc66;">,</span>price<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Sample Left Arm'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Armor'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> entity_stats<span style="color: #66cc66;">&#40;</span>stat_id<span style="color: #66cc66;">,</span>entity_id<span style="color: #66cc66;">,</span>value<span style="color: #66cc66;">,</span>entity_type<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> stats <span style="color: #993333; font-weight: bold;">WHERE</span> short_name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'aslot'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> items <span style="color: #993333; font-weight: bold;">WHERE</span> name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Sample Left Arm'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'aleft'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Item'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<h2>Extra Credit</h2>
<ol>
<li>Refactor the weapon shop code to use the same code as the armor shop for displaying current equipment.</li>
<li>Refactor the weapon/armor shop templates so that both pages can use the same template.</li>
<li>Refactor both pieces of code, so that there is only one template and one code file for weapons <strong>and</strong> armor.</li>
</ol>
<p class='blurb'>There was a small bug found in the weapon stat&#8217;s retrieval code during the writing of this entry &#8211; make sure to update your checked out version!</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/09/22/buying-armor-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Sending unknown number of variable parameters to new page in HTML</title>
		<link>http://buildingbrowsergames.com/2008/09/17/sending-unknown-number-of-variable-parameters-to-new-page-in-html/</link>
		<comments>http://buildingbrowsergames.com/2008/09/17/sending-unknown-number-of-variable-parameters-to-new-page-in-html/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 14:00:34 +0000</pubDate>
		<dc:creator>gostyloj</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[terratanks]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=386</guid>
		<description><![CDATA[Before I get started I wanted to say that I am not a web server expert so if you know of a better way to do this, post it in the comments and I will thank you as I implement your knowledge in my game.
When using PHP you eventually come to the realization that your [...]]]></description>
			<content:encoded><![CDATA[<p>Before I get started I wanted to say that I am not a web server expert so if you know of a better way to do this, post it in the comments and I will thank you as I implement your knowledge in my game.</p>
<p>When using PHP you eventually come to the realization that your gatekeepers for everything you do and display are HTTP and HTML. Â They are both great tools but were designed with a more static implementation in mind.  Values passed to new web pages are set up in key->value pairs that are accessed with an associative array using the name of the parameter as the key (ie. $_POST['myParam']). Â This post will deal with the issue of how to send an unknown number of parameters which are user set to a new page which can then use those parameters correctly.</p>
<p>My particular implementation is my Mining page in my game <a href="http://www.terratanks.com">TerraTanks</a>. Â The mining page allows the user to set the type of mining on each of their planets so that if they are deficient in a particular resource, they can mine it out faster. Â I wanted to make sure that this could be changed globally, so the mining page will show all of your planets and each planet will have a set of radio buttons allowing you to change the mining settings. Â With these constraints, you cannot know how many planets the user will have and you must write your code to account for any number of parameters.</p>
<p>This is a simplified version of what I have:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// I have the result set for all my mining planets in $result</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$element</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// code displaying what I need about planet //</span>
&nbsp;
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;INPUT TYPE=<span style="color: #000099; font-weight: bold;">\&quot;</span>radio<span style="color: #000099; font-weight: bold;">\&quot;</span> name=<span style="color: #000099; font-weight: bold;">\&quot;</span>miner<span style="color: #006699; font-weight: bold;">{$count}</span><span style="color: #000099; font-weight: bold;">\&quot;</span> value=<span style="color: #000099; font-weight: bold;">\&quot;</span>normal,<span style="color: #006699; font-weight: bold;">$element-&gt;location</span><span style="color: #000099; font-weight: bold;">\&quot;</span> checked&gt;Normal&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;INPUT TYPE=<span style="color: #000099; font-weight: bold;">\&quot;</span>radio<span style="color: #000099; font-weight: bold;">\&quot;</span> name=<span style="color: #000099; font-weight: bold;">\&quot;</span>miner<span style="color: #006699; font-weight: bold;">{$count}</span><span style="color: #000099; font-weight: bold;">\&quot;</span> value=<span style="color: #000099; font-weight: bold;">\&quot;</span>iron,<span style="color: #006699; font-weight: bold;">$element-&gt;location</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;Iron&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$count</span><span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I am creating the name of the radio button group by appending the value of $count the the word miner and then incrementing count as long as there are planets.  Basically, I am hacking together my own array.  It should be noted that I am setting the first radio button to the checked state.  The web page that will read the input depends on everything that exists having a value so it is necessary to make sure that something is selected.  In your work you will want to check what the value is previously set to and make sure the correct radio button is checked when the page loads.</p>
<p>You may also notice that I am shoving a lot of data into the value sent appended by commas.  This is a decent way of shoving related data through without having to define a ton of variables.</p>
<p>Now we have to create something on the receiving page that will correctly read in the values that we are sending.  This is the code that I am using:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$indexName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;miner&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$indexName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$miner</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$indexName</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$x</span><span style="color: #339933;">++;</span>
	<span style="color: #000088;">$indexName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;miner&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you read the code in English it roughly says, as long as there is a POST value at the index miner{$x} where $x starts at 0 and increments by one then just keep incrementing $x and saving the value into my own array.  This is why it was so important that as long as you had a planet you had a value that was passed.  If you set a value for planet 0, 1, and 5 but not 2, 3, or 4 then the code would stop at 2, determine there was no value and would never get to 5.</p>
<p>Now instead of creating a second pseudo array with hidden inputs that pointed to the location of the planet, I packed all the information into a comma appended string.  When I get the value of $miner[$x] I can use the php function explode to separate out the sub values.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$minerParts</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$miner</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// now $minerParts[$x][0] is the mining type and $minerParts[$x][1] is the location</span></pre></div></div>

<p>So there is a way to send an unknown number of variables to a new page.  Like I said, if you know of a better way make sure you post it in the comments.  I don&#8217;t claim to be an expert on this subject, it is just how I resolved this problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/09/17/sending-unknown-number-of-variable-parameters-to-new-page-in-html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building Browsergames: Integrating weapons into our combat system (PHP)</title>
		<link>http://buildingbrowsergames.com/2008/09/12/integrating-weapons-into-our-combat-system-php/</link>
		<comments>http://buildingbrowsergames.com/2008/09/12/integrating-weapons-into-our-combat-system-php/#comments</comments>
		<pubDate>Fri, 12 Sep 2008 14:00:15 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=354</guid>
		<description><![CDATA[For all that we&#8217;ve built both a combat system and a weapons system, we haven&#8217;t yet done anything that allows the weapon a user has selected as their primary weapon to affect the combat system. Today, we will be integrating weapons into our combat system, so that the stats of the weapon actually affect the [...]]]></description>
			<content:encoded><![CDATA[<p>For all that we&#8217;ve built both a <a href='http://buildingbrowsergames.com/2008/06/11/building-browsergames-a-simple-combat-system/'>combat system</a> and a <a href='http://buildingbrowsergames.com/2008/08/19/swapping-weapons-php/'>weapons system</a>, we haven&#8217;t yet done anything that allows the weapon a user has selected as their primary weapon to affect the combat system. Today, we will be integrating weapons into our combat system, so that the stats of the weapon actually affect the combat results.</p>
<p>This is actually a lot easier than it sounds &#8211; all we have to do is retrieve the weapon&#8217;s &#8216;attack&#8217; stat, use it to modify the damage that the player does, and we&#8217;re finished! We&#8217;ll open up <strong>forest.php</strong>, to the piece of code that retrieves our player&#8217;s attack:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>21
22
23
24
25
26
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$player</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span>
	name		<span style="color: #339933;">=&gt;</span>	<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
	attack 		<span style="color: #339933;">=&gt;</span>	getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'atk'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	defence		<span style="color: #339933;">=&gt;</span>	getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'def'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	curhp		<span style="color: #339933;">=&gt;</span>	getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'curhp'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Unfortunately, we don&#8217;t yet have a weapon stats retrieval system &#8211; but thanks to our DRY changes from earlier, that&#8217;s an easy piece of code to add:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'stats-dry.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> getWeaponStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$weaponID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> getStatDRY<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Weapon'</span><span style="color: #339933;">,</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$weaponID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><em></em></p>
<p>Because we won&#8217;t be allowing users to modify the stats of weapons in the game, we only need to add the <em>getWeaponStat</em> function. Save that file as <strong>weapon-stats.php</strong>, and go back to <strong>forest.php</strong>. First off, we&#8217;ll require our weapon stats code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'weapon-stats.php'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Because we&#8217;re using a simple combat formula, we can directly modify the player&#8217;s <em>attack</em> attribute in the associative array that we are using to store their stats. Here&#8217;s how we would retrieve their primary weapon, and then add it&#8217;s attack to the player&#8217;s:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>28
29
30
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$phand</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$atk</span> <span style="color: #339933;">=</span> getWeaponStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'atk'</span><span style="color: #339933;">,</span><span style="color: #000088;">$phand</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$player</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'attack'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$atk</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And with that small piece of code added, we&#8217;re finished! If the weapon has an attack value(even if it&#8217;s negative), it will be added to the player&#8217;s when they attack the monster.</p>
<h2>Extra Credit</h2>
<ul>
<li>Make the combat system display the name of the weapon when the player attacks &#8211; e.g. &#8220;You attack &lt;monster&gt; with your &lt;weapon&gt; for 10 damage!&#8221;.</weapon>
</ul>
<p class='blurb'>There was a small bug with the stats retrieval code found in this version of the tutorial &#8211; it has been fixed in the latest revision of the <a href='http://code.google.com/p/building-browsergames-tutorial'>Google Code Repository</a>. Make sure to update if you&#8217;ve been using it!</p></p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/09/12/integrating-weapons-into-our-combat-system-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Saving Database Space through Bit-masking</title>
		<link>http://buildingbrowsergames.com/2008/09/04/saving-database-space-through-bit-masking/</link>
		<comments>http://buildingbrowsergames.com/2008/09/04/saving-database-space-through-bit-masking/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 14:00:34 +0000</pubDate>
		<dc:creator>gostyloj</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=283</guid>
		<description><![CDATA[This is a trick you can use to increase the efficiency and readability of your project.  It is an argument for good up front design as utilizing this is only plausible when you take the time and effort at the beginning.  The following is a real world example from my game TerraTanks.
The problem [...]]]></description>
			<content:encoded><![CDATA[<p>This is a trick you can use to increase the efficiency and readability of your project.  It is an argument for good up front design as utilizing this is only plausible when you take the time and effort at the beginning.  The following is a real world example from my game TerraTanks.</p>
<p>The problem is that you have an object with a ton of properties that are incredibly similar and they describe the object in a yes|no fashion.  In my case, players can do 24 types of research and the state of the player is &#8220;yes, I have done that particular research&#8221; or &#8220;no, I have not done that research&#8221;.</p>
<p>One solution is to make a table with a column that associates with the player id and a boolean column for every type of research that you have.  Now if you have 24 types of research your table is 25 columns big.  This can get out of hand pretty quickly.  The table becomes hard to read and you have to use different code (or procedurally dynamic code) to set individual columns.</p>
<p>Another solution is to add a column to your player definition table and make it type INT UNSIGNED.  Then you let your code efficiently handle interpreting the integer as the player&#8217;s research definition through bit masking.  Here&#8217;s how it works.</p>
<p>The maximum value of an unsigned INT in MySQL is <code class="literal">4294967295. </code> In binary this number looks like 11111111111111111111111111111111.  That is 32 1&#8217;s in a row.  Each of those digits can describe a research type as &#8216;have&#8217; (it is a 1) or &#8216;have not&#8217; (it is a 0).  Now in a global file for your code you need to define each research type as a number that is a power of 2.  It would look something like this in PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$g_shield_research</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>              <span style="color: #666666; font-style: italic;">// in binary 001</span>
<span style="color: #000088;">$g_armor_piercing_research</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// in binary 010</span>
<span style="color: #000088;">$g_mining_research</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>             <span style="color: #666666; font-style: italic;">// in binary 100</span></pre></div></div>

<p>
Now if you want to know whether you have a particular research you would perform a bitmask operation on the integer you retrieve from your database using the &amp; operator.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// will mask players research and return true if the mining bit is set to 1</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$element</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">research</span> <span style="color: #339933;">&amp;</span> <span style="color: #000088;">$g_mining_research</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>
The bit masking procedure is extremely efficient and fast and you can see how it compresses all the research information into the size of an integer.  Also, if you want to know everything about a player&#8217;s research you only have to retrieve a single integer from the database.</p>
<p>Assigning research to a player is also very easy.  Simply bitwise OR the current research integer with the set bitmask using the | operator:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$newPlayerResearch</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$element</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">research</span> <span style="color: #339933;">|</span> <span style="color: #000088;">$g_shield_research</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can technically add the two numbers to get the same result, but this is unsafe because if you add the research when it is already there it will throw everything off.</p>
<p>There are some pitfalls to using this trick.  While it is easy to add another type of research just by assigning its mask to the next highest power of 2, you are limited to 32 total research types.  One way to get around this is to make the column type BIGINT which would give you 64 bits to work with, but at the end of the day you are still limited.  Also, once a game starts the research you choose for that bit position is pretty much stuck there unless you want to do some math maintenance.</p>
<p>While this trick will tend to make your code more readable because database statements won&#8217;t be as long, your database entry will not be human readable so it could slow down your debugging efforts.</p>
<p>So there you have it.  A very powerful tool if used wisely.  Please design well before you start writing code.  It makes life easier.</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/09/04/saving-database-space-through-bit-masking/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Building Browsergames: now on Google Code!</title>
		<link>http://buildingbrowsergames.com/2008/08/28/building-browsergames-now-on-google-code/</link>
		<comments>http://buildingbrowsergames.com/2008/08/28/building-browsergames-now-on-google-code/#comments</comments>
		<pubDate>Thu, 28 Aug 2008 14:00:43 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[gettingstarted]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rubyonrails]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=274</guid>
		<description><![CDATA[If you&#8217;ve been following along with our tutorial at all, you may have noticed that our code files tend to&#8230;evolve over time. Templates go from being simple list-of-links affairs to being filled with loops and conditionals and all kinds of other goodies.
Today, I have news for you that will make it much easier to follow [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve been following along with our tutorial at all, you may have noticed that our code files tend to&#8230;<em>evolve</em> over time. Templates go from being simple list-of-links affairs to being filled with loops and conditionals and all kinds of other goodies.</p>
<p>Today, I have news for you that will make it much easier to follow the different versions that our tutorial goes through &#8211; it&#8217;s now under source control! With the help of <a href='http://www.johnmunsch.com'>John Munsch</a>, the Building Browsergames tutorial is now on Google Code. You can take a look at the project by visiting <a href='http://code.google.com/p/building-browsergames-tutorial/'>http://code.google.com/p/building-browsergames-tutorial/</a>. You can check out the latest version of the entire tutorial&#8217;s codebase by issuing this command:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">svn checkout http://building-browsergames-tutorial.googlecode.com/svn/trunk building-browsergames-tutorial-read-only</pre></div></div>

<p>Which will retrieve the latest version(in all languages) and store it into a directory called <em>building-browsergames-tutorial-read-only</em>. If you&#8217;d like to check out the latest version of the code for a specific language, you can use this command:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">svn checkout http://building-browsergames-tutorial.googlecode.com/svn/trunk/language/pbbg buildingbrowsergames-tutorial-read-only</pre></div></div>

<p>..Where &#8216;language&#8217; is one of the languages that the tutorial has been implemented in(currently, &#8216;perl&#8217;, &#8216;php&#8217;, and &#8216;rubyonrails&#8217; are available).</p>
<p>You can also update the code if you retrieved the latest copy and then new changes are committed by running the <em>svn update</em> command:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">svn update</pre></div></div>

<p>Don&#8217;t know what Subversion is, or how to use it on your system? Take a look at this <a href='http://blog.clickablebliss.com/2006/04/26/introduction-to-subversion-screencast/'>introduction to Subversion screencast</a> to learn more.</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/08/28/building-browsergames-now-on-google-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Building Browsergames: Swapping Weapons (PHP)</title>
		<link>http://buildingbrowsergames.com/2008/08/19/swapping-weapons-php/</link>
		<comments>http://buildingbrowsergames.com/2008/08/19/swapping-weapons-php/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 14:00:15 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=233</guid>
		<description><![CDATA[It was a close race, but in the end the results of our poll on whether users should dual wield their weapons or have to toggle which one was active have been decided: users will toggle which weapon is currently active. Today, we&#8217;re going to be implementing that functionality.
The &#8217;swap weapon&#8217; page is a fairly [...]]]></description>
			<content:encoded><![CDATA[<p>It was a close race, but in the end the results of our poll on whether users should dual wield their weapons or have to toggle which one was active have been decided: <strong>users will toggle which weapon is currently active</strong>. Today, we&#8217;re going to be implementing that functionality.</p>
<p>The &#8217;swap weapon&#8217; page is a fairly simple one &#8211; all we need is a template and some code to handle it(we even wrote most of it earlier, when we built our <a href='http://buildingbrowsergames.com/2008/08/11/building-browsergames-buying-weapons-php/'>weapons shop</a>). We&#8217;ll start off with the template, and call it <strong>equipment.tpl</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Equipment Management&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;h3&gt;Current Equipment:&lt;/h3&gt;
	&lt;p&gt;&lt;a href='index.php'&gt;Back to main&lt;/a&gt;&lt;/p&gt;
	&lt;ul&gt;
		&lt;li&gt;
			Primary Hand:
			{if $phand ne ''}
				{$phand}
				&lt;form action='weapon-shop.php' method='post'&gt;
					&lt;input type='hidden' name='sell' value='phand' /&gt;
					&lt;input type='submit' value='Sell' /&gt;
				&lt;/form&gt;
			{else}
				None
			{/if}
		&lt;/li&gt;
		&lt;li&gt;
			Secondary Hand:
			{if $shand ne ''}
				{$shand}
				&lt;form action='weapon-shop.php' method='post'&gt;
					&lt;input type='hidden' name='sell' value='shand' /&gt;
					&lt;input type='submit' value='Sell' /&gt;
				&lt;/form&gt;
			{else}
				None
			{/if}
		&lt;/li&gt;
	&lt;/ul&gt;
	&lt;p&gt;
		&lt;form action='equipment.php' method='post'&gt;
			&lt;input type='submit' value='Swap' name='swap' /&gt;
		&lt;/form&gt;
	&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>This template is relatively simple, and essentially just the top parts of the weapon shop&#8217;s template &#8211; we even left in the &#8216;Sell&#8217; buttons, so that users can sell their equipment straight from their equipment page. We have to make sure that the &#8217;swap&#8217; button has a &#8216;name&#8217; attribute &#8211; that way we can check <em>$_POST</em> later to see if it was clicked. Next we&#8217;ll build <strong>equipment.php</strong>, which is responsible for retrieving the current weapons a user is using, and displaying this template:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'smarty.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'config.php'</span><span style="color: #339933;">;</span>		<span style="color: #666666; font-style: italic;">// our database settings</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><span style="color: #000088;">$dbhost</span><span style="color: #339933;">,</span><span style="color: #000088;">$dbuser</span><span style="color: #339933;">,</span><span style="color: #000088;">$dbpass</span><span style="color: #009900;">&#41;</span>
	or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error connecting to mysql'</span><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><span style="color: #000088;">$dbname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// retrieve player's ID</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT id FROM users WHERE UPPER(username) = UPPER('<span style="color: #009933; font-weight: bold;">%s</span>')&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'stats.php'</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// player stats</span>
<span style="color: #000088;">$phand</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$shand</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'shand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$phand_query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT name FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phand</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phand_query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phand_name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$phand_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$shand_query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT name FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$shand</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$shand_query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$shand_name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'shand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$shand_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">display</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'equipment.tpl'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Essentially, all that we&#8217;re going to be adding on to this code is handling for a POST request; if something is POSTed to this page, we swap the user&#8217;s current weapons. The code is fairly simple, as you can see:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #000088;">$shand</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'shand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #000088;">$phand</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$shand</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$shand</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$phand</span><span style="color: #339933;">;</span>	
	<span style="color: #000088;">$phand</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$temp</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And that&#8217;s all there is to it! We just add another link to our index page:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>19
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">	&lt;p&gt;&lt;a href='equipment.php'&gt;Equipment Management&lt;/a&gt;&lt;/p&gt;</pre></td></tr></table></div>

<p>And we&#8217;re finished! Here&#8217;s the code for our template:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Equipment Management&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;h3&gt;Current Equipment:&lt;/h3&gt;
	&lt;p&gt;&lt;a href='index.php'&gt;Back to main&lt;/a&gt;&lt;/p&gt;
	&lt;ul&gt;
		&lt;li&gt;
			Primary Hand:
			{if $phand ne ''}
				{$phand}
				&lt;form action='weapon-shop.php' method='post'&gt;
					&lt;input type='hidden' name='sell' value='phand' /&gt;
					&lt;input type='submit' value='Sell' /&gt;
				&lt;/form&gt;
			{else}
				None
			{/if}
		&lt;/li&gt;
		&lt;li&gt;
			Secondary Hand:
			{if $shand ne ''}
				{$shand}
				&lt;form action='weapon-shop.php' method='post'&gt;
					&lt;input type='hidden' name='sell' value='shand' /&gt;
					&lt;input type='submit' value='Sell' /&gt;
				&lt;/form&gt;
			{else}
				None
			{/if}
		&lt;/li&gt;
	&lt;/ul&gt;
	&lt;p&gt;
		&lt;form action='equipment.php' method='post'&gt;
			&lt;input type='submit' value='Swap' name='swap' /&gt;
		&lt;/form&gt;
	&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>And here&#8217;s the code for the file that handles the functionality behind the template:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'smarty.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'config.php'</span><span style="color: #339933;">;</span>		<span style="color: #666666; font-style: italic;">// our database settings</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><span style="color: #000088;">$dbhost</span><span style="color: #339933;">,</span><span style="color: #000088;">$dbuser</span><span style="color: #339933;">,</span><span style="color: #000088;">$dbpass</span><span style="color: #009900;">&#41;</span>
	or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error connecting to mysql'</span><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><span style="color: #000088;">$dbname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// retrieve player's ID</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT id FROM users WHERE UPPER(username) = UPPER('<span style="color: #009933; font-weight: bold;">%s</span>')&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'stats.php'</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// player stats</span>
<span style="color: #000088;">$phand</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$shand</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'shand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #000088;">$shand</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'shand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #000088;">$phand</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$shand</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$shand</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$phand</span><span style="color: #339933;">;</span>	
	<span style="color: #000088;">$phand</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$temp</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$phand_query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT name FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phand</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phand_query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phand_name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$phand_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$shand_query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT name FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$shand</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$shand_query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$shand_name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'shand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$shand_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">display</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'equipment.tpl'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/08/19/swapping-weapons-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Building Browsergames: Buying Weapons (PHP)</title>
		<link>http://buildingbrowsergames.com/2008/08/11/building-browsergames-buying-weapons-php/</link>
		<comments>http://buildingbrowsergames.com/2008/08/11/building-browsergames-buying-weapons-php/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 14:00:09 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=152</guid>
		<description><![CDATA[Based on the results of our poll, you&#8217;ve all voted that players should carry two weapons(of any type they want), with a primary hand and a secondary hand that can both have weapons in them.
Our stats system is perfect for this &#8211; all we have to do is add another stat for players that keeps [...]]]></description>
			<content:encoded><![CDATA[<p>Based on the results of our poll, you&#8217;ve all voted that players should carry two weapons(of any type they want), with a primary hand and a secondary hand that can both have weapons in them.</p>
<p>Our stats system is perfect for this &#8211; all we have to do is add another stat for players that keeps track of which weapon is in their primary hand, and which weapon is in their secondary hand.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span> short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Primary Hand Weapon'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'phand'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Secondary Hand Weapon'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'shand'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>With the new stats inserted, we can now start writing code to take advantage of them. But how will users obtain weapons to equip?</p>
<p>For the moment, players will get weapons at the Weapons Shop &#8211; which is what we will be building today. The weapons shop will list off a random selection of weapons that are available for users to purchase, and automatically put weapons into primary or secondary hands after a user purchases them.</p>
<p>In order for our items to work in a shop, however, we need to make another change &#8211; adding prices to them! We&#8217;ll add a column to our <em>items</em> table called &#8216;price&#8217;, and set it to have a default value of 10(for 10 gold coins):</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span>  <span style="color: #ff0000;">'items'</span> <span style="color: #993333; font-weight: bold;">ADD</span>  <span style="color: #ff0000;">'price'</span> INT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span>  <span style="color: #ff0000;">'10'</span>;</pre></div></div>

<p>With that finished, we can start working a little more on the actual Weapon Shop page. First off, we&#8217;ll build a template for the shop and call it <strong>weapon-shop.tpl</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;The Weapon Shop&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;p&gt;Welcome to the Weapon Shop.&lt;/p&gt;
	&lt;h3&gt;Current Equipment:&lt;/h3&gt;
	&lt;ul&gt;
		&lt;li&gt;Primary Hand: {if $phand ne ''}{$phand}{else}None{/if}&lt;/li&gt;
		&lt;li&gt;Secondary Hand: {if $shand ne ''}{$shand}{else}None{/if}&lt;/li&gt;
	&lt;/ul&gt;
	&lt;p&gt;Below are the weapons currently available for purchase.&lt;/p&gt;
	&lt;ul&gt;
		{foreach from=$weapons key=id item=i}
			&lt;li&gt;
				&lt;strong&gt;{$i.name}&lt;/strong&gt; - &lt;em&gt;{$i.price} gold coins&lt;/em&gt;
				&lt;form action='weapon-shop.php' method='post'&gt;
					&lt;input type='hidden' name='weapon-id' value='{$i.id}' /&gt;
					&lt;input type='submit' value='Buy' /&gt;
				&lt;/form&gt;
		{/foreach}
	&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>As you can probably see based on our template, we will be using a <em>foreach</em> loop to list off all of the weapons that are currently available. We&#8217;re going to use a fairly simple SQL query to list off 5 random weapons, each time that the user visits the weapon shop:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">DISTINCT</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">,</span> price <span style="color: #993333; font-weight: bold;">FROM</span> items <span style="color: #993333; font-weight: bold;">WHERE</span> type <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Weapon'</span> <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> RAND<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">5</span>;</pre></div></div>

<p>Now that we have the SQL query we&#8217;ll be using, we need to quickly write the PHP code to use that query for our shop, inside <strong>weapon-shop.php</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'smarty.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'config.php'</span><span style="color: #339933;">;</span>		<span style="color: #666666; font-style: italic;">// our database settings</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><span style="color: #000088;">$dbhost</span><span style="color: #339933;">,</span><span style="color: #000088;">$dbuser</span><span style="color: #339933;">,</span><span style="color: #000088;">$dbpass</span><span style="color: #009900;">&#41;</span>
	or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error connecting to mysql'</span><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><span style="color: #000088;">$dbname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// retrieve player's ID</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT id FROM users WHERE UPPER(username) = UPPER('<span style="color: #009933; font-weight: bold;">%s</span>')&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'stats.php'</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// player stats</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT DISTINCT(id), name, price FROM items WHERE type = 'Weapon' ORDER BY RAND() LIMIT 5;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$weapons</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$weapons</span><span style="color: #339933;">,</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$phand</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$phand_query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT name FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%%</span>s&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phand</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phand_query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phand_name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$phand_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$shand</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'shand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$shand_query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT name FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%%</span>s&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$shand</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$shand_query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$shand_name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'shand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$shand_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'weapons'</span><span style="color: #339933;">,</span><span style="color: #000088;">$weapons</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">display</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'weapon-shop.tpl'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>We loop through all of the data returned by our query, and display information on each weapon for our shop. We display our weapons, how much they cost, and a small &#8216;Buy&#8217; button for users to purchase them with. Now we just need to make it possible for users to actually buy weapons.</p>
<p>As you saw earlier in our template, we have placed a form into each of our weapon entries, with a &#8216;Buy&#8217; button and the ID of the weapon that users would choose to buy. We will be modifying <strong>weapon-shop.php</strong> so that when users click on the &#8216;Buy&#8217; button, we can purchase the weapon in question for them &#8211; or display a helpful error message if they cannot afford it. To start off, we&#8217;ll edit our template so that it can display our messages for us:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>12
13
14
15
16
17
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">{if $error ne ''}
	&lt;p style='color:red'&gt;{$error}&lt;/p&gt;
{/if}
{if $message ne ''}
	&lt;p style='color:green'&gt;{$message}&lt;/p&gt;
{/if}</pre></td></tr></table></div>

<p>With that modification made, we can now add the code to handle a user clicking on the &#8216;purchase&#8217; link:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$phand</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$shand</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'shand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$weaponID</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'weapon-id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT price FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$weaponID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cost</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$gold</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gold</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$cost</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// subtract gold, equip weapon, go from there.</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$phand</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #000088;">$weaponID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gold</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$cost</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$phand</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$weaponID</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'message'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'You equipped the weapon in your primary hand.'</span><span style="color: #009900;">&#41;</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;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$shand</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'shand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #000088;">$weaponID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gold</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$cost</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$shand</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$weaponID</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'message'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'You equipped the weapon in your secondary hand.'</span><span style="color: #009900;">&#41;</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: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'You already have two weapons! You must sell one before equipping another one.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'You cannot afford that weapon!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We&#8217;ve built some simple swapping logic here &#8211; if the player&#8217;s primary hand is empty, the purchased weapon goes there. Otherwise, it goes into their secondary hand &#8211; and if both have a weapon in them, a message is displayed.</p>
<p>If you haven&#8217;t noticed, we told users that they needed to sell a weapon if they wanted to purchase a new one &#8211; so that&#8217;s what we&#8217;ll be building now. We&#8217;ll start off by tweaking our template again:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;li&gt;
	Primary Hand:
	{if $phand ne ''}
		{$phand}
		&lt;form action='weapon-shop.php' method='post'&gt;
			&lt;input type='hidden' name='sell' value='phand' /&gt;
			&lt;input type='submit' value='Sell' /&gt;
		&lt;/form&gt;
	{else}
		None
	{/if}
&lt;/li&gt;
&lt;li&gt;
	Secondary Hand:
	{if $shand ne ''}
		{$shand}
		&lt;form action='weapon-shop.php' method='post'&gt;
			&lt;input type='hidden' name='sell' value='shand' /&gt;
			&lt;input type='submit' value='Sell' /&gt;
		&lt;/form&gt;
	{else}
		None
	{/if}
&lt;/li&gt;</pre></td></tr></table></div>

<p>With that change made, we&#8217;ll now modify <strong>weapon-shop.php</strong> again so that it can handle the new arguments sent to it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sell'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$weaponID</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sell'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT price FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$weaponID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$price</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$gold</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gold</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$price</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	setStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sell'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</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: #000088;">$phand</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$shand</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'shand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span></pre></td></tr></table></div>

<p>And with that finished, users can now buy and sell different weapons as they so choose. The single last change we need to make is adding a &#8216;Weapon Shop&#8217; link to our main page:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>18
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;p&gt;&lt;a href='weapon-shop.php'&gt;The Weapon Shop&lt;/a&gt;&lt;/p&gt;</pre></td></tr></table></div>

<p>And with that, we&#8217;re finished! Here&#8217;s the code behind our weapon shop, in it&#8217;s entirety:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'smarty.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'config.php'</span><span style="color: #339933;">;</span>		<span style="color: #666666; font-style: italic;">// our database settings</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><span style="color: #000088;">$dbhost</span><span style="color: #339933;">,</span><span style="color: #000088;">$dbuser</span><span style="color: #339933;">,</span><span style="color: #000088;">$dbpass</span><span style="color: #009900;">&#41;</span>
	or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error connecting to mysql'</span><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><span style="color: #000088;">$dbname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// retrieve player's ID</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT id FROM users WHERE UPPER(username) = UPPER('<span style="color: #009933; font-weight: bold;">%s</span>')&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'stats.php'</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// player stats</span>
<span style="color: #000088;">$phand</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$shand</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'shand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sell'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$weaponID</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sell'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT price FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$weaponID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$price</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$gold</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gold</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$price</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sell'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</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: #000088;">$phand</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$shand</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'shand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</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: #000088;">$weaponID</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'weapon-id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT price FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$weaponID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cost</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$gold</span> <span style="color: #339933;">=</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gold</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$cost</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// subtract gold, equip weapon, go from there.</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$phand</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #000088;">$weaponID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gold</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$cost</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$phand</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$weaponID</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'message'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'You equipped the weapon in your primary hand.'</span><span style="color: #009900;">&#41;</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;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$shand</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'shand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #000088;">$weaponID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					setStat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gc'</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gold</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$cost</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$shand</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$weaponID</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'message'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'You equipped the weapon in your secondary hand.'</span><span style="color: #009900;">&#41;</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: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'You already have two weapons! You must sell one before equipping another one.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'You cannot afford that weapon!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT DISTINCT(id), name, price FROM items WHERE type = 'Weapon' ORDER BY RAND() LIMIT 5;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$weapons</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$weapons</span><span style="color: #339933;">,</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$phand_query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT name FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phand</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phand_query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phand_name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$phand_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$shand_query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT name FROM items WHERE id = <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$shand</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$shand_query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$shand_name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'shand'</span><span style="color: #339933;">,</span><span style="color: #000088;">$shand_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'weapons'</span><span style="color: #339933;">,</span><span style="color: #000088;">$weapons</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">display</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'weapon-shop.tpl'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>And here&#8217;s the associated template:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;The Weapon Shop&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;p&gt;Welcome to the Weapon Shop.&lt;/p&gt;
	&lt;h3&gt;Current Equipment:&lt;/h3&gt;
	&lt;ul&gt;
		&lt;li&gt;
			Primary Hand:
			{if $phand ne ''}
				{$phand}
				&lt;form action='weapon-shop.php' method='post'&gt;
					&lt;input type='hidden' name='sell' value='phand' /&gt;
					&lt;input type='submit' value='Sell' /&gt;
				&lt;/form&gt;
			{else}
				None
			{/if}
		&lt;/li&gt;
		&lt;li&gt;
			Secondary Hand:
			{if $shand ne ''}
				{$shand}
				&lt;form action='weapon-shop.php' method='post'&gt;
					&lt;input type='hidden' name='sell' value='shand' /&gt;
					&lt;input type='submit' value='Sell' /&gt;
				&lt;/form&gt;
			{else}
				None
			{/if}
		&lt;/li&gt;
	&lt;/ul&gt;
	&lt;p&gt;Below are the weapons currently available for purchase.&lt;/p&gt;
	{if $error ne ''}
		&lt;p style='color:red'&gt;{$error}&lt;/p&gt;
	{/if}
	{if $message ne ''}
		&lt;p style='color:green'&gt;{$message}&lt;/p&gt;
	{/if}
	&lt;ul&gt;
		{foreach from=$weapons key=id item=i}
			&lt;li&gt;
				&lt;strong&gt;{$i.name}&lt;/strong&gt; - &lt;em&gt;{$i.price} gold coins&lt;/em&gt;
				&lt;form action='weapon-shop.php' method='post'&gt;
					&lt;input type='hidden' name='weapon-id' value='{$i.id}' /&gt;
					&lt;input type='submit' value='Buy' /&gt;
				&lt;/form&gt;
		{/foreach}
	&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>Don&#8217;t forget, you can check out our game in it&#8217;s current state at <a href='http://buildingbrowsergames.com/game/php/index.php'>http://buildingbrowsergames.com/game/php/index.php</a>!</p>
<h2>Extra Credit</h2>
<ol>
<li>Add a current gold display to the Weapon Shop page, so that users can see how much gold they have remaining.</li>
<li>Customize the &#8216;you cannot afford this weapon&#8217; message to also display the weapon name, and how much more gold the player needs.</li>
<li>Change how the weapons for sale are displayed, so that they don&#8217;t update after a user buys a weapon &#8211; <strong>or</strong>, make it so that the weapon the user just bought is not included in the new weapons list.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/08/11/building-browsergames-buying-weapons-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Building Browsergames: Securing our hashes (PHP)</title>
		<link>http://buildingbrowsergames.com/2008/07/15/securing-our-hashes-php/</link>
		<comments>http://buildingbrowsergames.com/2008/07/15/securing-our-hashes-php/#comments</comments>
		<pubDate>Tue, 15 Jul 2008 14:00:04 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=84</guid>
		<description><![CDATA[John Munsch recently pointed out that there&#8217;s a bit of a glaring security hole in our login and registration systems: at the moment, we&#8217;re extremely vulnerable to Rainbow Table attacks. In John&#8217;s words:

The MD5 hash doesnâ€™t actually protect you if someone were able to dump your table of users or gain access to the database [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://www.johnmunsch.com/'>John Munsch</a> recently pointed out that there&#8217;s a bit of a glaring security hole in our login and registration systems: at the moment, we&#8217;re extremely vulnerable to <a href='http://en.wikipedia.org/wiki/Rainbow_table'>Rainbow Table</a> attacks. In John&#8217;s words:</p>
<blockquote><p>
The MD5 hash doesnâ€™t actually protect you if someone were able to dump your table of users or gain access to the database in some fashion.
</p></blockquote>
<p>And you know what? <strong>He&#8217;s absolutely right</strong>. If a malicious user managed to get access to our database at the moment, our user&#8217;s logins wouldn&#8217;t be protected at all. This is a <strong>big problem</strong>, and something we need to fix.</p>
<p>Unfortunately, because password hashing is one-way, we can&#8217;t just get users to reset their password. They&#8217;ll either need to re-register entirely, or we can setup a special page(and stat) in order to make sure that users have reset their passwords. John recommends adding what&#8217;s known as a &#8217;salt&#8217; value to user&#8217;s passwords &#8211; that way, you might have something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="pseudocode" style="font-family:monospace;">password = 'foo'
password + salt = 'foobrownfox'
hashed password = hash('foobrownfox')</pre></div></div>

<p>And if a user were to attack our login information using a rainbow table, they might manage to figure out that the passwords being stored in the database were values like &#8216;foobrownfox&#8217; &#8211; but they&#8217;d have a bit of a harder time figuring out what was the salt value and what was the actual password.</p>
<p>Luckily, this is a pretty easy fix to implement &#8211; we just modify our two calls to md5() in our login and register code to add a salt to the user&#8217;s passwords. Here&#8217;s the changes we make to <strong>register.php</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>24
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">				<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'saltgoeshere'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Unfortunately, at this moment making these changes breaks things for users who signed up before we had to make this fix. In order to try and keep things as seamless as possible for the user, we&#8217;ll be modifying our login code slightly:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER('<span style="color: #009933; font-weight: bold;">%s</span>') AND password='<span style="color: #009933; font-weight: bold;">%s</span>'&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'authenticated'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location:changepass.php'</span><span style="color: #009900;">&#41;</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: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER('<span style="color: #009933; font-weight: bold;">%s</span>') AND password='<span style="color: #009933; font-weight: bold;">%s</span>'&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'saltgoeshere'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'authenticated'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPDATE users SET last_login = NOW() WHERE UPPER(username) = UPPER('<span style="color: #009933; font-weight: bold;">%s</span>') AND password = '<span style="color: #009933; font-weight: bold;">%s</span>'&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'saltgoeshere'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT is_admin FROM users WHERE UPPER(username) = UPPER('<span style="color: #009933; font-weight: bold;">%s</span>') AND password='<span style="color: #009933; font-weight: bold;">%s</span>'&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'saltgoeshere'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$is_admin</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$is_admin</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location:admin.php'</span><span style="color: #009900;">&#41;</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: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location:index.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>	
			<span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'There is no username/password combination like that in the database.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We&#8217;ve made a small change to our login code, so that it first tests to see if the user&#8217;s attributes match up to any users who haven&#8217;t had their passwords salted &#8211; if they do, we redirect them to the page where they can change their password. Here&#8217;s what the template for our &#8216;change password&#8217; page(<strong>change_pass.tpl</strong>) looks like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Change Password&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	{if $error ne &quot;&quot;}
		&lt;span style='color:red'&gt;Error: {$error}&lt;/span&gt;
	{/if}
	{if $message ne &quot;&quot;}
		&lt;span style='color:green'&gt;{$message}&lt;/span&gt;
	{/if}
	&lt;form method='post' action='changepass.php'&gt;
		Password: &lt;input type='password' name='password' id='password' /&gt;&lt;br /&gt;
		Confirm Password: &lt;input type='password' name='confirm' /&gt;&lt;br /&gt;
		&lt;input type='submit' value='Change Password' /&gt;
	&lt;/form&gt;
	&lt;script type='text/javascript'&gt;
	document.getElementById('password').focus();
	&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>With the template created, all we need to do is build the page that handles changing the user&#8217;s password &#8211; like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
26
27
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'smarty.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'login-check.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$confirm</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'confirm'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$confirm</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Passwords do not match!'</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;">require_once</span> <span style="color: #0000ff;">'config.php'</span><span style="color: #339933;">;</span>		<span style="color: #666666; font-style: italic;">// our database settings</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><span style="color: #000088;">$dbhost</span><span style="color: #339933;">,</span><span style="color: #000088;">$dbuser</span><span style="color: #339933;">,</span><span style="color: #000088;">$dbpass</span><span style="color: #009900;">&#41;</span>
			or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error connecting to mysql'</span><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><span style="color: #000088;">$dbname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPDATE users SET password = '<span style="color: #009933; font-weight: bold;">%s</span>' WHERE username = '<span style="color: #009933; font-weight: bold;">%s</span>'&quot;</span><span style="color: #339933;">,</span>
					<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'saltgoeshere'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
					<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Password updated successfully.'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span><span style="color: #339933;">,</span><span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'message'</span><span style="color: #339933;">,</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">display</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'change_pass.tpl'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>And that&#8217;s all there is to it! With a fairly simple change, we&#8217;ve managed to secure our user&#8217;s information a bit better &#8211; a malicious user with direct access to our database won&#8217;t be able to easily figure out what a user&#8217;s password is just by using a Rainbow table. As an added bonus, we&#8217;ve also created an extra piece of functionality &#8211; a change password page!</p>
<p><strong style='color:red'>Note</strong>: don&#8217;t forget to change &#8217;saltgoeshere&#8217; to an actually random value, like &#8217;s79dj@#*(hd&#8217; or something &#8211; you won&#8217;t make any security gains if malicious users can easily guess your password salt. If you&#8217;re feeling <strong>really</strong> adventurous, you could(and probably should) turn the salt into a configuration parameter &#8211; but I&#8217;ll leave how to do that up to you.</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/07/15/securing-our-hashes-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Building Browsergames: DRYing out our stats</title>
		<link>http://buildingbrowsergames.com/2008/07/11/drying-out-our-stats/</link>
		<comments>http://buildingbrowsergames.com/2008/07/11/drying-out-our-stats/#comments</comments>
		<pubDate>Fri, 11 Jul 2008 14:00:35 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[DRY]]></category>
		<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=82</guid>
		<description><![CDATA[Over the course of developing our game, our database has sort of grown organically. As we added a new feature, we&#8217;d add the tables we needed to accomodate this feature. While this works(and has been working for us just fine), it&#8217;s not neccessarily the best way to design your database &#8211; because as you can [...]]]></description>
			<content:encoded><![CDATA[<p>Over the course of developing our game, our database has sort of grown organically. As we added a new feature, we&#8217;d add the tables we needed to accomodate this feature. While this works(and has been working for us just fine), it&#8217;s not neccessarily the best way to design your database &#8211; because as you can see, we now have 3 stats tables(<em>user_stats</em>,<em>monster_stats</em>, and <em>item_stats</em>) when we only actually need one &#8211; as <a href='http://blog.fetmab.com/'>sepp</a> has once again helpfully pointed out.</p>
<p>With that in mind, we&#8217;re going to create another table, to replace those three. Because lots of different things can have stats, I&#8217;m going to call the table <em>entity_stats</em> &#8211; although if you want to, you can call it whatever you want(but make sure to keep your change in mind when you&#8217;re working through this code). Here&#8217;s what we&#8217;re going to do to create the table:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> entity_stats <span style="color: #66cc66;">&#40;</span>
	id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	stat_id int<span style="color: #66cc66;">,</span>
	entity_id int<span style="color: #66cc66;">,</span>
	value text<span style="color: #66cc66;">,</span>
	entity_type ENUM<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'User'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Monster'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Item'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now that we&#8217;ve created this new table, we&#8217;re going to need to customize our SQL queries slightly. Previously, we were only using an object&#8217;s ID value to retrieve the stats for it &#8211; now that we don&#8217;t have that separation, we will need to run the query based on two things &#8211; the object&#8217;s ID, and the object&#8217;s type.</p>
<p>Because we&#8217;ve been doing re-writing to our stats code, we&#8217;re actually in a good position to make this change &#8211; we only need to change the query in one file, instead of 3(or more). Our old SQL looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #ff0000;">&quot;SELECT value FROM table WHERE stat_id = (SELECT id FROM stats WHERE display_name = 'foo' OR short_name = 'bar') AND column = 'baz'&quot;</span></pre></div></div>

<p>The new SQL is going to look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #ff0000;">&quot;SELECT value FROM entity_stats WHERE stat_id = (SELECT id FROM stats WHERE display_name = 'foo' OR short_name = 'bar') AND entity_id = 'baz' AND type = 'bat'&quot;</span></pre></div></div>

<p>One of the benefits of doing this is that we&#8217;re actually going to be cleaning up our new DRY stats code a little bit more, too &#8211; we get to trim it down to only take a &#8216;type&#8217; argument, instead of the table and column names it needs to retrieve with. Think back to our DRY stats code from earlier:</p>
<h2>PHP</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> getStatDRY<span style="color: #009900;">&#40;</span><span style="color: #000088;">$tableName</span><span style="color: #339933;">,</span><span style="color: #000088;">$columnName</span><span style="color: #339933;">,</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$trackingID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	createIfNotExistsDRY<span style="color: #009900;">&#40;</span><span style="color: #000088;">$tableName</span><span style="color: #339933;">,</span><span style="color: #000088;">$columnName</span><span style="color: #339933;">,</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$trackingID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT value FROM <span style="color: #009933; font-weight: bold;">%s</span> WHERE stat_id = (SELECT id FROM stats WHERE display_name = '<span style="color: #009933; font-weight: bold;">%s</span>' OR short_name = '<span style="color: #009933; font-weight: bold;">%s</span>') AND <span style="color: #009933; font-weight: bold;">%s</span> = '<span style="color: #009933; font-weight: bold;">%s</span>'&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tableName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$statName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$statName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$columnName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$trackingID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>		
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Perl</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">sub</span> getStatDRY <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$tableName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$columnName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">;</span>
	createIfNotExistsDRY<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$tableName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$columnName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SELECT value FROM $tableName WHERE stat_id = (SELECT id FROM stats WHERE display_name = ? OR short_name = ?) AND $columnName = ?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$value</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">bind_columns</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">\$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetch</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">return</span> <span style="color: #0000ff;">$value</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And here&#8217;s what it looks like with the change made:</p>
<h2>PHP</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> getStatDRY<span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #339933;">,</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$trackingID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	createIfNotExistsDRY<span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #339933;">,</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$trackingID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT value FROM entity_stats WHERE stat_id = (SELECT id FROM stats WHERE display_name = '<span style="color: #009933; font-weight: bold;">%s</span>' OR short_name = '<span style="color: #009933; font-weight: bold;">%s</span>') AND entity_id = '<span style="color: #009933; font-weight: bold;">%s</span>' AND entity_type = '<span style="color: #009933; font-weight: bold;">%s</span>'&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$statName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$statName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$trackingID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>		
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Perl</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">sub</span> getStatDRY <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$type</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$trackingID</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">;</span>
	createIfNotExistsDRY<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$type</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SELECT value FROM entity_stats WHERE stat_id = (SELECT id FROM stats WHERE display_name = ? OR short_name = ?) AND entity_id = ? AND entity_type = ?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$trackingID</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$value</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">bind_columns</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">\$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetch</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">return</span> <span style="color: #0000ff;">$value</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Once we&#8217;ve made those changes, it&#8217;s easy to modify all of our code for the new database structure:</p>
<h2>PHP</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'database.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> getStatDRY<span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #339933;">,</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$trackingID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	createIfNotExistsDRY<span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #339933;">,</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$trackingID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT value FROM entity_stats WHERE stat_id = (SELECT id FROM stats WHERE display_name = '<span style="color: #009933; font-weight: bold;">%s</span>' OR short_name = '<span style="color: #009933; font-weight: bold;">%s</span>') AND entity_id = '<span style="color: #009933; font-weight: bold;">%s</span>' AND entity_type = '<span style="color: #009933; font-weight: bold;">%s</span>'&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$statName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$statName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$trackingID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>		
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> setStatDRY<span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #339933;">,</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$trackingID</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	createIfNotExistsDRY<span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #339933;">,</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$trackingID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPDATE entity_stats SET value = '<span style="color: #009933; font-weight: bold;">%s</span>' WHERE stat_id = (SELECT id FROM stats WHERE display_name = '<span style="color: #009933; font-weight: bold;">%s</span>' OR short_name = '<span style="color: #009933; font-weight: bold;">%s</span>') AND entity_id = '<span style="color: #009933; font-weight: bold;">%s</span>' AND entity_type = '<span style="color: #009933; font-weight: bold;">%s</span>'&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$statName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$statName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$trackingID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> createIfNotExistsDRY<span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #339933;">,</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$trackingID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT count(value) FROM entity_stats WHERE stat_id = (SELECT id FROM stats WHERE display_name = '<span style="color: #009933; font-weight: bold;">%s</span>' OR short_name = '<span style="color: #009933; font-weight: bold;">%s</span>') AND entity_id = '<span style="color: #009933; font-weight: bold;">%s</span>' AND entity_type ='<span style="color: #009933; font-weight: bold;">%s</span>'&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$statName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$statName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$trackingID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// the stat doesn't exist; insert it into the database</span>
		<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO entity_stats(stat_id,entity_id,value,entity_type) VALUES ((SELECT id FROM stats WHERE display_name = '<span style="color: #009933; font-weight: bold;">%s</span>' OR short_name = '<span style="color: #009933; font-weight: bold;">%s</span>'),'<span style="color: #009933; font-weight: bold;">%s</span>','<span style="color: #009933; font-weight: bold;">%s</span>','<span style="color: #009933; font-weight: bold;">%s</span>')&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$statName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$statName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$trackingID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'0'</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h2>Perl</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">package</span> statsDRY<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> database<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> getStatDRY <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$type</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$trackingID</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">;</span>
	createIfNotExistsDRY<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$type</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SELECT value FROM entity_stats WHERE stat_id = (SELECT id FROM stats WHERE display_name = ? OR short_name = ?) AND entity_id = ? AND entity_type = ?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$trackingID</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$value</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">bind_columns</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">\$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetch</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">return</span> <span style="color: #0000ff;">$value</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> setStatDRY <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$type</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$trackingID</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statValue</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">;</span>
	createIfNotExistsDRY<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$type</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;UPDATE entity_stats SET value = ? WHERE stat_id = (SELECT id FROM stats WHERE display_name = ? OR short_name = ?) AND entity_id = ? AND entity_type = ?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$statValue</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$trackingID</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> createIfNotExistsDRY <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$type</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$trackingID</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>	
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SELECT count(value) FROM entity_stats WHERE stat_id = (SELECT id FROM stats WHERE display_name = ? OR short_name = ?) AND entity_id = ? AND entity_type = ?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$trackingID</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$count</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">bind_columns</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">\$count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetch</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$count</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;"># no entry for that stat/user combination - insert one with a value of 0</span>
		<span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;INSERT INTO entity_stats(stat_id,entity_id,value,entity_type) VALUES ((SELECT id FROM stats WHERE display_name = ? OR short_name = ?),?,?,?)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And with that done, our change is made! Now it&#8217;s just a matter of modifying the code we created for each stat specifically(only one shown):</p>
<h2>PHP</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'stats-dry.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> getStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> getStatDRY<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #339933;">,</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> setStat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	setStatDRY<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #339933;">,</span><span style="color: #000088;">$statName</span><span style="color: #339933;">,</span><span style="color: #000088;">$userID</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h2>Perl</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">package</span> stats<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> DBI<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> statsDRY<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> getStat <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">return</span> statsDRY<span style="color: #339933;">::</span><span style="color: #006600;">getStatDRY</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'user'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> setStat <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statValue</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
	statsDRY<span style="color: #339933;">::</span><span style="color: #006600;">setStatDRY</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'user'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$userID</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$statValue</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And once all of the changes are done, you&#8217;re good! Now all of the stat values will be stored in a single table.</p>
<p>But what about users who have already registered for your game, or monsters and items that you&#8217;ve already added?</p>
<p>We definitely don&#8217;t want to have to tell every single person playing our game to re-register &#8211; so we&#8217;re going to write some quick SQL to copy the values over for us, before removing the tables that we no longer need:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> entity_stats<span style="color: #66cc66;">&#40;</span>stat_id<span style="color: #66cc66;">,</span>entity_id<span style="color: #66cc66;">,</span>value<span style="color: #66cc66;">,</span>entity_type<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> stat_id<span style="color: #66cc66;">,</span>user_id<span style="color: #66cc66;">,</span>value<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'user'</span> <span style="color: #993333; font-weight: bold;">FROM</span> user_stats<span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> user_stats;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> entity_stats<span style="color: #66cc66;">&#40;</span>stat_id<span style="color: #66cc66;">,</span>entity_id<span style="color: #66cc66;">,</span>value<span style="color: #66cc66;">,</span>entity_type<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> stat_id<span style="color: #66cc66;">,</span>monster_id<span style="color: #66cc66;">,</span>value<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'monster'</span> <span style="color: #993333; font-weight: bold;">FROM</span> monster_stats<span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> monster_stats;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> entity_stats<span style="color: #66cc66;">&#40;</span>stat_id<span style="color: #66cc66;">,</span>entity_id<span style="color: #66cc66;">,</span>value<span style="color: #66cc66;">,</span>entity_type<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> stat_id<span style="color: #66cc66;">,</span>item_id<span style="color: #66cc66;">,</span>value<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'item'</span> <span style="color: #993333; font-weight: bold;">FROM</span> item_stats<span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> item_stats;</pre></div></div>

<p>Once we&#8217;ve run that SQL, we&#8217;ll have deleted our 3 new tables &#8211; but not before we moved the data over from them to our new <em>entity_stats</em> table. Once you make the necessary modifications to your stats code and upload it all, take a look at your game &#8211; it will still work just like it did before!</p>
<p>This might seem like a weird thing to get excited about, but it&#8217;s a pretty big deal &#8211; we just changed a significant portion of our database, and didn&#8217;t break any of our existing code at all. <strong>This</strong> is why the DRY approach is so useful to have, and why you should always strive to use DRY in your designs to begin with &#8211; because making this change with the database logic spread accross dozens of files would drive even the calmest developer batty.</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/07/11/drying-out-our-stats/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
