...

View Full Version : build csv with var or array?



Boxhead
03-22-2004, 04:59 PM
I need to create a csv field in a form, for dates such as -

2302,2402,2502,0103,0203......

I have a popup calendar where users can select different dates and a javascript sets the bgcolor of that cell to blue, they can also de-select their selections.

When they have selected all the dates they require, I need the user to click a button which populates the field in the parent with the csv info (as above).

The id of each date is the info I require, so 3rd of jans id in 0301 etc.

I want to know the best way to build the info - the options I thought of are -

A. To add to an array each time a date is selected and search/delete from the array if one is deselected - when the user pushes the final button, the page builds the csv info by looping the array and then sends the info to the form field.

B. Or, create the csv as a var on the fly, so each selected date is added to the end of the var (with the comma added). Deselected dates could be removed using a reg exp?

Which of these sound like it would work/work best. Any other suggestions?
monkey

sad69
03-22-2004, 06:27 PM
I think we'd have to see your popup calendar and what values it returns before we can say anything about your solutions.

Also what do you mean by "ids"? Like does each cell have an id in your popup calendar? If you can do that then you're half-way there.

Sadiq.

Boxhead
03-23-2004, 10:01 AM
work so far can be seen here

http://www.splash-wiltshire.org.uk/date_picker_v1.asp

glenngv
03-23-2004, 10:27 AM
Create a "hash array" using the id as the key. You will add the key as you select each date. You will delete the key when you de-select it.


var arr = new Array();
function hilite(cell,weekend){
if(document.getElementById(cell).bgColor == "#3399ff" && weekend){
document.getElementById(cell).bgColor = endcolor;
arr[cell] = cell;
}else if(document.getElementById(cell).bgColor == "#3399ff" && !weekend){
document.getElementById(cell).bgColor = "#ffffff";
delete arr[cell];
}else{
document.getElementById(cell).bgColor = "#3399ff";
arr[cell] = cell;
}
}


Then you access the items like this:


for (var item in arr){
alert(arr[item]);
//or just: alert(item)
}

Boxhead
03-23-2004, 11:08 AM
Glenn - excellent!!

I thought this was going to be tricky, but was the easiest bit, I didint think of using the id for the array key:o

What do you mean by a "hash" array?

Is there any way I can put the array in numeric order? I tried

arr.sort() but this then returned 1,2,3....

cheers

PS while your at it, could you have a look at the cellspacing below the days in feb and aug - any ideas why this is bigger? I have tried all sorts, it seem to be related to months that start on the sunday?:confused:

glenngv
03-23-2004, 11:26 AM
You can't sort a "hash" array. Why do you have to? If you really need it to be sorted then put the items in the hash array into an index-based array and do the sorting.

A hash array stores name-value pairs (referred to as the key and item respectively) unlike in normal array which is index-based. Hash arrays are the same as Hashtable in Java, Dictionary in VBScript.

With regard to the cellspacing, you have two opening <tr> tags in Feb and Aug.

<TR>
<TR>

Boxhead
03-23-2004, 11:55 AM
Glenn

are you using a view source bookmarklet? if so I am getting 2 <tr>s as well, but cant work out why?:confused:

cheers

glenngv
03-23-2004, 12:02 PM
Yes I am.

Take a closer look at your code. You also have two closing </tr> tags.


day_title("Sat")
document.write("</tr><tr>")
...
// fill the remaining weeks
while (day <= month_length) {
for (var i=1;i<=7 && day<=month_length;i++){
//create the date with zero at start if below 10th
caldate=day+month_date;
if(i==1){
document.write("<tr><td align=center ...");
...
document.write("</tr>")
}
document.write("</tr></table>")

Boxhead
03-23-2004, 01:29 PM
I dont get it!

I've made some changes....but I am still getting pairs of unneeded td and tr tags.

I cant work out where my script is going wrong:(

please help!

cheers

glenngv
03-24-2004, 01:32 AM
Now you're getting excess <tr></tr>.

Just remove the code in red.

day_title("Sat")
document.write("</tr><tr>")
// pad cells before first day of month
...
document.write("</tr>")
// fill the remaining weeks
while (day <= month_length) {
...
}

Boxhead
03-24-2004, 09:37 AM
excellent! works fine now....but i'm still confused.

Where are the tr tags for the first week written in in the code? The days tr ends then its straight into td's?

cheers



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum