By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,660 Members | 1,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,660 IT Pros & Developers. It's quick & easy.

from form to .csv file to webpage help req.

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Fox


StumpY wrote:

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=e...tel.net&rnum=9
Jul 20 '05 #2

P: n/a
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

"Fox" <fo*@fxmahoney.com> wrote in message
news:3F***************@fxmahoney.com...


StumpY wrote:

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=e...tel.net&rnum=9
Jul 20 '05 #3

P: n/a
Hi,

StumpY wrote:
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

"Fox" <fo*@fxmahoney.com> wrote in message
news:3F***************@fxmahoney.com...

StumpY wrote:
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=e...tel.net&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
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #4

P: n/a
Fox


StumpY wrote:

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.



"Fox" <fo*@fxmahoney.com> wrote in message
news:3F***************@fxmahoney.com...


StumpY wrote:

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=e...tel.net&rnum=9

Jul 20 '05 #5

P: n/a
Cheers Fox - but I've got no PHP feature available (MS Hosting)
Stumpy
"Fox" <fo*@fxmahoney.com> wrote in message
news:3F**************@fxmahoney.com...


StumpY wrote:

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.



"Fox" <fo*@fxmahoney.com> wrote in message
news:3F***************@fxmahoney.com...


StumpY wrote:
>
> 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=e...tel.net&rnum=9
Jul 20 '05 #6

P: n/a
Hi Laurent,

a small example would be absolutely blinding :)

Cheers for your help

Stumpy

"Laurent Bugnion, GalaSoft" <galasoft-LB@bluewin_NO_SPAM.ch> wrote in
message news:3f********@news.bluewin.ch...
Hi,

StumpY wrote:
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

"Fox" <fo*@fxmahoney.com> wrote in message
news:3F***************@fxmahoney.com...

StumpY wrote:

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=e...tel.net&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
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #7

P: n/a
Hi,

StumpY wrote:
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>

--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #8

P: n/a
Hi,

StumpY wrote:
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
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #9

P: n/a
Thank you very much - a lifesaver!!

"Laurent Bugnion, GalaSoft" <galasoft-LB@bluewin_NO_SPAM.ch> wrote in
message news:bm**********@rex.ip-plus.net...
Hi,

StumpY wrote:
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>

--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #10

P: n/a
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

"Laurent Bugnion, GalaSoft" <galasoft-LB@bluewin_NO_SPAM.ch> wrote in
message news:bm**********@rex.ip-plus.net...
Hi,

StumpY wrote:
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>

--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #11

P: n/a
Hi,

StumpY wrote:
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
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #12

P: n/a
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

"Laurent Bugnion, GalaSoft" <galasoft-LB@bluewin_NO_SPAM.ch> wrote in
message news:3f**********@news.bluewin.ch...
Hi,

StumpY wrote:
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
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #13

P: n/a
Hi,

StumpY wrote:
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
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #14

P: n/a
Hi Laurent,
just got the following from my ISP - any help?

Cheers,

StumpY

FileSystemObject

Description

Provides access to a computer's file system.

Syntax

Scripting.FileSystemObject

Remarks

The following code illustrates how the FileSystemObject is used to
return a TextStream object that can be read from or written to:

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True)
a.WriteLine("This is a test.")
a.Close

In the code shown above, the CreateObject function returns the
FileSystemObject (fs). The CreateTextFile method then creates the file as a
TextStream object (a), and the WriteLine method writes a line of text to the
created text file. The Close method flushes the buffer and closes the file.

"Laurent Bugnion, GalaSoft" <galasoft-LB@bluewin_NO_SPAM.ch> wrote in
message news:3f**********@news.bluewin.ch...
Hi,

StumpY wrote:
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
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.