Jump to content
MakeWebGames

More SQL problems...


Damond

Recommended Posts

So my coding partner and I have been working on a bit of an advanced trade system and we just can't seem to get it to work the way it should. He has been sick for several days now and we are really behind on getting this game out to beta testing so I thought I would ask you guys.

The way it works:

Player A wants to trade items with Player B. Using a trade box player A can add items and/or gold. Once this is finished the trade box waits there for player B to add to it. If there is nothing to be added both players must accept the trade before it is completed.

Simple right?

We found a simple version of this here that needed a little re-coding and adjusting to make it work, but the problem is that you could only trade one item at a time and you could only have on trade box at a time. We want player to be able to have as many trades as they want with the ability to have up to 10 items of any amounts as well as gold.

So here is what we have:

CREATE TABLE IF NOT EXISTS `trades` (
 `trID` int(11) NOT NULL AUTO_INCREMENT,
 `trFROM` int(11) NOT NULL DEFAULT '0',
 `trUSER` int(11) NOT NULL DEFAULT '0',
 `trGOLD` int(11) NOT NULL DEFAULT '0',
 `trITEM` varchar(70) NOT NULL DEFAULT '',
 `trQTY` varchar(70) NOT NULL DEFAULT '',
 `trACC` tinyint(1) DEFAULT '0',
 `trRGOLD` int(11) NOT NULL DEFAULT '0',
 `trRITEM` varchar(70) NOT NULL DEFAULT '',
 `trRQTY` varchar(70) NOT NULL DEFAULT '',
 `trRACC` tinyint(1) NOT NULL DEFAULT '0',
 PRIMARY KEY (`trID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;

Coda001.jpg

 

The row trITEM is set to archer(70) so that player A can get more then one item in there, as well as trRITEM for Player B. We did the same with the two QTY rows so that it will hold the amount of each item.

Now all we needed where the functions for viewing, accepting, declining, and making a trade. Making the trade is where we are having a problem.

Here is the function and the form:

function begin_trade() {
   global $db, $ir, $c, $userid, $h;
   if($_POST) {
   $_POST['user'] = filter_var($_POST['user'], FILTER_VALIDATE_INT) ? abs( (int) $_POST['user']) : 0;
   $_POST['gold'] = filter_var($_POST['gold'], FILTER_VALIDATE_INT) ? abs( (int) $_POST['gold']) : 0;
   $_POST['userid'] = filter_var($_POST['userid'], FILTER_VALIDATE_INT) ? abs( (int) $_POST['userid']) : 0;
   $_POST['item'] = filter_var($_POST['item'], FILTER_VALIDATE_INT) ? abs( (int) $_POST['item']) : 0;
   $items = $_POST['item'];
   $qty = $_POST['amt'] = filter_var($_POST['amt'], FILTER_VALIDATE_INT) ? abs( (int) $_POST['amt']) : 0;
   for($i = 1; $i <= 5; $i++) {
       $_POST["item".$i] = filter_var($_POST["item".$i], FILTER_VALIDATE_INT) ? abs( (int) $_POST["item".$i]) : 0;
       $_POST['amt'] = filter_var($_POST['amt'], FILTER_VALIDATE_INT) ? abs( (int) $_POST['amt']) : 0;
       if($_POST['item'.$i] > 0) {
           $items .= ",". $_POST['item'.$i];
           $qty .= ",". $_POST['amt'];
       }
   }
   $us   = $db->query("SELECT * FROM users WHERE userid = {$_POST['user']}");
   if ($db->num_rows($us) == 0) {
       showErrMsg('You are trying to trade with an invalid user!');
   }

   if ($gold > $ir['gold']) {
       showErrMsg('You don\'t have enough gold to trade!');
   }

   if ($qty < 0) {
       showErrMsg('You can\'t trade a negative amount.');
   }
       $db->query("INSERT INTO trades VALUES (NULL,{$userid},{$_POST['user']},{$_POST['gold']},{$items},{$qty},0,0,0,0,0)");
   print "The trade has been entered!";
   exit($h->endpage());
}
?>
<form id="trade" method="post">
   <fieldset style='width:50%;'><legend>Trading</legend>
   Userid: <input type='text' name='user' length='7' maxlength='7' value='0' /> <br />
   <div id="input1" style="margin-bottom:4px;" class="clonedInput">
          Item Needed: <?php echo inv_dropdown($c, "item") ?> x <input type='text' name='amt' size='2' value='0' />
   </div>
   <div>
        <input type="button" id="btnAdd" value="&plus;" />
        <input type="button" id="btnDel" value="−" />
   </div> <br />
       Gold: <input type='text' name='gold' value='0' /> <br />
   </fieldset>
   <input type='submit' value='trade' />
</form>
<?php
}

 

In our form we call to a javascript that is in the header as it is used in several places. This script is what allows us to add multiple items to the trade:

<script type="text/javascript">
   $(document).ready(function() {
       $('#btnAdd').click(function() {
           var num     = $('.clonedInput').length;
           var newNum  = new Number(num + 1);

           var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

           newElem.children(':first').attr('id', 'item_needed' + newNum).attr('name', 'item_needed' + newNum);


           $('#input' + num).after(newElem);
           $('#btnDel').attr('disabled','');

           if (newNum == 10)
               $('#btnAdd').attr('disabled','disabled');
       });

       $('#btnDel').click(function() {
           var num = $('.clonedInput').length;

           $('#input' + num).remove();
           $('#btnAdd').attr('disabled','');

           if (num-1 == 1)
               $('#btnDel').attr('disabled','disabled');
       });

       $('#btnDel').attr('disabled','disabled');
   });
</script>

 

Now everything runs fine except that when you add more then one item to the trade it is not being added to the data base. It is only adding the first item and the first quantity. I have seen this done before with the varchar holding multiple variables I'm just really unsure why it is not working for me now.

Link to comment
Share on other sites

Here is what the trade looks like from the user end:

Coda002.jpg

Here is an example of what I am expecting the Database to look like:

Coda003.jpg

You can see in the items needed on lines 2 and 3 that there are two items listed in the items_needed column as well as in the qty_needed.

This is what I am expecting my code to do but for some reason it is not.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...