PDA

View Full Version : external js array file



actkillerby
05-10-2006, 02:15 PM
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:)

otaku
05-10-2006, 05:56 PM
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:



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!

actkillerby
05-10-2006, 07:48 PM
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

otaku
05-11-2006, 01:29 PM
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 ?

actkillerby
05-11-2006, 01:52 PM
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

otaku
05-12-2006, 12:19 PM
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.

otaku
05-12-2006, 07:33 PM
Hi Alex,

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

Online demo:
http://www.getelementbyid.com/demo/actkillerby/cart.html

Download:
http://www.getelementbyid.com/demo/actkillerby/actkillerby.zip

I have placed some explanations into cart.js

actkillerby
05-12-2006, 08:03 PM
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

otaku
05-12-2006, 08:18 PM
You are welcome Alex :)

Visiting the final result will be a pleasure for me.

Best regards!

actkillerby
05-16-2006, 09:34 AM
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

otaku
05-16-2006, 04:01 PM
Hello Alex,

I am in my lunch time at work, I will be very busy this afternoon but I will take a look on it tonignt at home and see what I can do.

How many products do you expect to have Alex ? 20, 50, 100 or maybe more ?

I will come back to the forum tonignt or tomorrow with some suggestion for you. I will have probably few more questions for you.

otaku
05-16-2006, 11:51 PM
Hi Alex,

What you can do is to placed your products infos into 1 or more pages (ex: products.html).
Each product will have its own "Quantity" textbox and "Add to Cart" button. Something like below:

products.html


<!-- ProductID: ZWS019 -->
... productImage ...
... productName ...
... productPrice ...
... productDescription ...
<input type="text" maxlength="3" size="3" id="ZWS019" value="1" />
<input type="button" id="bt1" value="Add to Cart" onclick="addToCart('ZWS019')" />

<!-- ProductID: ZWS026 -->
... productImage ...
... productName ...
... productPrice ...
... productDescription ...
<input type="text" maxlength="3" size="3" id="ZWS026" value="1" />
<input type="button" id="bt2" value="Add to Cart" onclick="addToCart('ZWS026')" />

etc...


Once the customer has entered the quantity and has clicked the button, we are going to add the product selected and quantity into the cart. Then the customer will be redirect to the cart.html page.

Into the cart.html page, the customer will be able to:

1) modify the quantity of each product chooses
2) remove one or more products chooses from his cart
3) click a button to continue shopping.
4) click a button to checkout

We are going to save the users order in a cookie like the previous version in case the customer close the page before submitting the order.

Let me know if that solution fit your needs ?

actkillerby
05-17-2006, 07:06 AM
Hi Martial

Hectic at work here and I was too tired to do anything last night.

The solution you have come up with looks like it will work for me.

If you want to email me direct my email is
f l y i n g _ h e r b e r t @ y a h o o . c o. u k
but without the spaces.

I will then send you a copy of exactly what we have been given so you can see what I am trying to do.

There are 71 items in the products list and we do not need to display a picture. They only need to be on one page also.

Let me know on here if you have emailed me as I will need to change PC's at work to access yahoo.

Cheers

Alex

otaku
05-17-2006, 12:16 PM
Hello Alex,

The email has been sent.

Regards