3 Replies - 762 Views - Last Post: 01 March 2011 - 12:44 PM

#1 tpgames  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 24-February 11

want shopping cart to send items bought to inventory

Posted 28 February 2011 - 01:35 PM

This is a hobby RPG game in JS. I am not a student.

What I want to do is have the items that player is going to "buy" automatically go to Inventory and have the "total" of the purchase be subtracted from their fake money. I don't want to use any server side scripting here. This is suppose to be a simple JS RPG game using newbie programming. Thanks!


This is part of what I have so far... I'm confused as to what to do next. Normally, this is where the CGI takes over, but I'm not using that, and wouldn't know how to program that anyways. Besides, I don't want any form that allows entering of private data. ;) G stands for Group (or Category). P stands for arbitrary PLU number that I might use as a convienance to distinguish between one almost identical product and another.


var giveControl = false;
var browseControl = false;
var curGLoc = -1;
var curPLoc = -1;
var infoStr = '';
var shoppingBag;
function Bag() {
	this.bagTotal = 0;
	this.things = new Array();
}

// HUGE SNIPPING OUT OF CODE! 


			function changeBag(formObj, showAgain) {
				var tempBagArray = new Array ();
				for (var i = 0; i < shoppingBag.things.length; i++) {
					if (!formObj.elements[ (i * 3) + 2].checked) {
						tempBagArray[ tempBagArray.length] = shoppingBag.things[ i];
						tempBagArray[ tempBagArray.length - 1].itemQty =
						formObj.elements[ ii * 3].selectedIndex + 1;
					}
				}
				shoppingBag.things = tempBagArray;
				if(shoppingBag.things.length == 0) {
					alert("You have emptied you bag. If you want to buy something, then please shop.");
					parent.frames[ 1].showStore();
				}
				else { showBag(); }
			}
			
			function checkOut(formObj) {
				giveControl = false;
				if(!confirm("Do you have the right quantity of what you want to buy?" +
					" Remember that you have to choose Change Bag to " +
				"remove products or change quantities. If so, choose OK to check " +
				"out.")) {
				return'
				}
				if(shoppingBag.things.length == 0) {
					showStore();
					return;




Is This A Good Question/Topic? 0
  • +

Replies To: want shopping cart to send items bought to inventory

#2 tpgames  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 24-February 11

Re: want shopping cart to send items bought to inventory

Posted 28 February 2011 - 03:16 PM

Can't edit my post that I can see... so

what I've remembered is that passing a value from one variable to another might help. So, now the question, how do I pass the data from shoppingBag to inventoryBag without losing the previous data contained within inventoryBag?

My brain is kicking out again. :P How do I convert "inventory cash = $$$" to something JS will subtract the formObj/ bagTotal from? I understand to a point how this code works, but I'm not fully connecting with it.

			function runningTab(formObj) {
				var subTotal = 0;
				for (var i = o; i < shoppingBag.things.length; i++) {
					subTotal += parseFloat(formObj.elements[ (i * 3) + 1].value);
				}
				forumObj.subtotal.value = numberFormat(subTotal);			
				formObj.total.value = numberFormat(subTotal +
				shoppingBag.subTotal = formObj.subtotal.value;
				shoppingBag.bagTotal = forumObj.total.value;
			}
			
			function numberFormat(amount) {
				var rawNumStr = round(amount) + ' ';
				rawNumstr = (rawNumstr.charAt(0) == '.' ? '0' + rawNumStr : rawNumstr);
				if (rawNumstr.charAt(rawNumStr.length - 3) == '.') {
					return rawNumStr
				}
				else if (rawNumStr.charAt(rawNumstr.length - 2) == '.') {
					reutrn rawNumStr + '0';
				}
				else { return rawNumStr + '.00'; }
			}
			function round(number,decPlace) {
				decPlace = (!decPlace ? 2 : decdPlace);
				return Math.round(number * Math.pow(10,decPlace)) /
				Math.pow(10,decPlace);
			}
			
			function changeBag(formObj, showAgain) {
				var tempBagArray = new Array ();
				for (var i = 0; i < shoppingBag.things.length; i++) {
					if (!formObj.elements[ (i * 3) + 2].checked) {
						tempBagArray[ tempBagArray.length] = shoppingBag.things[ i];
						tempBagArray[ tempBagArray.length - 1].itemQty =
						formObj.elements[ i * 3].selectedIndex + 1;
					}
				}
				shoppingBag.things = tempBagArray;
				if(shoppingBag.things.length == 0) {
					alert("You have emptied you bag. If you want to buy something, then please shop.");
					parent.frames[ 1].showStore();
				}
				else { showBag(); }
			}
			
			function checkOut(formObj) {
				giveControl = false;
				if(!confirm("Do you have the right quantity of what you want to buy?" +
					" Remember that you have to choose Change Bag to " +
				"remove products or change quantities. If so, choose OK to check " +
				"out.")) {
				return'
				}
				if(shoppingBag.things.length == 0) {
					showStore();
					return;
				
				var intro = '<h2> Shopping Bag Check Out </h2><form method=post ' +
				var itemInfo = ' ';
				for (var i = o; i < shoppingBag.things.length; i++) {
					itemInfo += '<Input type=hidden name="prod' + i +
					' " Value=" ' + shoppingBag.things[ i].plu + '-' +
					shoppingBag.things[ i].itemQty + ' ">';
				}
				var totalInfo = '<input typ=hidden name="subtotal" value=" ' +
				shoppingBag.subTotal + ' ">' +
				'<input type = hidden name="bagtotal" value=" ' +
				shoppingBag.bagTotal + ' ">';
				
				var footer = '</form></body></html>';
				
				infoStr = header + intro + itemInfo + 
				totalInfo + footer;
				parent.frames[ 0].location.replace('javascript:
					parent.frames[ 1].infoStr');
				}
				
// Question ??? 
// Inventory $ on hand - bagTotal
// shoppingBag => Inventory without wiping out previous inventory. 				
				



Was This Post Helpful? 0
  • +
  • -

#3 tpgames  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 24-February 11

Re: want shopping cart to send items bought to inventory

Posted 01 March 2011 - 12:38 PM

I had this thought, why don't I just simply pass the data in shoppingBag to a cookie, and then have inventoryBag grab that data and concatenate it to the inventoryBag data. I'm using an array code I found because its better for my overall purposes.

Here's the issue, shoppingBag is a variable. So how do I put it in an array? "shoppingBag" won't work, as it will give me the name instead of its data I thought. I'd have inventoryBag and quite possibly other data as well like cards collected, grades, etc. Also, using "shoppingBag" + "inventoryBag" will concatenate it, so that won't work.

Thanks!

function setCookieArray(name){
    this.length = setCookieArray.arguments.length - 1;
        for (var i = 0; i < this.length; i++) {
        data = setCookieArray.arguments[i + 1]
        setCookie (name + i, data, expdate);
        }        
    }

    
    var expdate = new Date();
    expdate.setTime (expdate.getTime() +  (24 * 60 * 60 * 1000 * 365)); 

 //??   var testArray = new setCookieArray( "", " ");


    function getCookieArray(name){
    var i = 0;
        while (getCookie(name + i) != null) {
        this[i + 1] = getCookie(name + i);
        i++; this.length = i; 
        }
    }



Was This Post Helpful? 0
  • +
  • -

#4 tpgames  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 24-February 11

Re: want shopping cart to send items bought to inventory

Posted 01 March 2011 - 12:44 PM

Okay, another issue. Using the cookie Array will not save the data when the page is left. Not useful at all. I'm trying hard to not use PHP as I'm trying to avoid server side programming.
I found this Internal DIC shoppingcart Question. Apparently, "you should use PHP" ended the discussion. I'm only writing a RPG game. :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1