Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: external js array file

  1. #1
    Join Date
    May 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb external js array file

    I have an external .js file with array info i.e.

    toys=new Array();
    toys[0]=new Array("ZWS019","House",169.73);
    toys[1]=new Array("ZWS026","Aeroplane",171.43);
    toys[2]=new Array("ZWS034","Car",54.36);
    toys[3]=new Array("ZWS036","Lorry",182.76);
    toys[4]=new Array("ZWS037","Doll",150.95);
    toys[5]=new Array("ZWS047","Soldier",154.12);

    How do I user the document.write object to write all 3 elements of each array to a page and then move on to the next line and do the same.

    I can use document.write to write to a web page if the array has a single element but after that i'm lost.

    Any help you can give is appreciated.

    Thanks

    ACT

  2. #2
    Join Date
    Apr 2006
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hello actkillerby,

    One way to do that is to use the javascript join function with document.write and display your data into an HTML table like below:

    Code:
    toys=new Array();
    toys[0]=new Array("ZWS019","House",169.73);
    toys[1]=new Array("ZWS026","Aeroplane",171.43);
    toys[2]=new Array("ZWS034","Car",54.36);
    toys[3]=new Array("ZWS036","Lorry",182.76);
    toys[4]=new Array("ZWS037","Doll",150.95);
    toys[5]=new Array("ZWS047","Soldier",154.12);
    
    document.write('<table cellspacing="0" cellpadding="3" border="1">');
    for(i=0;i<toys.length;i++){
    	document.write('<tr><td>');
    	document.write(toys[i].join("</td><td>"));
    	document.write('</td></tr>');
    }
    document.write('</table>');
    Hope this help!

  3. #3
    Join Date
    May 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Otaku

    That is excellent thanks very much, it answered another question I was going to ask as well, "how would I put the info in a table"

    I'm new to javascript so all this helps.

    Could you answer another question then is possible?

    How would I make a fourth cell in the row, and add a tick box so that a user could select an item like in shop?

    This is for a uni assignment so any help you can give is appreciated

    Regards and thanks

    Alex

  4. #4
    Join Date
    Apr 2006
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    So you want to do a small shopping cart ?

    The first cell in the row is the productID, the second is the product name, the third is the price and the fourth will a checkbox.

    The customer will check a checkbox to add the product to his cart.

    Maybe you want to display the total$ while the customer checked or unchecked these checkboxes. You probably need to get a list of all the productID selected too.

    Example:
    toys[0]=new Array("ZWS019","House","169.73");
    toys[1]=new Array("ZWS026","Aeroplane","171.43");
    toys[2]=new Array("ZWS034","Car","54.36");
    toys[3]=new Array("ZWS036","Lorry","82.76");
    toys[4]=new Array("ZWS037","Doll","150.95");
    toys[5]=new Array("ZWS047","Soldier","154.12");

    If the customer choose the following productID:
    ZWS026 and ZWS036 and also ZWS047

    The total will be: $508.31
    The list of productID selected will be: ZWS026,ZWS036,ZWS047

    Then you will send theses 2 above informations to another page and use a server side script (ASP.Net or PHP) with a secure (SSL) form and the customer will fill that form with his name, shipping adress, credit card etc... and then send all these informations to your credit card processing system.

    Is it what you want to do ?

  5. #5
    Join Date
    May 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi there Otaku

    You have stated exactly what I would like to do. A Small shopping cart where a user selects an item. This is then added to the cart. A running total would help.

    The idea then was to save the information while the user completed their logon details and then use a submit button to send to a php script to process them. I have a php script that will submit the order through formmail as there is no real need to process it further.

    Cheers for this

    I managed to get another cell with checkbox in but dont know how to give the checkbox its own id yet. I used your code to do this as follows

    document.write('<table cellspacing="0" cellpadding="3" border="1">');
    for(i=0;i<toys.length;i++){
    document.write('<tr><td>');
    document.write(toys[i].join("</td><td>"));
    document.write('</td><td><input type = "checkbox"></td></tr>');


    Another part of this is to save the users order in a cookie should they close the page before submitting the selection. I researching cookies now to see what i can find.

    Thanks for all your help

    Alex

  6. #6
    Join Date
    Apr 2006
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hello Alex,

    Nice project!

    Forget the PHP cookie, it's better to use javascript cookie in our case. I will take care of the javascript (shopping cart, cookie) and you take care of the PHP send mail and login stuff.

    I will come back later today with something.

  7. #7
    Join Date
    Apr 2006
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Alex,

    I hope this will give you a good start to your project.

    Online demo:
    http://www.getelementbyid.com/demo/a...erby/cart.html

    Download:
    http://www.getelementbyid.com/demo/a...ctkillerby.zip

    I have placed some explanations into cart.js

  8. #8
    Join Date
    May 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi there Otaku

    Thanks for all you help, Ive looked at the online demo and it works just how I want it to, it really is good of you to help.

    I'll have a go tomorrow and Sunday and see if I can get the rest done. I'll send you a link when I've completed so you can see what I've done.

    Many thanks and if I could i'd treat you to a drink.

    Alex

  9. #9
    Join Date
    Apr 2006
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You are welcome Alex

    Visiting the final result will be a pleasure for me.

    Best regards!

  10. #10
    Join Date
    May 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Hi Martial

    Just working on the final pages now so should have something to show you later this week.

    A couple of things that I didnt notice previously in what I have to do

    Where I have the tick box to "order" the item what I really need is a box in which the user can enter the number of items he would like to order up to a maximun of 999 items. Apologies I didnt read through the requirements completely previously but I couldnt get access to my Uni site until yesterday and I was trying to work from memory.

    If you can help it would be appreciated if not I'll work with what I have.

    Thanks (again)

    Alex

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
  •