SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict say's Avatar
    Join Date
    Sep 2003
    Location
    At work
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript mini cart

    Hi,

    I currently have a school assignment on using javascript to create a mini shopping cart. Its more than about done but stuck with just one problem.

    My cookie is currently storing items in this convention:

    Fish pendant|Hearts pendant|Fish pendant|

    By using split("|"), I'm able to iterate the items in a loop and populate into an HTML table. HOwever my problem now is that I need to have a Qty field, if there are 2 items the same for example "Fish pendant", how to I track in the loop and do an increment count for the particular item?

    Any help greatly appreciated.

    Currently my code is:

    PHP Code:
    var product getCookie'product' );
        
        if( 
    product != null )
        {
            var 
    str '<table width="72%" border="0">' 
              
    '<tr>' 
                
    '<td width="54%">Title</td>' 
                
    '<td width="17%">Price</td>' 
                
    '<td width="29%">Qty</td>' 
              
    '</tr>'
            
            var 
    cookie_data = new Array();
                
    cookie_data product.split("|");

            for( var 
    i=0i<cookie_data.count( )-1i++ )
            {
                
    item_ cookie_data[i];
                
    price item_.split("-");

                
    str += '<tr>' +
                        
    '<td>' price[0] + '</td>' +
                        
    '<td>' price[1] + '</td>' +
                        
    '<td></td>' +
                      
    '</tr>';
            }
              
            
    str += '</table>';

            
    document.getElementById("cartTable").innerHTML str;
        } 

  2. #2
    secure webapps for all Aleksejs's Avatar
    Join Date
    Apr 2008
    Location
    Riga, Latvia
    Posts
    755
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, a simple solution (using functions, you already are familiar with) would be to use:
    Fish pendant;2|Hearts pendant;1
    First you split by "|" to get Item;count and then split by ";"
    That way you can increase or decrease count.

  3. #3
    SitePoint Addict say's Avatar
    Join Date
    Sep 2003
    Location
    At work
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Aleksejs View Post
    Hi, a simple solution (using functions, you already are familiar with) would be to use:
    Fish pendant;2|Hearts pendant;1
    First you split by "|" to get Item;count and then split by ";"
    That way you can increase or decrease count.
    Hi, I believe I need to use array to track and count? I tried the following but keeps getting variable is undefined.

    PHP Code:
    // product = Fish pendant|Hearts pendant|Fish pendant|

            
    var container = new Array();
                
    container product.split("|");

            for( var 
    i=0i<cookie_data.count( )-1i++ )
            {
                if( 
    typeofcontainer[container[i]] == 'undefined' )
                {
                    
    container[container[i]] = 1;
                }
                else
                {
                    
    container[container[i]]++;
                }
            } 

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your for loop refers to cookie_data? Should it not be container?

    Code javascript:
    var a = 'a|b|c|d|a|a|b|c';
                    var items = a.split('|');
                    var result = new Array();
                    for(var i = 0; i < items.length; i++) {
                        if(typeof(result[items[i]]) != 'undefined')
                            result[items[i]]++;
                        else
                            result[items[i]] = 1;
                    }


  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    You're wanting to do a quantity count for each item
    Code javascript:
    var qty = [];
    for ( i = 0; i < cookie_data.length - 1; i += 1) {
    	qty[cookie_data[i]] = qty[cookie_data[i]] + 1 || 1;
    	console.log(qty[cookie_data[i]]);
    }
    // ['Fish pendant': 1, 'Hearts pendant': 2];

    cookie_data[i]] = qty[cookie_data[i]] + 1 || 1;
    The above increments the value, and if there is no valid value (eg. first time use) it sets it to 1.

    Then for each item you want to display it

    Code javascript:
    var item_;
    for( item_ in qty )
    {
    	price = item_.split("-");
     
    	str += '<tr>' +
    			'<td>' + price[0] + '</td>' +
    			'<td>' + price[1] + '</td>' +
    			'<td>' + qty[item_] + '</td>' +
    		  '</tr>';
    }
     
    str += '</table>';
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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