Jump to content

PHP 'odd' or 'even'


Reg Web

Recommended Posts

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!
Link to comment
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..
Link to comment
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?
Link to comment
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.
Link to comment
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]
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.