Jump to content

PHP to CSV format


bazazu

Recommended Posts

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
     
    // 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

tom@sawyer.com

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 tom@sawyer.com harry@magic.co.uk

emailMe Yes Yes

comments comments this is the comment

 

 

Plz help me on this one.. unable to get the script export the data in top to bottom columnwise format

Link to comment
Share on other sites

Do you know what a \r is? It's a carriage return. It's like a \n in that it can create new lines in a file.

1. You have a bunch of them, but

2. You don't want them, so

3. You should _____.

 

Mate i actually need them.. its just that am unable to get the data to come on the first row again after each form submission.

its like

 

Question

Question

Question

Question

Question

 

and on submission and exporting to excel it should come as

 

Question Answer

Question Answer

Question Answer

Question Answer

Question Answer

 

or submission again or by another user

 

Question Answer Answer

Question Answer Answer

Question Answer Answer

Question Answer Answer

Question Answer Answer

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.