from form to .csv file to webpage help req.

S

StumpY

HI,
I have set up a page with a form which appends data to a .csv file on my asp
server, this is to enable a limited number of users to add news threads to
this file, which contains the data in the following format
(date,headline,article);
"181003","news title","news article"
"171003","older news title","news article"
The form adds the data in reverse chronological order with newest at the top
of the list.
Now I wish to be able to write the contents of the csv file to a table cell,
formatted the same as the rest of the page (if I use the include tag I get
unformatted text and all the speach marks and no paragraph tag in between
items).
I am new to javascript and asp, although I do have a basic understanding of
how they work.
Could someone kindly explain the best way to achieve this, I'm not looking
for someone to do my hard work for me nessacerely, if it isn't possible to
show me the coding needed, just an explanation will help greatly.
Many thanks in advance
StumpY
 
F

Fox

StumpY said:
HI,
I have set up a page with a form which appends data to a .csv file on my asp
server, this is to enable a limited number of users to add news threads to
this file, which contains the data in the following format
(date,headline,article);
"181003","news title","news article"
"171003","older news title","news article"
The form adds the data in reverse chronological order with newest at the top
of the list.
Now I wish to be able to write the contents of the csv file to a table cell,
formatted the same as the rest of the page (if I use the include tag I get
unformatted text and all the speach marks and no paragraph tag in between
items).
I am new to javascript and asp, although I do have a basic understanding of
how they work.
Could someone kindly explain the best way to achieve this, I'm not looking
for someone to do my hard work for me nessacerely, if it isn't possible to
show me the coding needed, just an explanation will help greatly.
Many thanks in advance
StumpY

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&safe=off&[email protected]&rnum=9
 
L

Laurent Bugnion, GalaSoft

Hi,
N1 - is there any way to automate this procedure? I want my users to have
as little 'access' to the site as possible.
Cheers for the reply


http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&safe=off&[email protected]&rnum=9

Fox didn't see that you have ASP, so what you're looking for is a
server-side solution.

On ASP, the procedure would be to:

1) Load the text file, using the FileSystemObject.
2) For each line, split the line in fields in a JavaScript array, using
the method strLine.split( "," ); where strLine is the line read form the
text file.
3) Build your table using the array's fields in a string.
4) Pass the built string in the Response object, like:
Response.Write( strHtml );

If you have troubles implementing this, ask me and I can provide you
with a small example.

HTH,

Laurent
 
F

Fox

StumpY said:
N1 - is there any way to automate this procedure? I want my users to have
as little 'access' to the site as possible.
Cheers for the reply

The following is OffTopic

Use PHP -- it has a function specifically designed to read csv files.

<?php

// need to get how many records in file
// if your data file uses column labels
// remember to "burn off" the first record!
// this example ASSUMES your csv looks like:

// date,headline,article => column labels
// "181003","news title","news article"
// "171003","older news title","news article"


function
getNumRecords($h)
{
$cnt = -1; // discount column label row
// otherwise $cnt = 0;

while(fgetcsv($h, 10000)) $cnt++;
rewind($h); // reset file pointer to beginning
return $cnt;
}

$handle = fopen("myNews.csv","r");

$cnt = getNumRecords($handle);

echo "<script>\n\n";
echo "var newsdata = [\n\t\t";

fgetcsv($handle, 10000); // burn off labels

for($i = 0; $i < $cnt; $i++)
{
$data = fgetcsv($handle, 10000);
$cols = count($data);
echo "[";

for($j = 0; $j < $cols; $j++)
echo "\"" . $data[$j] . "\"" . ($j < $cols-1 ? "," : "");

echo "]" . ($i < $cnt - 1 ? "," : "") . "\n\t\t";
}

echo "];\n\n";
echo "</script>";

?>


The output from this will look like:

<script>

var newsdata = [
["181003","news title","news article"],
["171003","older news title","news aritcle"]
];


</script>


This PHP script does basically the same thing as the form routine I
linked you to.

The function fgetcsv takes care of stripping the quotes from "cell"
entries containing commas and the number of entries is correctly
returned -- no special handling should be needed [I'm not sure what
happens with quotes used in the data -- you'll probably need to
experiment with that [but I wouldn't be surprised if the function took
care of escaping it for you].

The arguments to fgetcsv are (obviously) the reference returned from
fopen and the *length* of the line to be returned -- note, the length
MUST be greater than the total number of characters in the longest line
within the file if you expect to retrieve all your data.
 
L

Laurent Bugnion, GalaSoft

Hi,
Hi Laurent,

a small example would be absolutely blinding :)

Cheers for your help

Stumpy

Here is a very simple example. You can modify the table layout in the
line 24 and following.

HTH,

Laurent

----------------------------
<%@Language=JavaScript %>

<HTML>
<HEAD>
<TITLE>GalaSoft</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">

<%

var NAME_CSV_FILE = "d:\\temp\\test.txt";

var fso = Server.CreateObject( "Scripting.FileSystemObject" );

if ( !fso.FileExists( NAME_CSV_FILE ) )
{
Response.Write( "The file doesn't exist" );
}
else
{
// 1 = ForReading
var fl = fso_OpenTextFile( NAME_CSV_FILE, 1 );

var strTable = "<TABLE BORDER='1'>\n";

while ( !fl.AtEndOfStream )
{
var strLine = fl.ReadLine();
var astrLine = strLine.split( ',' );

// Remove "" from fields
var strDate
= astrLine[0].substring( 1, astrLine[0].length - 1 );
var strTitle
= astrLine[1].substring( 1, astrLine[1].length - 1 );
var strArticle
= astrLine[2].substring( 1, astrLine[2].length - 1 );

var strTableRow = "<TR>\n"
+ "<TD>" + strDate + "</TD>\n"
+ "<TD>" + strTitle + "</TD>\n"
+ "<TD>" + strArticle + "</TD>\n"
+ "</TR>\n";

strTable += strTableRow;
}

strTable += "</TABLE>\n";

Response.Write( strTable );
}

%>

</BODY>
</HTML>
 
L

Laurent Bugnion, GalaSoft

Hi,
Cheers Fox - but I've got no PHP feature available (MS Hosting)
Stumpy

You could install PHP on your host, solutions are available AFAIK.
However, if you have a MS host anyway, you're better off with ASP and
JScript.

Laurent
 
L

Laurent Bugnion, GalaSoft

Hi,
Lauren
I get a "The page cannot be displayed" (HTTP500) error - I am running this
on my ISP server if that makes a difference?
All I have altered is to replace "d:\\temp\\test.txt" with "text.asp" (all
files in the root of my webspace)

Stumpy

Are you sure that your ISP supports ASP?

Laurent
 
L

Laurent Bugnion, GalaSoft

Hi,
Yep - would the server object be too much of a security risk to run on the
ISP server or is this a common practice?

Stumpy

I suggest that you discuss this with your ISP. On Windows servers (on
others too), it's possible to set pretty much any security restriction
on files and directories. To get access to files, the server must be
registered as a user for the directory the file is in. Without feedback
from your ISP, it's pretty much a wild guess.

Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top