Closed Thread Icon

Topic awaiting preservation: quick php question (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=13027" title="Pages that link to Topic awaiting preservation: quick php question (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: quick php question <span class="small">(Page 1 of 1)</span>\

 
mas
Paranoid (IV) Inmate

From: the space between us
Insane since: Sep 2002

posted posted 01-06-2004 13:59

hello
i got a database and i am reading some links out of it in divs
however, i want that every second div gets another background-color than the first one (by using the echo tag and defining css classes)
so it goes like this
class1
class2
class1
class2
class1
......
help would be very appreciated

Veneficuz
Paranoid (IV) Inmate

From: A graveyard of dreams
Insane since: Mar 2001

posted posted 01-06-2004 14:29
code:
$tmp = 0;
$class = array('class1', 'class2');

while (reading database content)
{
print "<div.... class=\"".$class[$tmp]."\"...></div">;

$tmp = ($tmp++) % 2;

}



That should work. What it does is increase $tmp each loop, but when $tmp equals 2 it is set to 0. That way the $tmp variable will alternate between 0 and 1. If you want to add more classes just add the class name to the $class variable and change the 2 to the number of classes you're using.

_________________________
"There are 10 kinds of people; those who know binary, those who don't and those who start counting at zero"
- the Golden Ratio -

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 01-06-2004 14:46

That is a great answer. I think I will have to use this somewhere. I just wouldn't have thought of the mod #classes.

-Dan-

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 01-06-2004 15:32

If you use an array to store the class IDs, you'd rather go for :

$tmp = ($tmp+1) % count( $class );

If you use only 2 different class IDs, you can do:

$tmp = ++$tmp & 1;

or my favorite

$tmp ^= 1;

mas
Paranoid (IV) Inmate

From: the space between us
Insane since: Sep 2002

posted posted 01-06-2004 19:31

thx for the solution guys
veneficuz: BIG THX...the only thing is that $tmp = ($tmp++) % 2; didn't work for me but $tmp = ($tmp+1) % count( $linkreaddiv ); did
thx again

« BackwardsOnwards »

Show Forum Drop Down Menu