The table represents the characteristics of the product. There is a header group of characteristics and the characteristics themselves. It is necessary to output an array of settings in the form of a table in such a way. And so that the alternation of color always begins with the same color. Option with even and odd know how to do:

foreach($specs_item_full AS $key => $value){ $i++; if($i $i%2 == 0){ echo "<tr style='background-color: #eeeeee'> <td class='spec_title'> $key </td> <td class='spec_details'> $value </td> </tr> "; } else { echo "<tr style='background-color: #e2e2e2'> <td class='spec_title'> $key </td> <td class='spec_details'> $value </td> </tr> "; } } 

But how to display the title until I understand. Moreover, in the variant that is indicated above, the alternation does not begin with one color - if there are an even number of elements in the array, then it starts with one color, and if it is odd, it starts with another. If I ended up with different colors, then it is clear, but here it is not clear. The key of the array is the name of the characteristic in Cyrillic, the value is the very value of the characteristic. The title comes with an empty value.

    1 answer 1

    You can remember the value of the current section in a separate variable, for example, $ key_curr. At each iteration, compare this value with the previous value; if they match, output the characteristics alternating.

     <?php $key_curr = null; $i = 0; foreach($specs_item_full AS $key => $value){ if($key == $key_curr) { $i++; if($i % 2) $color = "#eeeeee"; else $color = "#e2e2e2"; echo "<tr style='background-color: {$color}'> <td class='spec_title'> $key </td> <td class='spec_details'> $value </td> </tr> "; } else { $key_curr = $key; echo "<tr style='background-color: #ffffff'> <td class='spec_title' colspan='2'> $key </td> </tr> "; } } 

    If you need to continue the alternation of the previous section in the next section - just use the previous counter value $ i, if you want to start the alternation of colors from some initial color, reset the value of $ i in the else-block (the first option is given above).

    • cheops thanks. Only I did not quite understand. It turns out that just 3 colors will alternate, that is, three lines are output, then three lines again. Or I do not understand? I need the loop to check for the key value, for example, if it receives false, it displays the header color, and the remaining lines alternate in two colors. Characteristics can be any number. In the photo I have, for example, after the title of 4 lines. - Torawhite
    • Perhaps my option of displaying the characteristics is extremely inconvenient, but I couldn’t think of anything more convenient ... - Torawhite
    • Corrected the answer, did not immediately understand what is required. - cheops
    • cheops thanks a lot, only $ value to null is checked. Made without $key_curr = null; wrote if($value != null){...}else{...} It seems to work - Torawhite
    • I also decided to reset $i after each header ...else{$i=0 ...} so that the alternation starts again and so beautifully) - Torawhite