JSHINER Posted March 13, 2008 Share Posted March 13, 2008 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? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 13, 2008 Share Posted March 13, 2008 read in the file and explode() each line using a semi-colon. that will give you an array that you can display in an HTML table. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 13, 2008 Share Posted March 13, 2008 Use the function fgetcsv() specifying the semi-colon as the separator. Ken Quote Link to comment Share on other sites More sharing options...
JSHINER Posted March 14, 2008 Author Share Posted March 14, 2008 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? Quote Link to comment Share on other sites More sharing options...
accident Posted March 14, 2008 Share Posted March 14, 2008 the only problem with that is if there is a valid ; (that isnt a delimiter) in the document it will also get converted to a , use the fgetcsv() like ken stated Quote Link to comment Share on other sites More sharing options...
JSHINER Posted March 14, 2008 Author Share Posted March 14, 2008 There are no other ; in the files. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 14, 2008 Share Posted March 14, 2008 You can specify using the ";" as the delimiter in the fgetcsv() function. By default the double quote is used to delimit strings, so you shouldn't have to remove it. Ken Quote Link to comment Share on other sites More sharing options...
JSHINER Posted March 14, 2008 Author Share Posted March 14, 2008 Worked perfect. Thanks! (If there were a Solved button I'd click it) Quote Link to comment Share on other sites More sharing options...
JSHINER Posted March 14, 2008 Author Share Posted March 14, 2008 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 Quote Link to comment Share on other sites More sharing options...
accident Posted March 14, 2008 Share Posted March 14, 2008 if ($handle = opendir('/folder/')) while (false !== ($file = readdir($handle))) echo "$file - "; Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 14, 2008 Share Posted March 14, 2008 Use the glob() function: <?php echo implode(' - ',array_map('basename',glob('/folder/*.csv'))) . "<br>\n"; ?> Ken Quote Link to comment Share on other sites More sharing options...
JSHINER Posted March 14, 2008 Author Share Posted March 14, 2008 Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.