Welcome to Dream.In.Code
Become a PHP Expert!

Join 149,976 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,393 people online right now. Registration is fast and FREE... Join Now!




Writing To CSV

 
Closed TopicStart new topic

Writing To CSV, Getting to a new row

steevo301
30 Oct, 2008 - 04:06 AM
Post #1

D.I.C Head
**

Joined: 7 Sep, 2008
Posts: 78


My Contributions
Hey, I'm just learning PHP. I've written some code that takes information from an html form and appends it to a csv file on the server. It then sends a simple email back to me, just to notify me that new information has been submitted. Here it is:

php
<?php
echo $_GET["name"], "<br />", $_GET["address"], "<br />", $_GET["city"], "<br />", $_GET["state"], "<br />", $_GET["zip"], "<br />", $_GET["email"];

$myFile = "serverinfo.csv";
$fh = fopen($myFile, 'a') or die("can't open file");

$space = ",";

fwrite($fh, $_GET["name"].$space);
fwrite($fh, $_GET["address"].$space);
fwrite($fh, $_GET["city"].$space);
fwrite($fh, $_GET["state"].$space);
fwrite($fh, $_GET["zip"].$space);
fwrite($fh, $_GET["email"].$space);

fclose($fh);

$Name = "Automated E-Mail"; //senders name
$email = "email@address.com"; //senders e-mail adress
$recipient = "example@yahoo.com";
$mail_body = "Someone has submitted new information to your form!";
$subject = "It Works!";
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields

ini_set('sendmail_from', 'me@domain.com');

mail($recipient, $subject, $mail_body, $header);

?>

My first problem is that I don't know how to start writing to a new row in the csv. It goes to a new column for each variable, but after (or before) all of them are written, I'd like to have a new row.

Just another minor problem is that the e-mails literally take about 2 hours to get to the yahoo account they're sent to. Can anyone please help with this as well?

This post has been edited by steevo301: 30 Oct, 2008 - 04:09 AM
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Writing To CSV
30 Oct, 2008 - 04:29 AM
Post #2

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 862



Thanked: 89 times
Dream Kudos: 50
My Contributions
You write a newline character at the end of the line:
php
fwrite($fh, "\n");

User is offlineProfile CardPM
+Quote Post

steevo301
RE: Writing To CSV
30 Oct, 2008 - 04:37 AM
Post #3

D.I.C Head
**

Joined: 7 Sep, 2008
Posts: 78


My Contributions
Hmm, it seems like I've tried that, but I could be mistaken.

Thanks for the help!
User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 1/8/09 06:49PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month