Reply to Re: Parsing CSV-file and adding CSS to style

Your name:

Reply:


Posted by Rik on 06/24/06 12:54

Ψrjan Langbakk wrote:
> I'm parsing a CSV-file into a table on a webpage - and I'd like to be
> able to change the alignment for the _last_ <td> in each <tr> - but,
> as the file is today, it's not possible for me to assign a CSS-class
> on only the last <td>. I was wondering if anyone could give me a clue
> as to what I need to change in the following code, to make this
> possible (I'm assuming I need to have each <td> present in the code,
> so as to be able to assign a class to the last one, but how? I can
> tell as much as that I know the number of <td>'s in each CSV-file -
> they are a constant).
>
> Any help would be appreciated.
>
> Here is the code:
>
> <?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>\n
> <th id=\"th2\" valign=\"top\" align=\"left\">Prod. nr:</th>\n
> <th id=\"th3\" valign=\"top\" align=\"left\">Bilder:<br
> />(klikk for stψrre visning):</th>\n
> <th id=\"th4\" valign=\"top\"
> align=\"left\">Beskrivelse:</th>\n <th id=\"th5\"
> valign=\"top\" align=\"left\">Pris:</th>\n </tr>\n
> </thead>\n";

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);
}

Grtz,
--
Rik Wasmus

[Back to original message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация