Page 1 of 1
is_odd() not working
Posted: Fri Dec 29, 2006 12:48 am
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
Posted: Fri Dec 29, 2006 1:11 am
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

Posted: Fri Dec 29, 2006 1:24 am
by konstandinos
that's pretty darn leet. thanks
Posted: Fri Dec 29, 2006 1:39 am
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;
}
Posted: Fri Dec 29, 2006 2:11 am
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) {}
Posted: Fri Dec 29, 2006 2:44 am
by konstandinos
it is the same if the variable i am checking is of type boolean
Posted: Fri Dec 29, 2006 2:51 am
by volka
And another one
Code: Select all
for($i = 0; $i<10; $i++) {
echo $i, ': ', ($i&1) ? 'odd':'even', "<br />\n";
}
Posted: Fri Dec 29, 2006 2:57 am
by konstandinos
yeah i just learnt the & 1 trick for testing if odd. very cool indeed!

Posted: Fri Dec 29, 2006 3:32 am
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
Posted: Fri Dec 29, 2006 3:52 am
by Jaxolotl
The &1 trick is very cool!!!
I didn't understand how the bitwise AND operator works here
volka converting $i in binary
can you illuminate my ignorance ?
is it about the trailing "1" on binary of all odd numbers?
Posted: Fri Dec 29, 2006 3:57 am
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
...
Posted: Fri Dec 29, 2006 4:02 am
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 
Posted: Fri Dec 29, 2006 6:55 am
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.

Posted: Fri Dec 29, 2006 7:09 am
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

Posted: Fri Dec 29, 2006 12:14 pm
by Crazy Coder

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

I feel that I'm smi-guru now