Shamefully I have never moved past the n00b state. I need a tutorial to figure out how to understand the following to the point where I can actually create this without having to look anything up: <!-- Before loop --> <?php $c = 0; ?> <!-- Start loop --> <tr class="<?=($c++%2==1) ? 'odd' : 'even' ?>"> <!-- End loop --> PHP:
These might help: http://davidwalsh.name/php-shorthand-if-else-ternary-operators http://forum.codecall.net/topic/51638-the-ternary-operator-in-php/
Really not sure what type of tutorial your talking about this is very simple. Is it truly the ternary operator your having issues with, or the modulus operator? In anycase, here we go. ?: is also known as the ternary operator, works on the simple operation of <Your boolean expression ? <if true> : <if false> For example write a simple script that does <? echo true ? "This is True" : "This will never happen"; ?> Code (markup): Now, you also had in there the example of a modulus operator %.. this is thing of is the number evenly divisible by such & such? If so it will return 0, if not it will return true. Some examples 1 % 1 = 0 1 % 2 = 1 2 % 2 = 0 4 % 2 = 0 5 % 2 = 1 Code (markup): So you can use these modulus operators anywhere you want in order to get the affect you want. Also since your a beginner thinking I'll make you realize how the increment operator ACTUALLY works.. Sometimes people have the wrong idea, they think as soon as they do $i++ its going to get the new value. That is not the case. In fact, <? $i = 0; echo $i++ . "\r\n"; echo $i; ?> Code (markup): This will return respectively, 0 1 While we're on it.. Whats difference between <?= $string ?>, <?php echo $string ?>, and <? echo $string ?>.. Nothing! They're all equivalents, only different is the last one in there is using shorthand syntax, the first is using shorthand echo syntax(which is available on al php installation I think as of php 5.4 . Hope this has all helped you out a little.
Ternary operator can also be nested. Example: $value = $condition1 ? $value1 : ($condition2 ? $value2 : $value3); PHP: The example translates to: if($condition1) { $value = $value1; }else if($condition2) { $value = $value2; }else { $value = $value3; } PHP: Also Ternary operator always returns a value.
You can have it return an empty value though. My advice is to use Ternary sparingly and only at the appropriate time. It certainly reduces the length of php code, but it often does so at the expense of readability. Nesting can get really ugly.
I usually do it slightly longer but easier to understand, I reckon $toggle = 1; foreach($mydata as $row) { $class = ($toggle)?'odd':'even'; echo "<td class='{$class}'>"; $toggle = 1 - $toggle; } PHP:
Thats not going to do the trick, use the modulus operator. try this instead $count = 1; foreach($mydata as $row) { echo '<td class="'.(!($count++ % 2) ? "even" : "odd").'">'; } PHP: Notice I'm using a NOT operation here as well.. So the actual boolean value is inverses every time. Whats this doing first we're going to start with 1 So first operation is (1 % 2) = 1, which we're inverting that value with the NOT operator !, which then evaluates to !(1 % 2) = 0.. So we get odd next operation is !(2 % 2) = 1, so we get even.. And this pattern repeats itself.
Oh i see, just ran that code.. Nifty I guess I didn't look that into too much at first sight. 1 - 1 1 - 0 Code (markup): I see it does work, just never seen it done that way in my decades of coding. Guess I see and learn something new every day.
Its the way we got taught to do it when I was working on big Oracle systems. Seemed straight forward so I use it in PHP too.