www.webdeveloper.com
Results 1 to 5 of 5

Thread: Get the number of elements inside another element

  1. #1
    Join Date
    Apr 2009
    Location
    Thessaloniki, Greece
    Posts
    24

    Get the number of elements inside another element

    I'm working on an exercise I need to have finished pretty soon. The concept is simple: online bookstore with cart etc. It's written on php, using JS and mysql.

    Each time I view a book's details, I can add it to my cart, where I can also alter it's quantity.
    The code for my cart is this:

    PHP Code:
    <div style="margin-left:20px;">
        <p><h3>Your list of books to buy:</h3></p>
    <form id="place_order" action="Customer_Menu/plorder.php" method="post">
        <?php
        $cost 
    0;
        if(isset(
    $_SESSION['cart'])){
            foreach(
    $_SESSION['cart'] as $cartLine){ ?>
                <span><b>Title:</b><input type="text" id="title" name="title" value="<?php echo $cartLine['title']; ?>" size="50" disabled="disabled" style="background-color:#666633;border:none;color:white;"/><br/>
            <b>Price:</b><input type="text" id="price" name="price" value="<?php echo $cartLine['price']; ?>" size="1" disabled="disabled" style="background-color:#666633;border:none;color:white;"/>$<br/>
                    <b>Quantity:</b><select id="quant" onchange="javascript:ManageItem(this);">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
            </select></span><hr/>
                <?php $cost += $cartLine['price']*$cartLine['quantity']; 
            }
        }
        
    ?> 
           Total Cost: <input type="text" id="total" name="total" size="1" value="" style="background-color:#666633;border:none;color:white;" disabled="disabled"/>$<br/>
        <input type="submit" id="order_btn" name="order_btn" value="Procceed to order!"/>
    </form>
    </div>
    As you can see, each time I choose a book to add it to my cart, a new span will be created, to hold the info of this book. What I need to do with the ManageItem js function, is to update the total cost at the bottom of the page, but I can't seem to be getting anywhere, so any help will be mostly appreciated.

    Here is what I've done so far:

    Code:
    function ManageItem(x){
    	var my_spans = document.getElementsByTagName("span");
    	var my_prices = new Array();
    	var my_quantity = new Array();
    	var total = 0;
    
    	for(var i=0;i<my_spans.length;i++){
    		//working with each span seperate
    		var this_span = my_spans[i].childNodes;
    
    		my_quantity[i] = x.selectedIndex.value;
    		my_prices[i] = parseInt(this_span.document.getElementById("price").value)*parseInt(my_quantity[i]);
    	}
    	for(var j=0;j<my_prices.length;j++){
    		total +=my_prices[j]; 
    	}
    	document.getElementById("total").value = total;
    	
    }
    Last edited by salvi; 11-27-2011 at 05:59 AM.

  2. #2
    Join Date
    Apr 2009
    Location
    Thessaloniki, Greece
    Posts
    24
    Changed my JS code to this:

    Code:
    function ManageItem(){
    	var my_spans = document.getElementsByTagName("span");
    	var this_span = new Array();
    	var my_prices = new Array();
    	var my_quantity = new Array();
    	var total = 0;
    
    	for(var i=0;i<my_spans.length;i++){
    		this_span[i] = my_spans[i].getElementsByTagName("*");
    		for(var j=0;j<this_span[i].length;j++){
    			my_quantity[i] = this_span[i].item(2).selectedIndex.value;
    			my_prices[i] = parseInt(this_span[i].item(1).value)*parseInt(my_quantity[i]);
    		}
    	}
    	for(var j=0;j<my_prices.length;j++){
    		total +=parseInt(my_prices[j]); 
    	}
    	document.getElementById("total").value = total;
    }
    But if i try to print my my_quantity[i] to check if the value is assigned correctly, I'm getting "undefined" :/

  3. #3
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    What you might want to do is use a <textarea> to store your cart information.
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

  4. #4
    Join Date
    Apr 2009
    Location
    Thessaloniki, Greece
    Posts
    24
    Wouldn't that be the same as using span?
    Each time I add a product, the page looks like that http://img341.imageshack.us/img341/6369/siteej.jpg

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    For the beginning I see a mistake: you create, in a PHP loop, elements which have the same id (id="title", id="price"...). Unlike the name attribute, the id must be an unique identifier on the same document/session.
    Last edited by Kor; 11-28-2011 at 03:33 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.24419 seconds
  • Memory Usage 2,894KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates