...

View Full Version : make gold decrease when people buy wood



tylerbe992
12-02-2008, 02:48 AM
weird title i know but didn't know how else to explain this...lol

here's my html code..

<p>GOLD: 100</p>
<p>WOOD: 0</p>
<p>BUY WOOD:</p>
<p>
<label>
<select name="select" id="select">
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
</select>
</label>
</p>
<form id="form1" name="form1" method="post" action="">
<label>
<input type="submit" name="Submit" id="Submit" value="Submit" />
</label>
</form>
<p>&nbsp;</p>


I'm trying to make it so when people buy wood there wood count goes up and there gold count goes down....it sounds easy but I'm kind of new to PHP.

Fou-Lu
12-02-2008, 02:59 AM
What is you're storage type and schema in use?

tylerbe992
12-02-2008, 03:05 AM
my storage type is using a database using mySQL and what do you mean by schema ?

Fou-Lu
12-02-2008, 03:16 AM
How is you're database set up to store this information?

Oh, and do you have any php code to post?

tylerbe992
12-02-2008, 03:19 AM
my database isn't setup at all to store the data yet, and no i don't have any php code but i do have the HTML form as you can see..

tylerbe992
12-02-2008, 03:24 AM
i just want users to choose a number from the drop down list and click submit and the gold decreases and the wood count goes up by whatever number they chose.

Fou-Lu
12-02-2008, 03:30 AM
HTML is useless by itself when it comes to processing. You'll need to design you're database first otherwise you can't track any changes.

Essentially, this is what you'll need to do:


float cost <- product (cost, quantity)
array user <-& SELECT FROM STORAGE FOR USER
if user.gold >= cost then
user.gold -= cost
user.wood += quantity
UPDATE FROM STORAGE FOR USER < user.*
endif

Something like this. Just pseudo code, but without a database design I can't provide you with any usable code.


If you don't need persistent storage, PHP isn't the language to do this with. You'll need javascript, but that will only work while the user is on that page, and the numbers cannot interact with anything else.

tylerbe992
12-02-2008, 03:37 AM
ok sir i have a database!
it has three fields
1. player name
2. gold
3. wood

the type of each one is varchar(12)

Fou-Lu
12-02-2008, 04:15 AM
I would probably make those fields numerical, either int or decimal depending on if you need to track a decimal point or not.

How are you tracking you're users? Through sessions? You'll need to be able to track who you're updating as well. So you'll need an authentication system in the long run too.
I'll assume you're [planning on ]using sessions for this.


<?php

if (isset($_POST['submit']))
{
$con = mysql_connect('host', 'user', 'pass') or die(mysql_error());
mysql_select_db('dbname', $con);

$who = $_SESSION['playername'];
$obCurUser = mysql_query('SELECT * FROM yourplayertable WHERE playername = \'' . mysql_real_escape_string($who) . '\'') or die('Unable to query: ' . mysql_error());
if (mysql_num_rows($obCurUser) == 1)
{
$aRecord = mysql_fetch_assoc($obCurUser);
$fWoodCost = yourpriceforwood;
if (isset($_POST['select']))
{
$iQuantity = (int)$_POST['select'];
$fCost = $fWoodCost * $iQuantity;
if ($aRecord['gold'] >= $fCost)
{
mysql_query('UPDATE yourplayertable SET gold = gold - ' . $fCost . ', wood = wood + ' . $iQuantity . ' WHERE playername = \'' . $aRecord['playername'] . '\'') or die('Cannot update: ' . mysql_error());
print 'Successfully purchased ' . $iQuantity . ' wood.';
}
else
{
print 'You do not have the gold to purchase ' . $iQuantity . ' wood.';
}
}
}
else
{
print 'No user found.';
}
}



Something like this.

It would be a lot better for you to seek a tutorial on PHP and MySQL integration and PHP session handling. Learning to do what you need to do means you'll be able to alter it when you need to.

tylerbe992
12-02-2008, 08:44 PM
thxs!
i'll try to work with something like that!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum