Split table into 4 columns with PHP

Asked

Viewed 585 times

-1

I have the code below that prints from 28 to 31 lines (according to the month) and wanted instead of printing 1 below the other make 4 columns of 8 more or less for the layout to be better.

My code

<table border="0">

    <?php
    $total_dias = date("t", mktime(0,0,0,date('m'),'01',date('Y')));

    for($dia = 1; $dia<= $total_dias; $dia++){

        if($dia <= 9){
            $dia = '0'.$dia;
        }

        echo '<tr>';
        echo '<td>'.$dia.'/'.date('m').' - '.$this->agendamento->HorariosDisponiveisPorData($profissional->id, date('Y').'-'.date('m').'-'.$dia).'</td>';
        echo '</tr>';

    }
    ?>
    </table>

How could I divide these 31 exits into 4 columns ?

1 answer

1

 <?php
    $total_dias = date("t", mktime(0,0,0,date('m'),'01',date('Y')));
    $l = 4; // quantidade de colunas
    $c = 1; // contador auxiliar
    for($dia = 1; $dia<= $total_dias; $dia++){

        if($dia <= 9){
            $dia = '0'.$dia;
        }
        if ($c == 1) {
            echo '<tr>';
        }
        echo '<td>'.$dia.'/'.date('m').' - '.$this->agendamento->HorariosDisponiveisPorData($profissional->id, date('Y').'-'.date('m').'-'.$dia).'</td>';

        // verifica se chegou ao limite de colunas ou se está no último loop.            if ($c == 4 || ($dia + 1) > $total_dias) {
            echo '</tr>';
            $c = 1;
        } else {
            $c++;
        }

    }
    ?>

Browser other questions tagged

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