Jump to content

how to find if a number is even or odd


tomfmason

Recommended Posts

I have a query that will retrieve a product from disription from my db. Well I am wanting to use a counter and if $i is even then I will use one lay out and if it is odd I will use another.

here is a simple example of my code

[code=php:0]
$i = 0;
while ($rw = mysql_fetch_assoc($sql)) {
    if ($i == "I need something that means an even number") {
        $class = "startRight";
    }else{
        $class = "startLeft";
    }
    //now I finish displaying the results
}
[/code]

Any suggestions would be great.

Thanks,
Tom
Link to comment
Share on other sites

a "better" way? I don't think it gets better than that...

that's just a shorthand version of what you were trying to do. you could have done it like this:
[code]
$i = 0;
while ($rw = mysql_fetch_assoc($sql)) {
    if ($i % 2 == 0) {
        $class = "startRight";
    }else{
        $class = "startLeft";
    }
    //now I finish displaying the results
}
[/code]
however, i don't see anything in your code that increments $i ...

what is $i supposed to be?
Link to comment
Share on other sites

I forgot to add $i++; in my example.

$i is counting the number of times that the loop is processed. Which will be ten times. So if I go with your code then on the 4th time then wont it start on the left instead of the right? Or am I missing your point completly??

Thanks for the Help,
Tom
Link to comment
Share on other sites

okay the point of the ($i % 2 == 0) is that the % operator is the modulus operator. it takes $i and divides it by 2 and gives the remainder. if there is no remainder, then the number must be even.  So in your if..else.. statement, it does just that, assigning 'startRight' to $class if there is no remainder (therefore the condition is true because it would equal 0), but if it is not, then it assigns 'startLeft' to $class instead.  my one line piece of code is simply a shorthand way of doing your if..else statement, with the exact same condition.  it's called a ternary operator. Both of these do the same thing:

[code]
$i = 0;
while ($rw = mysql_fetch_assoc($sql)) {
    if ($i % 2 == 0) {
        $class = "startRight";
    }else{
        $class = "startLeft";
    }
    //now I finish displaying the results
    $i++;
}
[/code]
does the same as this:
[code]
$i = 0;
while ($rw = mysql_fetch_assoc($sql)) {
  $class =  ($i % 2 == 0) ? "startRight" : "startLeft";
  // now i finish displaying the results
  $i++;
}
[/code]
basically that line of code says this: if the part in the ( ) is true, then assign the value on the left side of the colon to $class. If it is false, then assign the value to the right side of the colon.
Link to comment
Share on other sites

Oh and to answer this:
[quote author=tomfmason link=topic=107313.msg430342#msg430342 date=1157691771]
So if I go with your code then on the 4th time then wont it start on the left instead of the right? Or am I missing your point completly??
[/quote]
no.  if you do $i++ it will alternate between assigning left and right to $class. each time $i is incremented, it divides that number by 2 and compares the remainder to 0.

0 / 2 = 0 no remainder:even.
1 / 2 = 0 remainder .5: odd.
2 / 2 = 1 no remainder: even.
3 / 2 = 1 remainder 1: odd.
4 / 2 = 2 no remainder: even.
5 / 2 = 2 remainder 1: odd.
6 / 2 = 3 no remainder: even.
7 / 2 = 3 remainder 1: odd.
8 / 2 = 4 no remainder: even.
9 / 2 = 4 remainder 1: odd.
10 / 2 = 5 no remainder: even.

get it?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.