Results 1 to 8 of 8

Thread: Array function only works first time?

  1. #1
    Join Date
    Nov 2009

    Question Array function only works first time?

    Not sure why this function works the first time but doesn't the second or third time. Any advice greatly appreciated. This is called with ul li tag onclick which sends an ajax get call to the database so the array can only contain upto 3 matching indexes. I should be able to urencode the array.join(' ') and send it to the server for pagination technique with ajax Get request, unfortunatley after the first round the array.join will produce 'art' 'art' 'silver'. Which I could break down on the server side with the php file but I would preffer that the array arrived as per the function. ie.
    eather of the following
    1. 'art'
    2. 'art' 'art'
    3. 'art' 'art' 'art'
    1. 'art'
    2. 'art' 'silver'
    3. 'silver' 'art' 'silvergold'
    Function Counter()
    function counter()
    else if(count.length==2 && count[0]!==count[1])
    alert(count.join(' '));//Demonstrates flaw
    else if(count.length==3 && count[0]!==count[1])
    alert(count.join(' '));
    else if(count.length==3 && count[1]!==count[2])
    alert(count.join(' '));
    else if(count.length>3)
    alert(count.join(' '));
    Not sure why it doesn't work beyond first time arround. Thanks again for any advice.
    Last edited by THEFOOL; 04-21-2012 at 11:56 AM.

  2. #2
    Join Date
    Nov 2009

    Thumbs up Array function only works first time?

    OK. I have it working better. I had to call the function 'counter()' twice, once at the begining of the onclick then at the end. It seems to work like a dream now. Hope I can pass the array.join in the Get httprequest!!!

  3. #3
    Join Date
    Dec 2005


    I see from your last post that you have solved your problem to your satisfaction.

    But, I'm not sure I understood the problem to begin with, so I wrote a small test program
    that does not rely on ajax to confound the problem with external files, servers, etc.

    Could you give an example entry into this program that "Demonstrates the flaw" as you state?

    <title> Untitled </title>
    <script type="text/javascript">
    var count = [];
    function counter() {
      if(count.length==1) { return; }
      else if(count.length==2 && count[0]!==count[1]) {
           alert(count.join(' '));  // Demonstrates flaw
      else if(count.length==3 && count[0]!==count[1]) {
           alert(count.join(' '));
      else if(count.length==3 && count[1]!==count[2]) {
           alert(count.join(' '));
      else if(count.length>3) {
           alert(count.join(' '));
    function testEntry() {
      count = document.getElementById('TBox').value.split(' ');
    function currEntry() {
      document.getElementById('Tdiv').innerHTML = count.join();
    <style type="text/css">
    Pseudo-ajax information 
    <input type="text" id="TBox" value="" size="40">
    <br><button onclick="testEntry()">Test Entry</button>
    <button onclick="currEntry()">Current COUNT Contents</button>
    <button onclick="document.getElementById('Tdiv').innerHTML=''">Clear Display</button>
    <div id='Tdiv' style="border:1px solid blue; height:100px; width:300px;"></div>

  4. #4
    Join Date
    Nov 2009

    Question Array function only works first time?

    JMRKER, wow great mockup. I'm not sure that the sql created the problem? The array when alerted should have read like this for each li tag with the same id and excepted the change of li tag id clicked that would / should make the array length equal 1.
    PER CLICKS 1,2,3

    1. 'art'
    2. 'art' 'art'
    3. 'art' 'art' 'art'
    Before I put the counter() function in the onclick() function twice, one at the begining, the other near the end. I would get results like this.
    PER CLICK 1,2,3,4

    1. 'art'
    2. 'art' 'art'
    3. 'art' 'art' 'silver' should just be 'silver'
    4. 'art' 'art' 'silver' 'silvergold' should just be 'silvergold' on first 4 clicks it was. Second 4 clicks created this output?

    Not sure if this will demonstrate/clarify but this is the onclick

    var count=new Array();
    function counter()
    else if(count.length==2 && count[0]!==count[1])
    else if(count.length==3 && count[0]!==count[1])
    else if(count.length==3 && count[1]!==count[2])
    else if(count.length>3)
    //my test of counter
    function countlength()
    alert('Counter 0: '+count[0]+'\nCounter 1: '+count[1]);
    alert('Counter 0: '+count[0]+'\nCounter 1: '+count[1]+'\nCounter 2: '+count[2]);
    if(count.length==3 && count[0]==count[1] && count[1]==count[2])
    var products=new Array();
    var sidebarli=document.getElementById('sidebar').getElementsByTagName('li');   
    for(var i=0;i<sidebarli.length;i++)
    if(sidebarli[i].className=='items' && sidebarli[i].parentNode.className=='prod')//check ul tags className prod
    var p=this.id;
    counter();//first application failed on second alert of count array
    var str=p.slice(0,1).toUpperCase() + p.slice(1);
    var pp=this.parentNode.id;
    var str2=pp.slice(0,1).toUpperCase() + pp.slice(1);
    alert(str2+', '+str+': Is sending your request.');
    counter();//second application worked a charm
    getproduct(p);//the Ajax call for getproduct.php using GET
    logo.title='www. Our .Com \"'+str2+', '+str+'\" page.';
    document.title='www. Our .Com '+str2+' '+str;
    //alert('The document title is now: '+document.title);
    f1.scrollTop;//display div f1 innerTML = responseText 
    //probably counter() function here only?
    //countlength(); my test
    //alert(count.join(' ')); another simpler test
    //goes on to ul tags not holding getproduct() ajax call
    Hope that clarifies a very convoluted scenario. Screen shot wouldn't as the side bar li tags are hidden untill the ul mousover.
    Let me know if you think it was the sql or the rest of the crud in the onclick. Thanks.
    Last edited by THEFOOL; 04-23-2012 at 06:30 PM.

  5. #5
    Join Date
    Dec 2005


    I don't know anything about SQL, so I'll be of no help there.
    The mock-up in may last post was only to try to simulate an SQL retrieval and then process the information.
    Does my mock-up do that for you?
    If it does not, then we can address the JS logic further. Once solved, you can move the logic into your program.

    However, you stated in your last post that your program did the following:
    The array when alerted should have read like this for each li tag with the same id ...
    That would lead to some errors in JS. Every id value MUST be unique, they cannot be the same.
    You don't show the HTML portion of your code so I don't know if that is where the problem is located.

  6. #6
    Join Date
    Nov 2009

    Question Array function only works first time?

    Putting the counter() function at the end of the onclick got it working properly so far. Here is the HTML for the #sidebar div. All li tags and Uls have DIFFERENT ids. I still have to examin you're mockup but the key point is that the counter() function is identical to mine and doe's not produce wrong output after the 6th click? ie. after the sith click, if count array length = 3, all three indexes are equal and the 7th click produces an index unequal to the three existing then count array length = 1. If the 8th click produces an unequal index then count array length = 1 with the index of the id of the 8th click. If the 8th click produces an equal index then count array length = 2 equal indexes etc. but never is count array length to be greater than 3?
    HTML Code:
    <div id="sidebar">
    <!--===============================#bags ul================================-->
    <ul id="bags" class="prod">
    <li class="top" title="Beautiful Bags Made in Mexico!">Bags &amp; Purses</li>
    <li class="items disabled" id="leatherbags" title="Coming soon.">Leather</li>
    <li class="items" id="syntheticbags" title="Synthetic Bags, Made in Mexico.">Synthetic</li>
    <li class="items" id="wovenbags" title="Woven Bags Made in Mexico.">Woven</li>
    <!--================================#boys ul===============================-->
    <ul id="boys" class="prod">
    <li class="top" title="Handsom Boys Ware Made in Mexico!">Boys</li>
    <li class="items disabled" id="boyaccessories" title="Coming soon.">Accessories</li>
    <li class="items disabled" id="boyjackets" title="Coming soon.">Jackets</li>
    <li class="items" id="boyshirts" title="Style, Tailoring &amp; Price.">Shirts</li>
    <li class="items disabled" id="boyshoes" title="Coming soon.">Shoes</li>
    <li class="items disabled" id="boyshorts" title="Coming soon.">Shorts</li>
    <li class="items disabled" id="boyslacks" title="Coming soon.">Slacks</li>
    <li class="items disabled" id="boysuits" title="Coming soon.">Suits</li>
    <!--=============================#decor ul===========================-->
    <ul id="decor" class="prod">
    <li class="top" title="Household furnishing &amp; decoration.">D&eacute;cor</li>
    <li class="items" id="art" title="Gloria&rsquo;s Choice.">Art</li>
    <li class="items" id="baskets" title="Hand Made by Mexican Artisans.">Baskets</li>
    <li class="items" id="books" title="About Mexican Artisans and their crafts.">Books</li>
    <li class="items" id="carpetrug" title="Fabulous style and craftsmanship.">Carpets &amp; Rugs</li>
    <li class="items" id="dinning" title="Beautiful collection of dinning regalia.">Dinning</li>
    <li class="items" id="figurines" title="Beautiful collection of figurines.">Figurines</li>
    <li class="items" id="lamps" title="Standup &amp; sconce candle lamps.">Lamps</li>
    <li class="items" id="planters" title="Exquisite Pot and Vase collection.">Planters</li>
    <li class="items" id="wallmasks" title="Exquisite Wall Masks collection.">Wall Masks</li>
    <!--==============================#girls ul================================-->
    <ul id="girls" class="prod">
    <li class="top" title="Fine girls ware, Made in Mexico.">Girls</li>
    <li class="items disabled" id="girlaccessories" title="Coming soon.">Accessories</li>
    <li class="items" id="girlblouses" title="Fashion, Tailoring &amp; Price.">Blouses</li>
    <li class="items" id="girldresses" title="Beautiful, casual comfort.">Dresses</li>
    <li class="items disabled" id="girlensembles" title="Coming soon.">Ensembles</li>
    <li class="items disabled" id="girljackets" title="Coming soon.">Jackets</li>
    <li class="items disabled" id="girlshoes" title="Coming soon.">Shoes</li>
    <li class="items disabled" id="girlshorts" title="Coming soon.">Shorts</li>
    <li class="items disabled" id="girlskirts" title="Coming soon.">Skirts</li>
    <li class="items disabled" id="girlslacks" title="Coming soon.">Slacks</li>
    <li class="items disabled" id="girltunics" title="Coming soon.">Tunics</li>
    <!--=============================#jewelry ul===========================-->
    <ul id="jewelry" class="prod">
    <li class="top" title="Beautiful Jewelry Made in Mexico!">Jewelry</li>
    <li class="items" id="silver" title="95.5% Silver Jewelry Made in Mexico!">Silver</li>
    <li class="items" id="silvergold" title="Silver &amp; Gold Jewelry, Made in Mexico!">Silver &amp; Gold</li>
    <li class="items" id="silverstones" title="Coming soon.">Silver &amp; Stones</li>
    <!--==============================#mens ul=============================-->
    <ul id="mens" class="prod">
    <li class="top" title="Handsom Mens Ware Made in Mexico.">Men</li>
    <li class="items disabled" id="menaccessories" title="Coming soon.">Accessories</li>
    <li class="items disabled" id="menjackets" title="Coming soon.">Jackets</li>
    <li class="items" id="menshirts" title="Style, Tailoring &amp; Price.">Shirts</li>
    <li class="items disabled" id="menshoes" title="Coming soon.">Shoes</li>
    <li class="items disabled" id="menshorts" title="Coming soon.">Shorts</li>
    <li class="items disabled" id="menslacks" title="Coming soon.">Slacks</li>
    <li class="items disabled" id="mensuits" title="Coming soon.">Suits</li>
    <!--==============================#womens ul=============================-->
    <ul id="womens" class="prod">
    <li class="top" title="Fine Women&rsquo;s ware, Made in Mexico.">Women</li>
    <li class="items" id="womenaccessories" title="Extraordinary Robes collection.">Accessories</li>
    <li class="items" id="womenblouses" title="Extrordinary Tailoring &amp; Price.">Blouses</li>
    <li class="items" id="womendresses" title="Beautiful, casual comfort.">Dresses</li>
    <li class="items disabled" id="womenensembles" title="Coming soon.">Ensembles</li>
    <li class="items disabled" id="womenshoes" title="Coming soon.">Shoes</li>
    <li class="items disabled" id="womenshorts" title="Coming soon.">Shorts</li>
    <li class="items disabled" id="womenslacks" title="Coming soon.">Slacks</li>
    <li class="items" id="womentunics" title="Coming soon.">Tunics</li>
    <!--===========================#sitelinks ul===========================-->
    <ul id="sitelinks" class="site">
    <li id="site" class="top" title="www.BoutiqueWholeSaleItems.com Site Links.">Site Links</li>
    <li class="items" id="home2" title="Click and Start All Over Again Easily!">Home</li>
    <li class="items disabled" id="products2" title="Boutique Wholesale Items Products Page.">Products</li>
    <li class="items" id="testimonials2" title="Testimonials from Our Fans!">Testimonials</li>
    <li class="items" id="aboutus2" title="About Boutique Wholesale Items.Com &amp; Mexicali Rose!">About Us</li>
    <li class="items" id="faq2" title="Mexicali Rose Order Faqs!">Order FAQ</li>
    <li class="items" id="contactus2" title="Please Feel Free to Contact Us.">Contact Us</li>
    <li class="items" id="mexicali2" title="You must complete an order to become a Mexicali Rose Member.">MyMexicali</li>
    <!--=========================#searchbycode ul===========================-->
    <ul id="searchbycode" class="bwi">
    <li id="code" class='top' title="Search By Item Code!">Item Code?</li>
    <li id="searchlist" class="items" title="Please enter the Item Code from Your Mexicali Rose recipt">
    <button id="getcode">Find it</button>
    <input type="text" size="5" name="searchcode" id="searchcode" value="Code..."/>
    <!--=========================END #SIDEBAR DIV============================-->
    The html comments should be removed as IE8 sees them as childNodes. None of the li tags has matching ids.

  7. #7
    Join Date
    Jan 2011
    are you still using "doctype xhtml 4.0" on your page?

  8. #8
    Join Date
    Nov 2009

    Question Array function only works first time?

    TroyIII Hey bud, sorry I haven't changed the doctype because I'm still using embeded tags to play sounds. Do you think it would affect array methods?

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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.18088 seconds
  • Memory Usage 2,966KB
  • Queries Executed 13 (?)
More Information
Template Usage (35):
  • (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
  • (3)bbcode_code
  • (2)bbcode_html
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (8)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)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