Jump to content
Larry Ullman's Book Forums

Chapter 9, Page 282 - The Ternary Operator. Alternate Background Colors. Script 9.4


Recommended Posts

As noted on page 283:

 

(condition) ? value T : value F. Will return a value after being evaluated based on the condition.

 

 

 

line 61:

$bg = '#eeeeee'; // Set the initial background color.

 

 

 

line 63 - 65:

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

 

$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color

 

echo '<tr bgcolor="' . $bg . '">

 

....

 

}

 

Firstly:

I don't understand how the $bg variable is defined twice, yet $bg will still hold the value #eeeeee.

 

Consider the following:

 

$var = 'Hello';

 

$var = 'World';

 

Wouldn't the new value now be 'World' not 'Hello' ?

 

Secondly,

It says "For the first row, $bg is equal to #eeeeee. For the second row, $bg is not equal to #eeeeee."

 

How can the second row not be equal to #eeeeee, when $bg's initial value is #eeeeee. Based on the ternary operator, $bg does equal #eeeeee, therefore the condition is true, thus the second row would also be #ffffff.

 

The result is that every row would be #ffffff, because $bg = #eeeeee.

 

 

 

Thanks,

Mark

Link to comment
Share on other sites

Hi Mark,

It isn't assigned twice, it's assigned (=) once and within that assignment it's coMpared (==)

 

Its really shorthand for

 

If($bg == '#eeee'){

 

$bg == '#ffff';

}Else{

 

$bg == '#eeee';

}

 

(My phone won't let me change captilisation)

  • Upvote 1
Link to comment
Share on other sites

You don't need to use it anymore for alternat background color and you can use css instead

 

 

you can use this selector tr:nth-child(odd)

 

 

here is an example where I use it

 

#container #content-box .content-elements .tblListData tbody tr:nth-child(odd){

background: #fff;

border-top: 10px solid #FFF;

}

  • Upvote 1
Link to comment
Share on other sites

It works because of this:

 

Before anything:

$bg = '#eeeeee';

 

1. Iteration:

($bg=='#eeeeee') is now TRUE and '#ffffff' is assigned to $bg.

 

2. Iteration:

($bg == '#eeeeee') is now FALSE because #ffffff was assigned last time. #eeeeee is therefor assigned to $bg

 

3. Iteration:

($bg=='#eeeeee') is now TRUE and '#ffffff' is assigned to $bg.

 

 

This is done until the all rows are fetched from MYSQL and the result is alternating colors. :)

 

The important part to understand:

Right hand side operations gets executed first. The result is then transfered to the left hand side. This is why something like this works:

 

$left = $right = false; // Both are false

  • Upvote 2
Link to comment
Share on other sites

Thanks for all your responses.

 

To both Jonathan and Antonio, thanks for presenting the script in different ways. It helped me understand what was really going on behind the scenes. I could not understand it at first, I was also unsure about the way I comprehended it.

 

But to reiterate (directed at Antionio's explanation):

 

 

In it's most simplest form:

 

The variable takes on a new value each time it is passed through the ternary operator, correct?

 

Either way, I believe this is how I think it works. It makes sense.

 

@bahaa - Your suggestion also a worth considering in future projects. This method will be most widely used once CSS3 is most widely supported by all the browsers.

Link to comment
Share on other sites

Thanks for all your responses.

 

To both Jonathan and Antonio, thanks for presenting the script in different ways. It helped me understand what was really going on behind the scenes. I could not understand it at first, I was also unsure about the way I comprehended it.

 

But to reiterate (directed at Antionio's explanation):

 

 

In it's most simplest form:

 

The variable takes on a new value each time it is passed through the ternary operator, correct?

 

Either way, I believe this is how I think it works. It makes sense.

 

@bahaa - Your suggestion also a worth considering in future projects. This method will be most widely used once CSS3 is most widely supported by all the browsers.

It is already supported by major browser and also on IE 9

  • Upvote 1
Link to comment
Share on other sites

The variable takes on a new value each time it is passed through the ternary operator, correct?

 

@bahaa - Your suggestion also a worth considering in future projects. This method will be most widely used once CSS3 is most widely supported by all the browsers.

 

Yes, that's what's happening. As Jonathon says, the code below will do the same. Ternary is a shorthand if/else.

 

$color = '#eeeeee'; // Set the initial background color.

while ( $row = mysqli_fetch_array($r, MYSQLI_ASSOC) ) {
  if ( $bg == '#eeeeee' ) {
  $bg = '#ffffff';
 } else {
  $bg = '#eeeeee';
  }
}

 

For just the background colors, bahaa has a point. This can very well be done in CSS. That's what I use to do.

  • Upvote 1
Link to comment
Share on other sites

Very interesting. I think I was very curious about the ternary operator because on page 285 in making sortable displays, the operator is used again but instead it is passing values through another condition.

 

Page 285:

$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'rd';

 

All your comments made me understand this, thanks guys.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...