Jump to content


Photo

how to find if a number is even or odd


  • Please log in to reply
7 replies to this topic

#1 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 08 September 2006 - 04:43 AM

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

$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
}

Any suggestions would be great.

Thanks,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 08 September 2006 - 04:52 AM

$class =  ($i % 2 == 0) ? "startRight" : "startLeft";


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 08 September 2006 - 04:55 AM

Will that work?? I am going to display 10 results per page. I guess I could create an even array then use in_array. But I thought that there might be a better way.

Thanks,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 08 September 2006 - 04:58 AM

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:
$i = 0;
while ($rw = mysql_fetch_assoc($sql)) {
    if ($i % 2 == 0) {
        $class = "startRight";
    }else{
        $class = "startLeft";
    }
     //now I finish displaying the results
}
however, i don't see anything in your code that increments $i ...

what is $i supposed to be?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 08 September 2006 - 05:02 AM

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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 08 September 2006 - 05:10 AM

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:

$i = 0;
while ($rw = mysql_fetch_assoc($sql)) {
    if ($i % 2 == 0) {
        $class = "startRight";
    }else{
        $class = "startLeft";
    }
     //now I finish displaying the results
    $i++;
}
does the same as this:
$i = 0;
while ($rw = mysql_fetch_assoc($sql)) {
   $class =  ($i % 2 == 0) ? "startRight" : "startLeft";
   // now i finish displaying the results
   $i++;
}
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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 08 September 2006 - 05:12 AM

Thank you very much. That was very informative. I also googled it and found several other methods.

Thanks again,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 08 September 2006 - 05:37 AM

Oh and to answer this:

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??

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?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users