解析CSV文件并将CSS添加到样式 [英] Parsing CSV-file and adding CSS to style
问题描述
我正在将一个CSV文件解析到网页上的一个表格中 - 我希望能够改变_last_< td>的对齐方式。在每个< tr>中 - 但是,如今文件是
,我不可能只在最后一个< td>上分配
上的CSS类。我想知道是否有人能给我一个线索,以便我需要在下面的代码中更改,以使这成为可能(我是
假设我需要让每个< td>出现在代码中,以便能够将一个类分配给最后一个,但是怎么样?我可以告诉我那个我
知道每个CSV文件中< td>的数量 - 它们是常数。)
任何帮助都将不胜感激。
这是代码:
<?php
class csv {
/ *此课程从CSV文件的内容构建一个html表。 * /
var $ html ='''';
函数csv($ csvFile){
if(!$ csvHandle = fopen($ csvFile,''r'')){
$ this-> html。="无法打开$ csvFile。" ;;
}
else {
$ this-> html。="< table id = \" dusjkabinetter \"> ;
< thead>< tr> \ n
< th id = \" th2 \" VALIGN = \" top\"对齐= \" 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" ;; < br $>
}
fclose($ csvHandle);
$ this-> html。="< / table> \ nn \\ n $ \\ nn ;;
}
}
函数getHtml(){
返回$ 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
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
推荐答案
html ='''';
函数csv(
html = '''';
function csv(
csvFile){
if(!
csvFile){
if(!
csvHandle = fopen(
csvHandle = fopen(
这篇关于解析CSV文件并将CSS添加到样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!