PHP Web Form to CSV Formatting Bug

I have created a web based print log that sends the form data to a .CSV file using PHP. Everything seems to be working correctly. Although, I have found one small bug that I cannot seem to resolve (yet).

There are seven entries on the print log that submit to the .CSV file. Two of those entries are drop down lists that are being populated from two different .DAT files which are updated on a daily basis.

The issue:

After successfully submitting the form data and opening the .CSV file, I notice that the two entities that use drop down lists are formatted different than the rest of the data.

You can see what I mean by viewing the following screen shot: http://i49.tinypic.com/2n87eko.jpg

I believe the issue is coming from the following code that pulls the "Project Number" and "Employee Name" drop down box data:

<?php
$file = 'dat_files/Employees.dat';

$handle = @fopen($file, 'r');
if ($handle) {
   while (!feof($handle)) {
       $line = fgets($handle, 4096);
       $item = explode('|', $line);
       echo '<option value="' . $item[0] . '">' . $item[0] . '</option>' . "\n";
   }
   fclose($handle);
}
?>

Any help or ideas would be greatly appreciated. Thank you.

Below is the code that is used to send the form data to the .CSV file.

    <?php

if(isset($_POST["submit"]))
{ 
    $type = $_POST["type"]; 
    $date = $_POST["date"]; 
    $job_number = $_POST["job_number"]; 
    $phase = $_POST["phase"];
    $task = $_POST["task"];
    $number_copies = $_POST["number_copies"];
    $name = $_POST["name"];


    if(empty($type)||empty($date)||empty($job_number)||empty($phase)||empty($task)||empty($number_copies)||empty($name))  
    { 
        header('Location: submit/unsuccessful.htm'); 
        die; 
    } 
    $cvsData = "\"$type\",\"$date\",\"$job_number\",\"$phase\",\"$task\",\"$number_copies\",\"$name\"".PHP_EOL; 
    $fp = fopen("log.csv", "a"); 

    if($fp) 
    { 
        fwrite($fp,$cvsData); // Write information to the file 
        fclose($fp); // Close the file 
        header('Location: submit/successful.htm');

    }
}
?>

Answers


I am guessing, but am not really sure that the code you show is the code that echos the data to the screen inside the table shown in the screenshot. if that is true then what are all the option tags about?

if not true, then we probably need to see a bit more source code to help you.


I didn't see any extra line breaks in the resulting CSV file when I ran your code. Maybe it's because your Excel formatting is off? Try highlighting the dropped down columns in Excel and then right click. Select 'Format Cells'. Then go to the 'Alignment' tab. Uncheck 'Wrap text' under the 'Text control' section.


Need Your Help

Replace all floats with doubles

java numbers typedef

Is it possible to use some construct to replace all floats with doubles (or the opposite) without refactoring?

Getting a return value from a methodInfo.invoke

invoke methodinfo

How do I get a return value (int) from a methodInfo.invoke?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.