PHP Font Color

He has: 1,380 posts

Joined: Feb 2002

Yo, it's me again...lol. This time, I have a script that finds out if the primary key of a dB is even or odd, and then is supposed to select a font color based on this. It always seems to go with the ' != ' option, even when reverse the statements. What's wrong with this?

<?php
while ($qry = mysql_fetch_array($sql)) {
   
$date = $qry['date'];
   
$order = $qry['order'];
   
        if ((
$order/2) = (round($order/2))) {
   
$font = \"p class=\\"sideright\\"\";
    } elseif ((
$order/2) != (round($order/2))) {
   
$font = \"p class=\\"sideright2\\"\";
    };
?>

...later in the code, the color tag...
<td class=\"sideright\"><$font>$date:</td>
'

Thanks.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

http://www.linux.org.za/Lists-Archives/dev-0303/msg00019.html may work better for you. Testing evenness or oddness instead of rounding?

He has: 1,380 posts

Joined: Feb 2002

But if an even # is divisible by 2(which it is)...then when i divide and compare to it's divided and rounded counterpart, why doesn't it work? (just curious)

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I'm not sure, but it may be because your comparing a float to an integet (2.5~3).

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Most likely it's failing because you're testing things wrongly -- x = y sets x equal to y. x == y tests if x and y are the same. The first (true) conditional statement doesn't make sense, the second does (false), so it defaults to the one that makes sense.

However, that said, it's not an efficient test of evenness/oddness because it requires too many mathematical computations. The function suggested in the thread I linked to is a smoother, likely faster, solution.

druagord's picture

He has: 335 posts

Joined: May 2003

the easy way to find an odd number is to use modulo 2 like this

<?php
while ($qry = mysql_fetch_array($sql)) {
   
$date = $qry['date'];
   
$order = $qry['order'];
   
        if ((
$order%2) == 0) {
   
$font = \"p class=\\"sideright\\"\";
    } else {
   
$font = \"p class=\\"sideright2\\"\";
    };
?>

IF , ELSE , WHILE isn't that what life is all about

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

That's what this guy has built his function around. I haven't had the need, but now that I know how, I'm thinking this would be interesting for colouring table rows?

<?php
// function: Check if a value ($value) is even or odd.

function even($value) {
        if (
is_int($value)) {              // Per definition only integers can be odd/even
               
if (($value % 2) == 0)) {  // Divide by 2 and if modulus is 0 we are even
                       
return 1;          // Number is even
               
} else {
                        return
0;          // Number is odd
               
}
        } else {
                return
0;                  // Number is not an integer
       
}
}
?>

from: Carl Swart

druagord's picture

He has: 335 posts

Joined: May 2003

Quote:
That's what this guy has built his function around. I haven't had the need, but now that I know how, I'm thinking this would be interesting for colouring table rows?

it can be usefull for all kind of thing but your right thats how i use this most of the time. Sorry i didn't follow the link you put guess i was to eager to answer Smiling

IF , ELSE , WHILE isn't that what life is all about

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

No shame in being eager, heh. I thought to myself when you wrote that, well, hmmm, maybe I should post the whole function instead of just linking to it. Always thinking of the lurkers, that's me! Wink

He has: 1,380 posts

Joined: Feb 2002

I checked php.net for modulus, but it didn't help define it. What exactly is "modulus"?

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

"modulus" means "remainder of". If you have 4%2 or mod (4, 2) your result will be 0. That is, 4 divided by 2 is 2, with 0 remaining. Whereas if you had 3%2 or mod (3, 2) your result would be 0.5, since 3 divided by 2 is 1 plus a remainder of 0.5. So you can test for even numbers by looking for a returned value of 0 after using %2. Anything else should be odd. Smiling

Suzanne, modulus is the standard way of colouring tables, afaik. What have you been using, then? Wink

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Abhishek Reddy wrote: Suzanne, modulus is the standard way of colouring tables, afaik. What have you been using, then? Wink

I don't colour table rows, as a habit. Smiling I haven't done anything (yet) that would require it (in php).

druagord's picture

He has: 335 posts

Joined: May 2003

Quote:
2) your result would be 0.5

no 3/2 would give 1 reminder 1 so 3%2 would give 1 as a result. there are no decimals with modolus only integers. Try to remember the first division you made in school before you learned about decimals
15%10 would be 5

IF , ELSE , WHILE isn't that what life is all about

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

druagord wrote: 15%10 would be 5

http://www.google.com/search?q=15%2510

Google is so handy! Don't mind me while I walk through this for my own edification:

10 gahzinta* 15 once, remainder 5
2 gahzinta 3 once, remainder 1
8 gahzinta 16 twice, remainder 0
2 gahzinta $anyevennumber exactly, remainder 0

So if it's even, it returns 0, no matter what.

Excellent.

Now, how you do you set it up? Oh, of course, since two goes into any even number perfectly, you'll always get a remainder of zero for %2 if the number is even.

Nifty!

These are all zero:
http://www.google.com/search?q=36895825420%252
http://www.google.com/search?q=2%252
http://www.google.com/search?q=18%252

*gahzinta = "goes into" slang slang slang

druagord's picture

He has: 335 posts

Joined: May 2003

Another use of modulus i came across a few time is when displaying result in multiple pages. Let say i have 25 results per pages i do a query that comes up with 134 results i do
number of pages = ceil(134/25) = 6
number of result on the last page = 134%25 = 9

IF , ELSE , WHILE isn't that what life is all about

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Hm, true, druagord. Guess I have to revisit my math. Sad

I've used modulus in ASP to create evenly distributed results in three columns, similar to pagination. Very handy. Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.