Jump to content
xisto Community
mrdee

Displaying And Formatting Csv Contents

Recommended Posts

Hi,I would like to achieve the following:If people submit data through a web form, and I let that form save the data into a .CSV file, how can Ia) Display the data in a table (that is simple enough), but omit the people's email addresses B) Automatically update the table when new data are added tot the CSV file and display the latest file data upon a page refresh.Especially with respect to point B), as far as I am aware, there are no for-next loops in HTML (or there might be something equivalent in HTML 5) which can display row after row of the table and update the number of the loop when rows are added.The omission of the email address from the table might not be that difficult, but I thought I'd ask anyway, as i have not done anything similar for a long time, and at the moment, i am nt thinking 100% straight (See my other post "Hello everyone").I seem to remember I have done something like this in a grey past, and the solution is probably staring me in the face, but any help will still be very much appreciated.Thank you in advance.

Share this post


Link to post
Share on other sites

This is an example of a good use of PHP to read the csv file, drop the email address information and create the table in an html/css format.In order to do this, you need to be on a server which allows the use of PHP, which the Servers here allow.I have a page which does this for email information, but it is on a flash drive at home and I won't be there until the weekend, so if someone else could assist until then, it would be great. I will forward the code as soon as I get access to it on Saturday or Sunday.

Edited by jlhaslip (see edit history)

Share this post


Link to post
Share on other sites

Thank you for that, Jim.I opted for a CSV file to avoid having to put all the data in a MySQL database, but I notice now (probably was not thinking clearly yet, as I mentioned), that I will still have to resort to PHP in order to display the data.I have code available to display the data from a MySQL database on a website, I assume, with some minor adjustments it will roughly be the same for achieving the same with the contents of a CSV file, or am I wrong?Thank you for your reaction.

Share this post


Link to post
Share on other sites

This is an example of a good use of PHP to read the csv file, drop the email address information and create the table in an html/css format.In order to do this, you need to be on a server which allows the use of PHP, which the Servers here allow.

I have a page which does this for email information, but it is on a flash drive at home and I won't be there until the weekend, so if someone else could assist until then, it would be great. I will forward the code as soon as I get access to it on Saturday or Sunday.


I'm not quite sure how this would work. CSV just means "comma separated values," right? So basically what you would do is:

1) Read in CSV file
2) Parse it, breaking apart the commas
3) Add data parts that aren't email to a table (ex. if it goes name, email, then you'd just be reading every odd array value) and output it?
Edited by rpgsearcherz (see edit history)

Share this post


Link to post
Share on other sites

By the way, on another note, I also wonder why I was required to enter a security code in order to get my reply through, and even more, why my reply to this post has not appeared here.

Share this post


Link to post
Share on other sites

By the way, on another note, I also wonder why I was required to enter a security code in order to get my reply through, and even more, why my reply to this post has not appeared here.


I can read this current post, if that's what you mean (or did you mean another one that was in response to this one?). As for the security captcha... I have no idea. Were you logged in at the time?

I've never had a captcha here.

Share this post


Link to post
Share on other sites

That was probably the reason, and, no, it was not the post you quoted.I noticed later on I was not logged in.(No idea why I got logged out, though, as I never logged out).Thank you anyway.

Share this post


Link to post
Share on other sites

That was probably the reason, and, no, it was not the post you quoted.
I noticed later on I was not logged in.
(No idea why I got logged out, though, as I never logged out).

Thank you anyway.


The random log-out happens to me from time to time as well. Sometimes I'll just leave the browser up, even on the index, come back a little while later and upon clicking on a topic I'll have logged out. Not sure what causes it, as sometimes I can be logged in for days or weeks with no problems.

Share this post


Link to post
Share on other sites

Same here, oddly enough.I usually stay logged in for quite some time, unless I clear the cache on my machine or something like that.(I rarely or never log out myself).

Share this post


Link to post
Share on other sites

I'm not quite sure how this would work. CSV just means "comma separated values," right? So basically what you would do is:
1) Read in CSV file
2) Parse it, breaking apart the commas
3) Add data parts that aren't email to a table (ex. if it goes name, email, then you'd just be reading every odd array value) and output it?

Correct. And the CSV file will be read one line at a time. Treat the data similar to a MYSQL results array.

Share this post


Link to post
Share on other sites
}$handle = @fopen($file_name, "r");if (!$handle) {	 echo "File Handle Not Available For Use"; exit;	 };?><table summary="Posted Comments" ><tr class="hi_lite">	 <th>Date and Time</th>	 <th>Sender</th>	 <th>Email</th>	 <th>Message</th><?phpif ($show_IP){echo '<th>IP Address</th>';};echo '</tr>';$row=0 ;while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {if ($row % 2) { $data_string .= '<tr class="hi_lite"> ';}else { $data_string .= '<tr> ';}if ($show_IP){$num = count($data);	 }		 else {$num = (count($data) - 1 );			 };			 for ($c=0; $c < $num; $c++) {	 $data_string .= '<td> ' . $data[$c] . " </td>" ;		 };$data_string .= '</tr> ' . "rnt" ;$row++;};	 print $data_string ;	 $data_string = "";fclose($handle);?></table>
Here is the php code to print the file contents to the web page.
Notice that the excluded information in this sample is the IP address instead of email address, but it will not be difficult to alter to not print the email instead of the IP address.
This sample also uses a class on the table row of output so CSS can be used on that class to hi-lite alternating rows. :)

Post here if you need any more information. Glad to help.
Edited by jlhaslip (see edit history)

Share this post


Link to post
Share on other sites

}$handle = @fopen($file_name, "r");if (!$handle) {	 echo "File Handle Not Available For Use"; exit;	 };?><table summary="Posted Comments" ><tr class="hi_lite">	 <th>Date and Time</th>	 <th>Sender</th>	 <th>Email</th>	 <th>Message</th><?phpif ($show_IP){echo '<th>IP Address</th>';};echo '</tr>';$row=0 ;while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {if ($row % 2) { $data_string .= '<tr class="hi_lite"> ';}else { $data_string .= '<tr> ';}if ($show_IP){$num = count($data);	 }		 else {$num = (count($data) - 1 );			 };			 for ($c=0; $c < $num; $c++) {	 $data_string .= '<td> ' . $data[$c] . " </td>" ;		 };$data_string .= '</tr> ' . "rnt" ;$row++;};	 print $data_string ;	 $data_string = "";fclose($handle);?></table>
Here is the php code to print the file contents to the web page.Notice that the excluded information in this sample is the IP address instead of email address, but it will not be difficult to alter to not print the email instead of the IP address.
This sample also uses a class on the table row of output so CSS can be used on that class to hi-lite alternating rows. :)

Post here if you need any more information. Glad to help.

In the code you posted, what is "exit" (near the top) supposed to do, stop the entire script from continuing or just the if statement? I've never seen that used before...

Share this post


Link to post
Share on other sites

Hi again, folks.Sorry for my silence during your posts, but I have just been admitted to hospital again for six days and just came home today.I will have a good read through your posts and I will also have a look at the code of the scripts and do a bit of testing (whenever I feel up to it, mind you), and then I will post a bit of feedback.However, I trust that you experts will get the code pretty much right.Thanks for the replies, gentlemen.

Share this post


Link to post
Share on other sites

EXIT terminates the script when you can not open the data file.There is no sense in continuing if the data you need is not available.Mrdee, hope you are feeling better soon.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.