I need an explanation about what a code I don’t understand does

Asked

Viewed 228 times

6

I need to make a change to a code and I found this: <?=(((++$i % 2) == 1) ? 'class="colored"' : '')?>

What that code does?

  • 1

    That’s a ternary operator, makes similar to what the if...else ago.

  • That’s not in a loop loop(for or while), No? Has an increment operator along.

  • It’s not in a loop No!

  • This out of a loop has no use, because the ++$i (preincremented) should be iterated for its value to change. - Example

  • The code is exactly that: <tr <?=(((++$i % 2) == 0) ? 'class="colored"' : '')?>>

  • I happen to have another tr under this, and I need this zebra table code to include the tr from below for the color change.

  • Create another question, so that we can answer, here already has too many answers related to a specific problem.

  • 2

    But for a quick response, you can use this in the style sheet - table tr:nth-child(even) {color:violet;}, or within the tags style.

Show 3 more comments

3 answers

13

It increments the variable $i divide by 2 and take the rest. If the rest is 1 - if it is odd - it uses the string 'class="colored"', otherwise uses a string empty. In this case he is assigning the class when the counter is odd, that is, he is probably making a zebra table, but it is not possible to say without more details. If you’re not in a loop it seems to be very wrong.

This is the conditional operator. Along with the preincrement operator.

  • It assigns the class to each new item the list receives. I need it to assign every 2 items the list receives. How can I do?

  • That’s exactly it, sorry I don’t give more details but that’s right!!

  • Need to use in 2 items, then 2 is without, then comes back in 2, etc.? Or need a yes and a no?

  • I need the following: This table has items etc. Some items have the "Note" field below the item, when there is note I want the note field to be the same color as the above item, when there is no note I need to continue alternating colors with each new item.

  • @Tiagop. C Just with the code you entered, impossible to help, post the rest of your zebra table code and its logic in your question.

  • 2

    @Tiagop. C Only with these details can not answer. As already has several answers, this question is already compromised. Now all you have to do is ask another question showing your real code, give details of how it is working and what you want. Read this: http://answall.com/help/mcve

  • Okay @bigown, thank you so much for your answers, you’ve been of great help, I’ll ask you another question with part of the code!

Show 2 more comments

12


It is a ternary that checks whether $i is an odd number using the two module, if positive returns a piece of html that is the definition of a class(css) otherwise an empty string is returned.

The symbol (<?=) at the beginning serves to print the result of the expression it is equivalent to <?php echo 'algo';

((++$i % 2) == 1) ? 'class="colored"' : '';
 | 1 | 
 |  2           | 3 caso seja impar   | 4 caso seja par

1 - Preincrement of $i i.e., it will have its value increased before re-running the module calculation.

2 - Two case module calculation returns 1 means that the number is odd, if zero is even.

3 - True condition.

4 - Condition false.

It’s likely that this code fragment was extracted from something like:

<?php
    $i = 0;
    while($row = mysqli_fetch_assoc($result)){
?>
    <tr <?=(((++$i % 2) == 1) ? 'class="colored"' : '');?>>
        <td><?=$row['nome'];?></td>
        <td><?=$row['descricao'];?></td>
    </tr>

    <?php } ?>
  • 1

    I find it interesting to comment on the short tag <?= = echo

  • @Guilhermelautert, thank you I passed right on that, already editing adding this information

3

It is on a table that in a dot line of one color and in the next line of another color and alternate.

  • Exactly! I need him to paint every two lines, how to do?

  • 4

    @Tiagop. C You asked what the code does. And that’s already been answered. I suggest you open a new question and explain what you need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.