Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    May 2008
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    make gold decrease when people buy wood

    weird title i know but didn't know how else to explain this...lol

    here's my html code..
    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    What is you're storage type and schema in use?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    May 2008
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts
    my storage type is using a database using mySQL and what do you mean by schema ?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    How is you're database set up to store this information?

    Oh, and do you have any php code to post?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New Coder
    Join Date
    May 2008
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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..

  • #6
    New Coder
    Join Date
    May 2008
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    Code:
    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.

    Edit:
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #8
    New Coder
    Join Date
    May 2008
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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)

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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 Code:
    <?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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #10
    New Coder
    Join Date
    May 2008
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts
    thxs!
    i'll try to work with something like that!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •