FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Get alternate table color with php





pashmina
Is it possible to get alternate table color with php coding? I know about the javascript method, but I am looking for a method in full php coding.
kv
Code:
<table border="1" style="background-color: <color code here>;">
  <tr>
    <th>Column</th>
    <th>Revenue</th>
  </tr>
  <tr>
    <td>1</td>
    <td>1,200</td>
  </tr>
</table>
urtrivedi
What do mean by alternate color?
Do you want to display adjacent col/row with different color?
Fire Boar
PHP can generate stuff like that fairly easily...

Code:
<?php
$currentcol = 1;
$colours = array("#FFFFFF", "#CCCCCC");
function row($html)
{
  global $currentcol, $colours;
  $currentcol ^= 1;
  return "<tr style=\"background-color: ".$colours[$currentcol]."\">\n".$html."</tr>\n";
}

echo "<table>\n";
echo row("<td>First row</td>\n");
echo row("<td>Second row</td>\n");
echo row("<td>Third row</td>\n");
echo row("<td>Fourth row</td>\n");
echo row("<td>Fifth row</td>\n");
echo "</table>\n";
?>


You get it. Just use the row() function to add a row. Presto! Alternating rows with very little effort. Change $colours to get different coloured rows.
sonam
I am not sure am I understand good your question but I will solve this with different css and simple php in head of html. In that case you can change and include more colors and differences with few lines in php. For example:
1. Create three different css - pink.css, blue.css and white.css
2. Insert this php in head of your page:

Code:
<?php
$n = $_GET['color'];
switch($n) {
        case "1": echo '<link rel="stylesheet" href="pink.css" type="text/css">'; break;
        case "2": echo '<link rel="stylesheet" href="blue.css" type="text/css">'; break;
        default: echo '<link rel="stylesheet" href="white.css" type="text/css">';
    }
?>


3. If you didn't chose any number in url the brower will load white.css
4. If you chose e.g. www.your_page.php?color=1 browser will load pink.css

Of course you can have one main css for all documents and php can include and switch only addition css.

Sonam Cool
pashmina
Firstl thank you for the reply.

Can somebody explain this php coding to me

Code:
<?php echo ($ac_sw1++%2==0)?"even":"odd"; ?>
urtrivedi
Code:
<?php echo ($ac_sw1++%2==0)?"even":"odd"; ?>


Here this code determines whether your variable $ac_sw1 contains odd number or even number.
depending on that it returns "even" or "odd". I guess they are names of the style in your css.
pashmina
i found one more method to do so..
Code:
<?php

/*** an array of animals ***/
$animals = array('dingo',
        'wombat',
        'platypus',
        'kangaroo',
        'steve irwin',
        'wallaby',
        'kookaburra',
        'kiwi');
?>

<html>
<head>
<style type="text/css">

table tbody tr.light {
    background-color:pink;
}

table tbody tr.dark {
    background-color: grey;
}

table tbody tr:hover {
    background-color: white;
}
</style>
</head>

<body>
<table>
<thead>
<tr><td>Animal Name</td></tr>
</thead>
<tfoot>
<tr><td>PHPRO.ORG<td></tr>
</tfoot>
<tbody>
<?php
    /*** set a counter ***/
    $i=0;
    /*** the CSS class names ***/
    $colors = array('light', 'dark');
    foreach( $animals as $critter )
    {
        echo '<tr class="'.$colors[$i++ % 2].'"><td>'.$critter.'</td></tr>';
    }
?>
</tbody>
</table>
Aredon
Yup that should work just fine too. There isn't just one method you know Cool.
Related topics
How long does it usally take?
alternate row color
MySQL Database
A very good PHP MySQL Tutorial
PHP VS ASP
Segredos da microsoft
Javascript Color Pallet
how do i make a window/iframe in a table within php page..??
Trying to connect to table through PHP page
cannot add data to mysql from PHP form
Force table size?
Call a value through Foreign id from another table(DW+PHP)
array diplay in a table using php
Watch Dogs
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.