tags:

views:

239

answers:

8

Hi, 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>\n");
$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);

?>
+1  A: 

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; 
}
RageZ
+1  A: 

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); ?>
cletus
You've got `for ($c=0; $c <= $num; $c++) {` and `endfor;`
Justin Johnson
A: 

thanks for the quick help!

I can't seem te make it work. Problem is that I'm a beginner at php... I've tested the it here; /indextestsort.php

I must be doing something wrong :/

Mike K.
All of your rows have `class="even"` on them.
Justin Johnson
A: 

RageZ, print("");

seems to color all rows even.

Cletus, thanks for your answer! But when i try it, I'm getting

Parse error: syntax error, unexpected '<' in csvtotableB.php on line 12

Any ideas?

Mike K.
A: 

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'] }); 
    } 
);
jathanism
A: 

thanks synack, but I'm importing my table data from a CSV file, meaning I can't give the table and tags, and neither a table ID.

Is there a way to solve this?

Mike K.
A: 

The table sorter looks amazing! I might go with that. I tried the following, but there seems to be something not working: test file here; /csvtotable/test2.php

Anyone knows what makes this not work?

<head>
 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
 <title>Brian's super duper sortable table</title>

 <link rel="stylesheet" href="style.css" type="text/css" media="print, projection, screen" />


 <script type="text/javascript" src="min.js"></script>
 <script type="text/javascript" src="jquery.js"></script>

 <script type="text/javascript" charset="utf-8">
  $(document).ready(function() 
      { 
          $("#tablesorter-demo").tablesorter();
      } 
  );
 </script>

</head>

<body>



    <table id="tablesorter-demo" class="tablesorter" border="0" cellpadding="0" cellspacing="1">

    <?php
    $row = 1;
    $handle = fopen("test_portfolio.csv", "r");
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
    {
        $num = count($data);
        /*echo "<p> $num fields in line $row: <br /></p>\n";*/
        $row++;

     if ($row == 2)
     {
      echo "<thead>\n<tr>\n";
         for ($c=0; $c < $num; $c++)
      {
             echo "<th class=\"header\">" . $data[$c] . "</th>\n";
         }
      echo "</tr>\n</thead>\n<tbody>";
     }

     else
     {
      echo "<tr class=\"even\">\n";
         for ($c=0; $c < $num; $c++)
      {
             echo "<td>" . $data[$c] . "</td>\n";
         }
      echo "</tr>\n";
     }
    }
    fclose($handle);
    ?>

    </tbody>
    </table>


</body>
</html>
Mike K.
A: 

Its working!!! Just apply the script I posted 1 post ago.

Thanks to everybody for helping me out... really appreciated :)

Mike K.