954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP to CSV format

Hi im desperately in need of some help here..
Im exporting my html page data to csv but i am unable to get the data onto a new column the code is as follows
HTML FORM

<form id="form1" name="form1" method="post" action="index.php">
<table class="formatTblClass">
<tr>
<th colspan="6"><?=$message;?></th>
</tr>
<tr>
<td width="68"><span>First Name</span></td>
<td width="215"><input class="Name" type="text" name="fn" id="fn" /></td>
<td width="62"><span>Last Name</span></td>
<td colspan="3"><input class="Name" name="ln" type="text" id="ln" size="50" /></td>
</tr>
<tr>
<td colspan="6"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="71">Address</td>
<td width="721"><input class="Address" name="address" type="text" id="address" size="100" /></td>
</tr>
</table></td>
</tr>
<tr>
<td><span>City</span></td>
<td><input class="City" type="text" name="city" id="city" /></td>
<td><span>State</span></td>
<td width="148"><input class="State" type="text" name="state" id="state" /></td>
<td width="24"><span>ZIP</span></td>
<td width="255"><input class="ZIP" type="text" name="zip" id="zip" /></td>
</tr>
<tr>
<td><span>Phone</span></td>
<td><input class="Phone" type="text" name="phone" id="phone" /></td>
<td><span>Email</span></td>
<td><input class="Email" type="text" name="email" id="email" /></td>
<td><input name="emailMe" type="checkbox" id="emailMe" value="Yes" checked="checked" /></td>
<td>Please send me email</td>
</tr>
<tr>
<td colspan="6"><span>Comments
<textarea name="comments" id="comments" cols="45" rows="5"></textarea>
</span>
<div align="center">
<input type="submit" name="Submit" id="Submit" value="Submit" />
<input type="reset" name="Reset" id="button" value="Reset" />
</div></td>
</tr>
</table>
</form>


PHP FILE

<?php

// Receiving variables
@$pfw_ip= $_SERVER['REMOTE_ADDR'];
@$fn = addslashes($_POST['fn']);
@$ln = addslashes($_POST['ln']);
@$address = addslashes($_POST['address']);
@$city = addslashes($_POST['city']);
@$state = addslashes($_POST['state']);
@$zip = addslashes($_POST['zip']);
@$phone = addslashes($_POST['phone']);
@$email = addslashes($_POST['email']);
@$emailMe = addslashes($_POST['emailMe']);
@$comments = addslashes($_POST['comments']);

// Validation
//saving record in a text file
$pfw_file_name = "formtest.csv";
$pfw_first_raw = "fn,\rln,\raddress,\rcity,\rstate,\rzip,\rphone,\remail,\remailMe,\rcomments\r\n";
$pfw_values = "$fn,\r$ln,\r$address,\r$city,\r$state,\r$zip,\r$phone,\r$email,\r$emailMe,\r".str_replace ("\r\n","<BR>",$comments )."\r\n";
$pfw_is_first_row = false;
if(!file_exists($pfw_file_name))
{
 $pfw_is_first_row = true ;
}
if (!$pfw_handle = fopen($pfw_file_name, 'a+')) {
 die("Cannot open file ($pfw_file_name)");
 exit;
}
if ($pfw_is_first_row)
{
  if (fwrite($pfw_handle, $pfw_first_raw ) === FALSE) {
  die("Cannot write to file ($pfw_filename)");
  exit;
  }
}
if (fwrite($pfw_handle, $pfw_values) === FALSE) {
  die("Cannot write to file ($pfw_filename)");
  exit;
}
fclose($pfw_handle);

 echo("<p align='center'><font face='Arial' size='3' color='#FF0000'>thanx</font></p>");
?>


THIS IS THE OUTPUT I GET

fn
ln
address
city
state
zip
phone
email
emailMe
comments
Tom
Sawyer
shady lane
new york
new york
NY124
123456789
[email]tom@sawyer.com[/email]
Yes
comments

THIS IS THE OUTPUT I AM LOOKING FOR
fn Tom Harry
ln Sawyer Potter
address shady lane magic lane
city new york xyz
state new york xyz
zip NY124 12343
phone 123456789 987654321
email [email]tom@sawyer.com[/email] [email]harry@magic.co.uk[/email]
emailMe Yes Yes
comments comments this is the comment


Plz help me on this one

bbman
Junior Poster
182 posts since May 2010
Reputation Points: 22
Solved Threads: 10
 
<?php

// Receiving variables
@$pfw_ip = $_SERVER['REMOTE_ADDR'];
@$fn = addslashes($_POST['fn']);
@$ln = addslashes($_POST['ln']);
@$address = addslashes($_POST['address']);
@$city = addslashes($_POST['city']);
@$state = addslashes($_POST['state']);
@$zip = addslashes($_POST['zip']);
@$phone = addslashes($_POST['phone']);
@$email = addslashes($_POST['email']);
@$emailMe = addslashes($_POST['emailMe']);
@$comments = addslashes($_POST['comments']);

// Validation
//saving record in a text file
$pfw_file_name = "formtest.csv";
$pfw_first_raw = "fn,$fn\rln,$ln\raddress,$address\rcity,$city\rstate,$state\rzip,$zip\rphone,$phone\remail,$email\remailMe,$emailMe\rcomments, $comments\r\n";
//$pfw_values = "$fn,\r$ln,\r$address,\r$city,\r$state,\r$zip,\r$phone,\r$email,\r$emailMe,\r".str_replace ("\r\n","<BR>",$comments )."\r\n";
$pfw_is_first_row = false;
if (!file_exists($pfw_file_name)) {
    $pfw_is_first_row = true;
}
if (!$pfw_handle = fopen($pfw_file_name, 'a+')) {
    die("Cannot open file ($pfw_file_name)");
    exit;
}
if ($pfw_is_first_row) {
    if (fwrite($pfw_handle, $pfw_first_raw) === false) {
        die("Cannot write to file ($pfw_filename)");
        exit;
    }
}
if (fwrite($pfw_handle, $pfw_values) === false) {
    die("Cannot write to file ($pfw_filename)");
    exit;
}
fclose($pfw_handle);

echo ("<p align='center'><font face='Arial' size='3' color='#FF0000'>thanx</font></p>");
?>
lili.edryana
Light Poster
47 posts since Nov 2009
Reputation Points: 10
Solved Threads: 2
 

The way you have the code set up, you are going to to keep adding to the bottom of the file. If you want to add the values in horizontally, you will need to pull out each line of the file (use the php file() function into an array, add in the new data and recreate the file.

This way the csv will have the data populated horizontally rather than appending it at the bottom.

sudeepjd
Junior Poster
181 posts since Oct 2010
Reputation Points: 32
Solved Threads: 43
 

@lili.edryana:

Your code will put it in correctly, but only the first time. The next time the program is run, it will append again to the bottom of the file.

sudeepjd
Junior Poster
181 posts since Oct 2010
Reputation Points: 32
Solved Threads: 43
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: