is_odd() not working

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
konstandinos
Forum Commoner
Posts: 68
Joined: Wed Oct 04, 2006 4:20 am

is_odd() not working

Post by konstandinos »

hi

any idea why this doesn't work:

Code: Select all

for($i = 0; $i < mysql_num_fields($result); $i++)
{
        if(is_odd($i))
	{
	        echo "<td class='odd'>" . $row[$i] . "</td>";
        }
	else
	{
		echo "<td class='even'>" . $row[$i] . "</td>";
	}
}
i basically want to alternate colors of rows for easy reading. but the html ain't being outputted. no parse error though. it just stops outputting at the if statement.

thanks
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

maybe instead of the above, you could do something like this::

Code: Select all

$c = true;

for($i = 0; $i < mysql_num_fields($result); $i++) {
  echo "<td class='" . ($c === true ? 'odd' : 'even') . "'>{$row[$i]}</td>";
  $c =! $c;
}
same concept, just a bit easier to use ;)
konstandinos
Forum Commoner
Posts: 68
Joined: Wed Oct 04, 2006 4:20 am

Post by konstandinos »

that's pretty darn leet. thanks
konstandinos
Forum Commoner
Posts: 68
Joined: Wed Oct 04, 2006 4:20 am

Post by konstandinos »

a slight optimisation to boot ;-)

Code: Select all

$c = true;

for($i = 0; $i < mysql_num_fields($result); $i++) {
  echo "<td class='" . ($c ? 'odd' : 'even') . "'>{$row[$i]}</td>";
  $c =! $c;
}
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

konstandinos wrote:a slight optimisation to boot ;-)

Code: Select all

$c = true;

for($i = 0; $i < mysql_num_fields($result); $i++) {
  echo "<td class='" . ($c ? 'odd' : 'even') . "'>{$row[$i]}</td>";
  $c =! $c;
}
how is this more optimised?

Code: Select all

if ($f) {}

is not the same as

if ($f === true) {}
konstandinos
Forum Commoner
Posts: 68
Joined: Wed Oct 04, 2006 4:20 am

Post by konstandinos »

it is the same if the variable i am checking is of type boolean
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

And another one

Code: Select all

for($i = 0; $i<10; $i++) {
	echo $i, ': ', ($i&1) ? 'odd':'even', "<br />\n";
}
konstandinos
Forum Commoner
Posts: 68
Joined: Wed Oct 04, 2006 4:20 am

Post by konstandinos »

yeah i just learnt the & 1 trick for testing if odd. very cool indeed! :-)
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

Am I wrong or

Code: Select all

$c = true;

for($i = 0; $i < mysql_num_fields($result); $i++) {
  echo "<td class='" . ($c ? 'odd' : 'even') . "'>{$row[$i]}</td>";
  $c =! $c;
}
will return odd when is even and even when is odd because of the starting from 0 on the for() cycle?

to start from 0 the code should be changed like this isn't it?

Code: Select all

($c ? 'even' : 'odd') // first even then odd
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

The &1 trick is very cool!!!
I didn't understand how the bitwise AND operator works here volka converting $i in binary

Code: Select all

($i&1) ? 'odd':'even'
can you illuminate my ignorance ?

is it about the trailing "1" on binary of all odd numbers?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Jaxolotl wrote:is it about the trailing "1" on binary of all odd numbers?
Yes
0 -> 0000 (4 bit)
1 -> 0001
2 -> 0010
3 -> 0011
4 -> 0100
...
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

(0000 & 0001) -> 0000 -> false
(0001 & 0001) -> 0001 -> true
(0010 & 0001) -> 0000 -> false
(0011 & 0001) -> 0001 -> true

and so on

Get it !!! TNX volka :)
konstandinos
Forum Commoner
Posts: 68
Joined: Wed Oct 04, 2006 4:20 am

Post by konstandinos »

you might be interested in this little piece of info too. i learnt this soon after learning the & 1 trick...

when dealing with a binary number, say 1110, if you want to refer to the 0 bit, or the value on the far right etc, the common way to do so is to refer to the 'lsb' which stands for least significant bit.

i gather its the least significant, because its only 2^0, as opposed to the next few bits which are 2^1, 2^2, etc. ;-)

this guy that explained all this also ran a test on using & 1 vs using % to gauge odd/even'ness of a number, up to values of 10 000 000. the total time taken for the & method was 3.6 seconds, while the % was 4.9. so the & method is not only leeter looking, but much faster too.

i'm betting that the % operator makes use of & somewhere further up the class hierarchy, but i'd obviously need some guru to confirm this.

interesting stuff. your random fact for the day. learning that & trick left me feeling very satisfied earlier this morning. it felt good to be a geek. 8)
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

konstandinos wrote:...
i'm betting that the % operator makes use of & somewhere further up the class hierarchy, but i'd obviously need some guru to confirm this.
...
actually you don't real guru...everything sure ends up on 0 1 operations. PCs are that simple :)
Crazy Coder
Forum Newbie
Posts: 7
Joined: Thu Dec 14, 2006 8:55 am

Post by Crazy Coder »

:) thank you guys. I learned very important lesson today about &1 trick.

:lol: I feel that I'm smi-guru now
Post Reply