-1

i have this code

<table class="table table-bordered text-center font-weight-bold">
    <tr>
        <?php
        for ($i = 1; $i <= 60; $i++) { ?>
            <td class="<?= $i == 7 || $i == 8 || $i == 11 ? 'bg-danger' : 'bg-success'; ?> m-0 p-0">
                <a href="#" style="text-decoration: none; font-size: 5vw">
                    <div class="h-100 w-100 my-2 text-white">
                        <?= $i; ?>
                    </div>
                </a>
            </td>
            <?php if (!empty($i) && ($i) % 6 == 0) {
            ?>
    </tr>
    <tr>
    <?php } ?>
<?php }
?>
    </tr>
</table>

showing this result i don't want to

i want result like this

1 2 3 4 5 6

12 11 10 9 8 7

13 14 15 16 17 18

24 23 22 21 20 19

25 26 27 28 29 30

36 35 34 33 32 31

37 38 39 40 41 42

etc...

i have no idea how the most simple logic works for this

  • 1
    If you use [flexbox](https://css-tricks.com/snippets/css/a-guide-to-flexbox/) instead of a table, you can output the data in the order you're currently outputting it and just change the `flex-direction` on every other row using CSS. – M. Eriksson Sep 17 '20 at 11:35

1 Answers1

0

I rewrote the code treating the values before the loop, following the steps below:

  1. Create an array with 60 sequential positions with range();
  2. Split the array into chunks, making a matrix with the values;
  3. For each row in array, reverse the odd indices.

The $selected array is used to apply the 'bg-danger' class to the td's.

<table class="table table-bordered text-center font-weight-bold">
<?php
    $numbers = range(1, 60);
    $handle_values = array_chunk($numbers, 6);
    foreach($handle_values as $k => &$hv) {
        if($k & 1) $hv = array_reverse($hv);
    }

    $selected = [7, 8, 11];

    foreach ($handle_values as $values) {
         ?>
        <tr>
            <?php foreach($values as $i) { ?>
                <td class="<?= in_array($i, $selected) ? 'bg-danger' : 'bg-success'; ?> m-0 p-0">
                    <a href="#" style="text-decoration: none; font-size: 5vw">
                        <div class="h-100 w-100 my-2 text-white">
                            <?= $i; ?>
                        </div>
                    </a>
                </td>
            <?php } ?>
        </tr>
    <?php } ?>
</table>