identifying whether a number is odd or even

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

identifying whether a number is odd or even

Post 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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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]
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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)."\">";
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post 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 :).
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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';
	}
}
Post Reply