English 中文(简体)
Styling rows in table that uses CSV data through PHP
原标题:
  • 时间:2009-11-18 04:21:44
  •  标签:
  • php
  • css
  • csv

I ve been working on this simple code that puts a CSV file into a nice table. But because the data is imported, styling ODD rows is a pretty hard thing to do.

All I need would be a method to address certain rows, so I can make a "zebra" like background, and put specific data in another text style.

Does aynone have an idea? thanks a lot!

<?php
print("<TABLE>
");
$row = 0;
$handle = fopen("test_file.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
   $num = count($data);
   for ($c=0; $c <= $row; $c++)
{
    print("<TR>");
    print("<TD>".$data[0]." </td>");
    print("<TD>".$data[1]." </td>");
    print("<TD>".$data[2]." </td>");
    print("<TD>".$data[3]." </td>");
    print("<TD>".$data[4]." </td>");
    print("</TR>"); 
}
}
fclose($handle);

?>
最佳回答

There is a jQuery plugin called TableSorter that allows for the zebra-style coloring and also adds the ability to to click-to-sort rows. It is very easy to integrate.

This is not a pure PHP solution, but in the majority of cases you re going to end up having to code CSS and JavaScript anyways, so this ends up saving a lot of time and prevents you from hard-coding that stuff into your PHP logic.

First you link the scripts in the <head> of your document:

<script type="text/javascript" src="/path/to/jquery-latest.js"></script> 
<script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script> 

Then you make sure your table has <thead> and <tbody> elements:

<table id="myTable"> 
<thead> 
<tr> 
    <th>Last Name</th> 
    <th>First Name</th> 
    <th>Email</th> 
    <th>Due</th> 
    <th>Web Site</th> 
</tr> 
</thead> 
<tbody> 
<tr>
    <td>...
...
</tbody>
</table>

End then you enable it with jQuery:

$(document).ready(function() 
    { 
        $("#myTable").tablesorter({ widgets: [ zebra ] }); 
    } 
);
问题回答

what about

print("<TR class= " . ($c%2 == 0? even : odd )." >");

after you can add the proper CSS

.even {
  background-color: #aaaaaa; 
}

.odd {
  background-color: #fffff; 
}

Use something like:

<table>
<tbody>
<?php
$row = 0;
$handle = fopen( test_file.csv ,  r );
while ($data = fgetcsv($handle, 1000,  , )):
  $class = ++$row & 1 == 1 ?   class="odd"  :   ;
  $num = count($data);
?>
<tr<?php echo $class; ?>>
<?php for ($c=0; $c <= $num; $c++) {
  <td><?php echo $data[$c]; ?></td>
<?php endfor; ?>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<?php fclose($handle); ?>

with:

tr.odd td { background: #CCC; }

Or with short tags (which I personally prefer):

<table>
<tbody>
<?
$row = 0;
$handle = fopen( test_file.csv ,  r );
while ($data = fgetcsv($handle, 1000,  , )):
  $class = ++$row & 1 == 1 ?   class="odd"  :   ;
  $num = count($data);
?>
<tr<?= $class ?>>
<? for ($c=0; $c <= $num; $c++) {
  <td><?= $data[$c]; ?></td>
<? endfor; ?>
</tr>
<? endwhile; ?>
</tbody>
</table>
<? fclose($handle); ?>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签