Welcome Guest, Not a member yet? Register   Sign In
Formatting Tables with CSS
#1

[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.
#2

[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;}
#3

[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;
#4

[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>
#5

[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');
#6

[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
#7

[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 #
#8

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

[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;
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB