View Full Version : Dynamic entry style alternation

Jun 24th, 2006, 07:27 PM
I'm looking for how to implement a basic idea. For example, I have an admin section where I can update a link exchange. So, there are several entries. Is there a way to alternate styles, like to make the first entry have a gray background, the next have light gray, the next gray....and so on. Not so much that exact thing, but that principle. A perfect example is at http://www.generationchurch.org/audio

Jun 24th, 2006, 08:44 PM
Assuming you have your SQL all worked out...

echo '<ul>';
$i = 0;
while($row = mysql_fetch_array($results)) {
if($i%2 == 0)
{$class =' class="style" ';}
echo '<li'. $class . '><a href="' . $row['link'] . ' title="' . $row['title'] . '>' . $row['text'] . '</a></li>';

That should do it. It's been a while since I've used the modulus function though.

Jun 24th, 2006, 08:55 PM
The way I did it is I set up a toggle field, which I then change by dividing the toggle+1 by the max colors and assigning the modulus. Maybe this will help:

//set up array with names of the two CSS classes that will display a white or gray background
$colorArray = {'whiteBG', 'grayBG'};

//initialize the toggle field
$toggleBGColor = 0;

//Indicate how many different colors there can be
$maxColors = 2;

//loop through data to be displayed
for ($i = 0; $i = count(dataArray); $i++) {

//print the HTML to actually display using the correct CSS class
print "<div class=\"{$colorArray[$toggleBGColor]}\">{$dataArray['theData']}\n";

//assign the new color array index by using the modulus
$toggleBGColor = ($toggleBGColor + 1) % $maxColors;