Jump to content

Converting a CSV File


JSHINER

Recommended Posts

I have a CSV file in the following format: (It's automatically created by a program)

 

Title;"Sub";"2008-02-01";"2008-02-01 10:23";"2008-02-01 10:45";"00:22";""

 

How can I get that to a readable format and display in a table?

Link to comment
Share on other sites

Here's where I'm at with this:

<?php

require('Database.php');

$db = new Database();
$handle = fopen('Mike.csv', 'r');

echo'
<table width="100%" border="0" cellspacing="0" cellpadding="0">
';

while ($row = fgetcsv($handle)) {


if($db->escape(trim($row[0]))=="Project") {

echo'
<tr>
    <td><b>Project</b></td>
    <td><b>Task</b></td>
    <td><b>Date</b></td>
    <td><b>Start</b></td>
    <td><b>End</b></td>
    <td><b>Duration</b></td>
</tr>';

}

else {

echo'
<tr>
    <td>', $db->escape(trim($row[0])), '</td>
    <td>', $db->escape(trim($row[1])), '</td>
    <td>', date("F j, Y", strtotime($db->escape(trim($row[2])))), '</td>
    <td>', date("g:i a", strtotime($db->escape(trim($row[3])))), '</td>
    <td>', date("g:i a", strtotime($db->escape(trim($row[4])))), '</td>
    <td>', $db->escape(trim($row[5])), '</td>
</tr>';

}

}

echo '</table>';

fclose($handle);
$db->close();

 

Now the only problem is I need to take the original CSV file and in Excel do Replace all for ( ; to , ) and ( " = (delete) )

 

I'm sure there must be a way to do this with preg_replace - but I can't get it to work. Any suggestions?

 

Link to comment
Share on other sites

Actually one last question. How can I get the contents of a directory? For example if /folder/ contained the following files:

- one.csv

- two.csv

- three.csv

 

How could I create a while statement to display:

 

one.csv - two.csv - three.csv

 

 

 

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.