Changing CSS class through PHP

Its a very simple way of changing css class using php, all you have to do is to run a for loop and each loop it will change the class.

Changing CSS class through PHP

First we define css classes as gray blue green and black, and we’ll use it in table using php.

<html>
<title>Changing CSS class through PHP - Demo</title>
<head>
<style>

.gray{background-color:red; color:white;font-size:20px; font-weight:bold; }
.blue{background-color:blue; color:white;font-size:20px; font-weight:bold; }
.green{background-color:green; color:white;font-size:20px; font-weight:bold; }
.black{background-color:black; color:white;font-size:20px; font-weight:bold; }
</style>
</head>
<body>

<table width=”500″ border=”6″ cellspacing=”6″ cellpadding=”6″ >

<?php
for($row=1; $row<=10; $row++)
{ echo “<tr>”;
for($column=1; $column<=4; $column++)
{
switch($column){
case 1: $class=”gray”; break;
case 2: $class=”blue”; break;
case 3: $class=”green”; break;
default: $class=”black”;
}
echo “<td class=\”$class\” align=\”center\”>[$row,$column]</td>”;
}

echo “</tr>”;
}
?>
</table>
</body>

Watch demo


One Response to “Changing CSS class through PHP

  • 1
    Eugene
    October 21st, 2008 13:43

    I am looking for some idea and stumble upon your posting :) decide to wish you Thanks. Eugene

Leave a Reply