By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,871 Members | 2,418 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,871 IT Pros & Developers. It's quick & easy.

Parsing CSV-file and adding CSS to style

P: n/a
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";
while(($row = fgetcsv($csvHandle, filesize($csvFile))) !== false){
$this->html .= "<tr>";
foreach($row as $field){
$this->html .= "<td>$field</td>";
}
$this->html .= "</tr>\n";
}
fclose($csvHandle);
$this->html .= "</table>\n\n\n";
}
}

function getHtml(){
return $this->html;
}
}

echo <<<TABELL_START
<div class='t_tabell'>
TABELL_START;

$html .= "<h2>Dusjkabinetter</h2>\n";
$mycsv = new csv('csv-filer/dusjkabinetter.csv', ' ');
$html .= $mycsv->getHtml();

echo $html;

echo <<<TABELL_SLUTT
</div>
TABELL_SLUTT;
?>

--
mvh
Ørjan Langbakk
Jun 24 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Message-ID: <44********@news.broadpark.no> from Ørjan Langbakk contained
the following:
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.


Untested.
....
$this->html .= "<tr>";
$count=0;
foreach($row as $field){
if($count<3){
$this->html .= "<td>$field</td>";
}
else{
$this->html .= "<td
style='text-align:center'>$field</td>";
}
$count ++;
}
$this->html .= "</tr>\n";

....
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jun 24 '06 #2

P: n/a
Rik
Ø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
Jun 24 '06 #3

P: n/a
Rik
Rik wrote:
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);
}


Or, if you don't know the number of array-elements, for future use:

$rowbegin ='
<tr>';
$rowbetweeen = '
<td>%s</td>';
$rowend = '
<td align="right">%s</td>
</tr>';
while(($row = fgetcsv($csvHandle, filesize($csvFile))) !== false){
$this->html =
vsprintf($rowbegin.str_repeat($rowbetween,count($r ow)-1).$rowend, $row);
}

Grtz,
--
Rik Wasmus
Jun 24 '06 #4

P: n/a
Den 24.06.2006 14:40, skriblet Geoff Berrow følgende:
Message-ID: <44********@news.broadpark.no> from Ørjan Langbakk contained
the following:
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.


Untested.
...
$this->html .= "<tr>";
$count=0;
foreach($row as $field){
if($count<3){
$this->html .= "<td>$field</td>";
}
else{
$this->html .= "<td
style='text-align:center'>$field</td>";
}
$count ++;
}
$this->html .= "</tr>\n";

...


Works like a charm :)

Now, is there a way I can use _this_ code to also add styles to other
<td>s? Or is this a "one-stop" option? I tried adding more counts, but
couldn't seem to get it right - probably because I missed a basic point
somewhere :)

--
mvh
Ørjan Langbakk
Jun 24 '06 #5

P: n/a
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
Jun 24 '06 #6

P: n/a
Rik
Ørjan Langbakk wrote:
Works like a charm :)

Now, is there a way I can use _this_ code to also add styles to other
<td>s? Or is this a "one-stop" option? I tried adding more counts, but
couldn't seem to get it right - probably because I missed a basic
point somewhere :)


Seeing as you know the number of array-elements here, replace with:
$rowhtml ='
<tr>
<td style="#somestyle1">%s</td>
<td style="#somestyle1">%s</td>
<td style="#somestyle1">%s</td>
<td style="#somestyle1">%s</td>
</tr>';
while(($row = fgetcsv($csvHandle, filesize($csvFile))) !== false){
$this->html .= vsprintf($rowhtml, $row);
}

Grtz,
--
Rik Wasmus
Jun 24 '06 #7

P: n/a
Rik
Ørjan Langbakk wrote:
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: while(($row = fgetcsv($csvHandle, filesize($csvFile))) !== false){
$this->html = vsprintf($rowhtml, $row);
}


Should offcourse be:
$this->html .= vsprintf($rowhtml, $row);
------------^

My apologies.

Grtz,
--
Rik Wasmus
Jun 24 '06 #8

P: n/a
Den 24.06.2006 15:47, skriblet Rik følgende:
Ørjan Langbakk wrote:
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:

while(($row = fgetcsv($csvHandle, filesize($csvFile))) !== false){
$this->html = vsprintf($rowhtml, $row);
}


Should offcourse be:
$this->html .= vsprintf($rowhtml, $row);
------------^

My apologies.

Grtz,


Thanks! Now it works perfectly :D

I feel I should have seen that, though... I need to pay a little more
attention to what I'm actually reading :)

--
mvh
Ørjan Langbakk
Jun 24 '06 #9

P: n/a
Ø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?

<snip>

<OT>
According to your browser support, you may use CSS selectors
<http://www.w3.org/TR/css3-selectors/> for that:

td:last-child{}
</OT>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jun 25 '06 #10

P: n/a
Rik
R. Rajesh Jeba Anbiah wrote:
Ø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?

<snip>

<OT>
According to your browser support, you may use CSS selectors
<http://www.w3.org/TR/css3-selectors/> for that:

td:last-child{}
</OT>


Yup, if only more browsers supported here. The last time I checked it was
only Netscape.

Grtz,
--
Rik Wasmus
Jun 25 '06 #11

P: n/a
Den 25.06.2006 19:47, skriblet R. Rajesh Jeba Anbiah følgende:
Ø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?

<snip>

<OT>
According to your browser support, you may use CSS selectors
<http://www.w3.org/TR/css3-selectors/> for that:

td:last-child{}
</OT>


Doesn't work in all browsers, and that makes it completely useless. But,
see the answer from Rik, which solved my problem :)

--
mvh
Ørjan Langbakk
http://www.bergenpchjelp.no
http://www.cubic-design.net
Jun 25 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.