views:

328

answers:

2

I am using php and i am iterating a table with a result array ... I want to add row color and alternate row color to it.... How to do so? Any suggestion...

<table  id="chkbox" cellpadding="0" cellspacing="2" 
               width="100%" class="table_Style_Border">
<tr>
<td style="width:150px" class="grid_header" align="center">RackName</td>    
   <td style="width:150px" class="grid_header" align="center">LibraryName</td>  
<td style="width:200px" class="grid_header" align="center">LibrarianName</td>
<td style="width:200px" class="grid_header" align="center">Location</td>
        <td style="width:1%" class="grid_header"></td>


    </tr>

     <? if(isset($comment))
           { echo '<tr>
      <td class=table_label colspan=5>'.$comment.'</td></tr>'; } ?>
    <?php foreach($rackData as $row) { ?>
    <tr>
        <td align="left" class="table_label">
                <?=$row['rack_name']?>
        </td>
        <td align="left" class="table_label">
                <?=$row['library_name']?>
        </td>
        <td align="center" class="table_label">
                <?=$row['librarian']?>
        </td>
        <td align="center" class="table_label">
                <?=$row['location']?>
        </td>
        <td align="center">
            <input type="checkbox" name="group" id="group" 
  value="<?=$row['rack_id']?>" onclick="display(this);"  > 
        </td>

    </tr>

  <?    } ?>
    <table>

EDIT:

<?php foreach($rackData as $key =>  $row) { ?>
        <?php printf('<tr class="%s">', ($key % 2) ? : 'rowcolor' : 'alternaterowcolor');?>

It doesn't seem to take your syntax....

ERROR:

Parse error: syntax error, unexpected ':' in D:\xampp\htdocs\codeigniter_cup_myth_new\system\application\views\rackdetails.php  on line 238
+3  A: 

Use modulo

<?php foreach($rackData as $key => $row) { ?>
    <?php printf('<tr class="%s">', ($key % 2) ? 'odd' : 'even'); ?>
    // ...

Then you can define CSS classes with the names .odd and .even and given them the background-color you want the rows to alternate with.

With modern browsers (read: not IE) you can also do it directly in CSS with the :nth-child pseudo class:

tr:nth-child(even) { background-color: #FFF; }
tr:nth-child(odd) { background-color: #EEE; }
Gordon
@gordon even and odd are css class?
chandru_cp
@chandru_cp No, you define them in your CSS and give them the colors you want
Gordon
@gordon ya i was refering that only.. I ll let you after trying it..
chandru_cp
@gordon look at my edit
chandru_cp
@chandru_cp *it doesnt work* is not a valid error description. describe what exactly is not working. You also didn't write valid PHP code. There was typo in my code though too, which I fixed. Copy and paste now.
Gordon
@gordon i have edited that syntax with yours that too didnt work...
chandru_cp
@chandru_cp you still have the typo of mine in it. *that too didn't work* is not a valid error message either. Please, for making debugging easier on this and future questions, be specific about what doesn't work and always provide any error message you get.
Gordon
@gordon tat worked
chandru_cp
@gordon how to get onmouseover and onmouseout events to the table..
chandru_cp
@chandru_cp you do that from Javascript. Please ask a new question for this.
Gordon
+1  A: 

To streamline your server code you could use javascript to highlight your rows and add mouse over/out handlers to the rows to do whatever you want.

Very easy to do with jquery and many examples.

zaf