Page 1 of 1

identifying whether a number is odd or even

Posted: Mon Sep 18, 2006 8:26 am
by hame22
Hi

I am building a table from fields within my database.

If a field is not empty a new row is built in the table:

Code: Select all

if(!empty($website))
	{
		print '<tr><td valign="top"><strong>Website:</strong></td><td><a href = "http://'.$website.'" target="_blank">'.$website.'</a></td></tr>';
	}
	
	if(!empty($profile))
	{
		print '<tr><td valign="top"><strong>Company Profile:</strong></td><td>'.$profile.'</td></tr>';
	}
	
	if(!empty($specialisations))
	{
		print '<tr><td valign="top"><strong>Specialisms:</strong></td><td>'.$specialisations.'</td></tr>';
	}
	
	if(!empty($training))
	{
		print '<tr><td valign="top"><strong>Training and consultancy offered:</strong></td><td>'.$training.'</td></tr>';
	}
this works fine but what I want to now do is add alternating row colours to my table, is there an easy way to achieve this?

thanks in advance

Posted: Mon Sep 18, 2006 8:30 am
by impulse()
JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

if (($i%2)==0) { //code//} else { //code//}
I picked that up from a tutorial a few days ago


JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Sep 18, 2006 8:51 am
by CoderGoblin
When using alternating colors in tables I generally use CSS and simply have the row with class="table_rowx" or some such where the x is 1 or 2. I do this as color can change on a regular basis and I do not like rather than hardcoding the colour in PHP.

Code: Select all

$row="<tr class=\"trow".($i%2==0 ? 1:2)."\">";

Posted: Mon Sep 18, 2006 9:19 am
by Z3RO21
CoderGoblin wrote:When using alternating colors in tables I generally use CSS and simply have the row with class="table_rowx" or some such where the x is 1 or 2. I do this as color can change on a regular basis and I do not like rather than hardcoding the colour in PHP.

Code: Select all

$row="<tr class="trow".($i%2==0 ? 1:2)."">";
Agree, I use CSS when I alternate between rows or columns. Also I use class1 and class2 as well :).

Posted: Mon Sep 18, 2006 9:23 am
by Oren
To tell if a number is odd or even:

Code: Select all

if ($num & 1)
{
	// Odd
}


else
{
	// Even
}
For alternating rows all you need is a simple XOR. For example, the below will print something like 101010...

Code: Select all

$i = 0;

while(1)
{
	if ($i ^= 1)
	{
		echo '1';
	}

	else
	{
		echo '0';
	}
}