Reg Web Posted October 29, 2006 Share Posted October 29, 2006 In pascal, there is a way to work out even numbers, is there a way to do this with in php? Quote Link to comment https://forums.phpfreaks.com/topic/25488-php-odd-or-even/ Share on other sites More sharing options...
Orio Posted October 29, 2006 Share Posted October 29, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/25488-php-odd-or-even/#findComment-116278 Share on other sites More sharing options...
ignace Posted October 29, 2006 Share Posted October 29, 2006 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]<?phpif (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! Quote Link to comment https://forums.phpfreaks.com/topic/25488-php-odd-or-even/#findComment-116301 Share on other sites More sharing options...
.josh Posted October 29, 2006 Share Posted October 29, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/25488-php-odd-or-even/#findComment-116303 Share on other sites More sharing options...
SharkBait Posted October 29, 2006 Share Posted October 29, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/25488-php-odd-or-even/#findComment-116305 Share on other sites More sharing options...
ignace Posted October 29, 2006 Share Posted October 29, 2006 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]<?phpif (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.. Quote Link to comment https://forums.phpfreaks.com/topic/25488-php-odd-or-even/#findComment-116315 Share on other sites More sharing options...
.josh Posted October 29, 2006 Share Posted October 29, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/25488-php-odd-or-even/#findComment-116344 Share on other sites More sharing options...
ignace Posted October 29, 2006 Share Posted October 29, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/25488-php-odd-or-even/#findComment-116346 Share on other sites More sharing options...
.josh Posted October 29, 2006 Share Posted October 29, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/25488-php-odd-or-even/#findComment-116364 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.