Home
   <<
Alternating CSS classes with PHP
Post / Update: 05/05/10
Views: 1668

You may have seen on download websites or music lists, items arranged beneath each other and with the color of cells or layers alternated. This can be accomplished with relative ease in PHP: that's enough a loop and a incrementing variable which will define the class to be set. Create a new file in the folder 'www' and type the code below.

Ex. 1 - Layers:
<html><head><title>CSS class test</title>
<style type="text/css">
.classeCor1{ background-color:#FC0; }
.classeCor2{ background-color:#FF0; }
</style>
</head><body>
<?php

$i = 0;
$classe = '';
While($i < 10){
If($i % 2 == 0){ // if the division rest of $i by 2 is equal than 0
$classe = 'classeCor1';
} else { // in case of it be different than 0
$classe = 'classeCor2';
}
echo "<div class=\"$classe\">Iten $i</div>"; // then we write the class in the div
$i++; // i is incremented
}
?>
</body></html>

Ex. 2 - Tables:
<html><head><title>CSS class test</title>
<style type="text/css">
.classeCor1{ background-color:#FC0; }
.classeCor2{ background-color:#FF0; }
</style>
</head><body>
<?php

$i = 0;
$classe = '';
echo '<table>';
While($i < 10){
If($i % 2 == 0){ // if the division rest of $i by 2 is equal than 0
$classe = 'classeCor1';
} else { // in case of it be different than 0
$classe = 'classeCor2';
}
echo "<tr class=\"$classe\"><td>Id</td><td>Item $i</td></tr>"; // then we write the class in the row
$i++; // i is incremented
}
echo '</table>';
?>
</body></html>


For any doubts, you can access blogs, forums and the PHP page: http://www.php.net.
 PHP Course

Last post

Post/Update: 14/09/10

Structure, as its name implies, determines how a document or a particular composition are organized. In the case of control structures, it determines how flow the reading and processing in programming languages.

In the case of PHP, there are three...
 << See more



 Estou Hospedado na EazzyHost Design | Illustration | Programming