CodeIgniter Forums
Formatting Tables with CSS - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Formatting Tables with CSS (/thread-29631.html)

Pages: 1 2


Formatting Tables with CSS - El Forum - 04-15-2010

[eluser]maniac[/eluser]
I am generating tables with php from the database and want to know whether it is possible to change the color of each alternative row. Everything I have tried doesn't work.

Thanks for any help.


Formatting Tables with CSS - El Forum - 04-15-2010

[eluser]pickupman[/eluser]
Check out the HTML Table Class docs from the user guide. You can see that you can set an alternate row start configuration.
Code:
//table template
$tmpl = array (
                    'row_alt_start'       => '<tr class="alt">',
                    'row_alt_end'         => '</tr>',
                    'cell_alt_start'      => '<td>',
                    'cell_alt_end'        => '</td>',

              );
$this->table->set_template($tmpl);

//CSS
tr.alt td {background: #ccc;}



Formatting Tables with CSS - El Forum - 04-15-2010

[eluser]John_Betong[/eluser]
[quote author="maniac" date="1271399025"]I am generating tables with php from the database and want to know whether it is possible to change the color of each alternative row. Everything I have tried doesn't work.

Thanks for any help.[/quote]
&nbsp;
Does "Everything" include using alternator()?
&nbsp;
&nbsp;
&nbsp;


Formatting Tables with CSS - El Forum - 04-15-2010

[eluser]maniac[/eluser]
I must admit I'm totally new at all this. This is how I was taught to do the tables -

<table width="57%" border="1" cellspacing="0" cellpadding="0" align="center">
<tr>
<th>Employee ID</th>
<th>Employee First Name</th>
<th>Employee Last Name</th>
<th>Department</th>
</tr>
&lt;?php foreach($result as $row): ?&gt;
<tr>
<td>&lt;?=$row->empid?&gt;</td>
<td >&lt;?=$row->empfirstname?&gt;</td>
<td >&lt;?=$row->emplastname?&gt;</td>
<td >&lt;?=$row->deptid?&gt;</td>
</tr>
&lt;?php endforeach; ?&gt;
</table>


Formatting Tables with CSS - El Forum - 04-15-2010

[eluser]pickupman[/eluser]
There is nothing wrong with that. No need to use the table library if don't want to. Just change it up a bit with
Code:
&lt;?php $this->load->helper('string');?&gt;

<table width=“57%” border=“1” cellspacing=“0” cellpadding=“0” align=“center”>
      <tr>
      <th>Employee ID</th>
      <th>Employee First Name</th>
      <th>Employee Last Name</th>
      <th>Department</th>
      </tr>
      &lt;?php
      foreach($result as $row): ?&gt;
      <tr class="&lt;?php echo alternator('alt,'');?&gt;">
      <td>&lt;?=$row->empid?&gt;</td>
      <td >&lt;?=$row->empfirstname?&gt;</td>
      <td >&lt;?=$row->emplastname?&gt;</td>
      <td >&lt;?=$row->deptid?&gt;</td>
      </tr>
      &lt;?php endforeach; ?&gt;
    </table>

Or if you are using jQuery you can cheat, and keep your existing markup.
Code:
//In script block
$("table tr:odd").addClass('alt');



Formatting Tables with CSS - El Forum - 04-15-2010

[eluser]maniac[/eluser]
Thanks for the help, but it didn't work.

I added the helper to the controller, would that make a difference? Should it be in the view file?

Thanks


Formatting Tables with CSS - El Forum - 04-15-2010

[eluser]John_Betong[/eluser]
&nbsp;
Try this:
Code:
// old
    <tr class="&lt;?php echo alternator('alt,'');?&gt;">

  // New
    <tr style="&lt;?php echo alternator('background:#cfc','background:#ffc'); ?&gt;">
&nbsp;
&nbsp;
&nbsp;
edit: added missing #


Formatting Tables with CSS - El Forum - 04-15-2010

[eluser]maniac[/eluser]
Worked perfectly. Thank you.


Formatting Tables with CSS - El Forum - 04-16-2010

[eluser]John_Betong[/eluser]
[quote author="maniac" date="1271414616"]Worked perfectly. Thank you.[/quote]
&nbsp;
Looks like my guess was right and there was a missing CSS -> .alt statement.
&nbsp;
&nbsp;
&nbsp;


Formatting Tables with CSS - El Forum - 04-16-2010

[eluser]maniac[/eluser]
Sorry, what do you mean?