|
Posted by Ψrjan Langbakk on 06/24/06 13:42
Den 24.06.2006 14:54, skriblet Rik fΓΈlgende:
> Why use ""? '' is much simpler here:
> $this->html .= '
> <table id="dusjkabinetter">
> <thead>
> <tr>
> <th id="th2" valign="top" align="left">Prod. nr:</th>
> <th id="th3" valign="top" align="left">Bilder:<br />(klikk
> for stΓΈrre visning):</th>
> <th id="th4" valign="top" align="left">Beskrivelse:</th>
> <th id="th5" valign="top" align="left">Pris:</th>
> </tr>
> </thead>";
>
>> while(($row = fgetcsv($csvHandle, filesize($csvFile))) !== false){
>> $this->html .= "<tr>";
>> foreach($row as $field){
>> $this->html .= "<td>$field</td>";
>> }
>> $this->html .= "</tr>\n";
>> }
>
> Seeing as you know the number of array-elements here, replace with:
> $rowhtml ='
> <tr>
> <td>%s</td>
> <td>%s</td>
> <td>%s</td>
> <td align="right">%s</td>
> </tr>';
> while(($row = fgetcsv($csvHandle, filesize($csvFile))) !== false){
> $this->html = vsprintf($rowhtml, $row);
> }
I tried doing as you said above, but I ended up with no <thead>, and
only one of the <tr>s shows up - the last one (only two in the CSV-file
at the moment, but still).
And, also, the whole thing is centered.
Maybe I didn't do it right?
Here's the code after replacment:
<?php
class csv{
/* This class builds a html table from the contents of a CSV file. */
var $html = '';
function csv($csvFile){
if(!$csvHandle = fopen($csvFile, 'r')){
$this->html .= "Couldn't open $csvFile.";
}
else{
$this->html .= '
<table id="dusjkabinetter">
<thead>
<tr>
<th id="th2" valign="top" align="left">Prod. nr:</th>
<th id="th3" valign="top" align="left">Bilder:<br />(klikk
for stΓΈrre visning):</th>
<th id="th4" valign="top" align="left">Beskrivelse:</th>
<th id="th5" valign="top" align="left">Pris:</th>
</tr>
</thead>'; <-- I changed this from " to ' - else it wouldn't
parse at all.
$rowhtml ='
<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td align="right">%s</td>
</tr>';
while(($row = fgetcsv($csvHandle, filesize($csvFile))) !== false){
$this->html = vsprintf($rowhtml, $row);
}
fclose($csvHandle);
$this->html .= "</table>\n\n\n";
}
}
function getHtml(){
return $this->html;
}
}
echo <<<TABELL_START
TABELL_START;
$html .= "<h2>Dusjkabinetter</h2>\n";
$mycsv = new csv('csv-filer/dusjkabinetter.csv', ' ');
$html .= $mycsv->getHtml();
echo $html;
echo <<<TABELL_SLUTT
TABELL_SLUTT;
?>
--
mvh
Γrjan Langbakk
[Back to original message]
|