Forum Moderators: open

Message Too Old, No Replies

Need Script to Compare Rows of CSV File

Search for script to read flat file and display row-level results.

         

Skippyo

8:29 pm on May 22, 2006 (gmt 0)

10+ Year Member



Does anyone know a way to have an HTML eform query a CSV (or equivalent flat file) to compare the data in two columns and return the results from the third column? Example: Column A=Departure Location. Column B=Destination. Column C=Price. Essentially, I'm looking to run some script that can match Column A and B from user-input criteria, and return the row display for the price (Column C). Apologies if this thread request has appeared before. Thanks in advance.

Dijkgraaf

9:40 am on May 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Usually that sort of thing would be done in a scripting language such as ASP, PHP, Perl or Java. I think you will have a tough time of it trying to do it (even it is even possible) in JavaScript.

texmex

7:55 pm on May 28, 2006 (gmt 0)

10+ Year Member



I agree that this type of searching should reside on the server. In case this is not an option for you (maybe all you have is a bit of webspace with no access to cgi)Here's a possible way forward.

Firstly you could include the contents of your CSV file in a hidden IFRAME element on the form page. thusways


<iframe name="csvsource" src="filename.csv" style="visibility:hidden"></iframe>

A word of warning though. You will find it difficult to separate the lines of the csv file once in this IFRAME. I'd suggest that you include an end marker on the end of each line of your CSV file (such as the # symbol).

Now in your javascript you can get a handle to your csv content as follows;


//This get's hold of the text
var csv=frames["csvsource"].document.body.innerHTML;

//This next line will split it into separate lines
var lines=csv.split("#");

//Now itterate through each line
for(var n=0;n<lines.length.n++)
{//split current line into separate fields
var fields=lines[n].split(",");

//Now make the check
if(fields[1]==departure)&&(fields[2]==arrival))
{//this line meets criteria.
alert("found a record");
}


Hopefully the above will give you some ideas, but I really would urge you to put this onto the server side, if possible.

If you are stuck with client side, and have trouble following my suggestion, I could probably knock up an entire example document to show you the separate parts in context. Maybe displaying the possible alternatives to the client, to allow them to choose which one they want.