Jump to content


Photo

PHP 'odd' or 'even'


  • Please log in to reply
8 replies to this topic

#1 Reg Web

Reg Web
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 29 October 2006 - 03:05 PM

In pascal, there is a way to work out even numbers, is there a way to do this with in php?

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 29 October 2006 - 03:08 PM

if (num%2 == 0) echo "Even";
else echo "Odd";

The script above checks if the modulo when deviding by two is zero -> The number is even.

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,431 posts
  • LocationBelgium

Posted 29 October 2006 - 04:17 PM

the above mentioned method is good, but not when you are going to work with databases, therefor i suggest you use the following code:

<?php
if (isset($tmp)) { unset($tmp); $type='odd'; }
else { $tmp = 1; $type='even'; }
?>

this is a very messy code, but it really gets the job done! if anyone knows a better way of doing this, then please share it with us!

#4 .josh

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

Posted 29 October 2006 - 04:23 PM

That piece of code looks like it's specifically designed for whatever larger script its from. I fail to see how it works as a generic 'odd or even' checker. Can you please explain your code in more detail, ignace?
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 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 29 October 2006 - 04:24 PM

I am curious how it works too.

I always use the modulus to check if its odd or even.  I also use it with database entries with no issues.

#6 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,431 posts
  • LocationBelgium

Posted 29 October 2006 - 04:42 PM

whenever you delete a record from your database, you will get two even's or odd's after eachother (example you delete record 3 meaning that record 2 and 4 will return 0 which makes them both even (what is actually correct, but will mess up our row coloring/..)).

now my code however (the sloppy, messy one :D)

<?php
if (isset($tmp))
{
   unset($tmp);
   $type='odd';
}
else
{
   $tmp = 1;
   $type='even';
}
?>

@Crayon Violent: i did not steal it from any other script, i created it myself, not that i am proud of it or anything because it is very unproffesionally written, but it always get the job done!

so when you use this piece of code in your loop, the first time $tmp won't exist making the first row even the next time $tmp does exist and is being unset(); so the current row is odd, the next time $tmp does not exist making it again even, etc, etc..

#7 .josh

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

Posted 29 October 2006 - 05:43 PM

yeah i can kinda sorta see how that would be useful..if you were working with a database and under certain circumstances..anyways, I wasn't saying you stole it. I was saying it looked like a piece of code that was part of a larger piece of code, doing something specific, as opposed to being a general purpose piece of code that could be used, well, generically.

also, am i mistaken, or it vaguely seems like your using your code to do something like alternate row colors or something?
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 :)

#8 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,431 posts
  • LocationBelgium

Posted 29 October 2006 - 05:47 PM

well i use this code for all kinda things but in general for alternate row coloring, because the most row coloring solutions are mostly also temporarily (so until you remove a record) because they forgot that you can delete records... messing up my website, and it colors... this however i found a more definitive solution.

don't mistake me for a proffesional, because just like the most users of this forum i am a noob seaking answers to my questions, and sharing the information i already received.

#9 .josh

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

Posted 29 October 2006 - 06:19 PM

well..here is another way of alternating row colors, non-dependant of your sql rows..
<?php
   // pretend this array is your results array from a query
   $blah = array('a','b','c','d','e','f','g','h','i','j');

   // example colors
   $color1 = '#777777';
   $color2 = '#FFFFFF';

   // example loop. would normally use a while with fetch_array or whatever
   echo "<table border='1'>";
   for($x = 0; $x<10; $x++) {
      // is color == to color1? set to color2. otherwise, set to color1
      $color = ($color == $color1) ? $color2 : $color1;
      echo "<tr><td bgcolor = '$color'>{$blah[$x]}</td></tr>";
   }
   echo "</table>";
?>

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