Can’t think of any tutorials off-hand, but I can give you a quick run-down of how to do that. The key PHP function you need is called ‘fgetcsv’ – because like most C syntax language functions, there’s this paranoid hatred of descriptive names as if two or three characters is going to kill us. “fileGetCSV” would be much more clear, and is what it does. (I swear some programmers WANT programming to be hard, see “google rust”)
Let’s say you had a simple 3 column table of a home budget, saved as CSV:
budget.csv
"Action","Amount","Total"
"Income Feb 2011",2066,2066
"Rent",-1200,866
"Electric",-140,726
"Cable/Internet",-122.60,603.40
"Groceries",-400,203.40
"","",""
"Income March 2011",2066,2269.40
"Rent",-1200,1069.40
"Electric",-140,929.40
"Cable/Internet",-122.60,806.80
"Groceries",-400,$606.80
the first row is the labels for the columns, each line there-after containing our comma separated cells.
First thing you’d need to do in your PHP is open the file…
if ($handle=fopen($fileName,'r')) {
The ‘fopen’ command opens the file (again with the cryptic abbreviations), and returns a handle (id code) to access the file with. the ‘r’ means to open the file as read-only. Then you just need to process all the rows:
$row=fgetcsv($handle,0x400)
Which plugs a ‘row’ of cells into our $row variable. The 0x400 (hexadecimal) is the same as 1024 – basically it says to not allow lines longer than 1k. (which saves memory and runs faster)
This is typically done inside a ‘while’ command so it continues until end of file.
while ($row=fgetcsv($handle,0x400)) {
… which you can then echo out:
echo '
<tr>
<th scope="row">',$row[0],'</th>
<td>',$row[1],'</td>
<td>',$row[2],'</td>
</tr>';
That’s the basics of it. A more robust handler would auto-figure out how many rows there are, put the first row in a THEAD as all TH, auto-detect the first row on the TBODY values, build a colgroup for CSS off formatting… use a define for the max CSV line length and a variable for the filename… pretty much this would be a ‘better’ version of the above:
<?php
define(MAX_CSV_ROW,0x400);
$fileName='budget.csv';
if (
($handle=fopen($fileName,'r')) &&
($row=fgetcsv($handle,MAX_CSV_ROW))
) {
echo '
<table>
<caption>',$fileName,'</caption>
<colgroup>';
$columns=count($row);
for ($counter=0; $counter<$columns; $counter++) {
echo '
<col align="',(
$counter==0 ? 'left' : 'right'
),'" />';
}
echo '
</colgroup>
<thead>
<tr>';
foreach ($row as $data) {
echo '
<th scope="col">',$data,'</th>';
}
echo '
</tr>
</thead>
<tbody>
';
while ($row=fgetcsv($handle,MAX_CSV_ROW)) {
echo '<tr>';
$count=0;
foreach ($row as $data) {
$tag=($count++==0 ? 'th' : 'td');
echo '
<',$tag,(
$tag=='th' ? ' scope="row"' : ''
),'>',(
is_numeric($data) ? number_format($data,2) : $data
),'</',$tag,'>';
}
echo '
</tr>';
}
echo '
</tbody>
</table>';
fclose($handle);
} else {
echo '
<p>
There was an error opening "budget.csv"
</p>';
}
?>
Which using the above budget.csv would output a nice semantic proper table with all the proper relationships/scope declared:
<table>
<caption>budget.csv</caption>
<colgroup>
<col align="left" />
<col align="right" />
<col align="right" />
</colgroup>
<thead>
<tr>
<th scope="col">Action</th>
<th scope="col">Amount</th>
<th scope="col">Total</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Income Feb 2011</th>
<td>2,066.00</td>
<td>2,066.00</td>
</tr><tr>
<th scope="row">Rent</th>
<td>-1,200.00</td>
<td>866.00</td>
</tr><tr>
<th scope="row">Electric</th>
<td>-140.00</td>
<td>726.00</td>
</tr><tr>
<th scope="row">Cable/Internet</th>
<td>-122.60</td>
<td>603.40</td>
</tr><tr>
<th scope="row">Groceries</th>
<td>-400.00</td>
<td>203.40</td>
</tr><tr>
<th scope="row"></th>
<td></td>
<td></td>
</tr><tr>
<th scope="row">Income March 2011</th>
<td>2,066.00</td>
<td>2,269.40</td>
</tr><tr>
<th scope="row">Rent</th>
<td>-1,200.00</td>
<td>1,069.40</td>
</tr><tr>
<th scope="row">Electric</th>
<td>-140.00</td>
<td>929.40</td>
</tr><tr>
<th scope="row">Cable/Internet</th>
<td>-122.60</td>
<td>806.80</td>
</tr><tr>
<th scope="row">Groceries</th>
<td>-400.00</td>
<td>$606.80</td>
</tr>
</tbody>
</table>
Will need some CSS to be really pretty – still annoys me that Firefox/gecko based browsers STILL don’t support align on the COL tags – See Bugzilla 915… ALSO annoying not one browser out there supports align=“char” – a value that when working with tabular data (like a table) should be considered essential and has been in the specification since 1998. LOVE all this idiocy of adding new stuff to browsers when they don’t even have the old stuff working right yet.
Given that the HTML output from ANY of the office software is totally USELESS bloated rubbish I’d NEVER put on a website, and then Dreamweaver is a fat bloated steaming pile that, well… as a dearly departed friend often said “the only thing you can learn from Dreamweaver is how NOT to build a website” – Uhm… NO!!!
Methinks the two are mutually exclusive – though once you have the code to handle it written and if you can keep your spreadsheet formats consistent with no extraneous data, it should be a WOUM. (Write once, Use Mostly).