Jump to content

Archived

This topic is now archived and is closed to further replies.

Reg Web

PHP 'odd' or 'even'

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


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

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

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!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

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

@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..

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
well..here is another way of alternating row colors, non-dependant of your sql rows..
[code]
<?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>";
?>
[/code]

Share this post


Link to post
Share on other sites

×

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.