Welcome to WebmasterWorld Guest from 50.17.5.36

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

alternate backgrounds colours in list of links

novice needs help!

     
4:35 pm on Sep 20, 2005 (gmt 0)

Full Member

10+ Year Member

joined:May 9, 2003
posts:210
votes: 0


I've been trying to modify a script that lets users upload files to my site, and to view/download a list of the uploaded files.

I want to modify it so that every second list item uses a stylesheet class that makes its background a darker colour.

Here is the code that produces the list of filenames/links:

$list = "<ul>";
$dir = opendir($absolute_path);
while($file = readdir($dir)) {
if (($file!= "..") and ($file!= ".")) {
$list .= "<li><a href=\"$dl$file\">$file</a></li>";
}
}
$list .= "</ul>";
echo $list;

I want to change this so that every second list item appears as follows:

<li class="darkbg">

Any help would be greatly appreciated!

5:58 pm on Sept 20, 2005 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


you can use modulus and a counter

$cnt = 0; 
$list = "<ul>";
$dir = opendir($absolute_path);
while($file = readdir($dir)) {
if (($file!= "..") and ($file!= ".")) {
if ($cnt % 2) $list .= "<li><a href=\"$dl$file\">$file</a></li>";
else $list .= "<li class=\"darkbg\"><a href=\"$dl$file\">$file</a></li>";
$cnt++;
}
}
$list .= "</ul>";
echo $list;
6:35 pm on Sept 20, 2005 (gmt 0)

Full Member

10+ Year Member

joined:May 9, 2003
posts:210
votes: 0


Perfect! Thanks for the beginner's lesson, JK. Exactly what I wanted.