Jump to content

How do loop in vertical table?


PedroSoares

Recommended Posts

Hello guys,

I'm having trouble making a loop to display the data in a table, I need the loop to display a table with the following data:

example:

 

1 2 3

1 2 3

4 5 6

4 5 6

7 8 9

 

 

My code:

<?php

echo '<table width="100%" border="1" cellspacing="0" cellpadding="0">';

$contador = 1; 

    while ($contador <= 10) { 

        if($contador % 2){

            for($x=0;$x<2;$x++){

                echo "<tr><td>Registro $contador</td>";

            }

        }else{

            echo "<td>Registro $contador</td></tr>";

        }

        ++$contador;

    }

echo '</table>';

?>

 

Help me!

Link to comment
https://forums.phpfreaks.com/topic/283032-how-do-loop-in-vertical-table/
Share on other sites

If each line is to be duplicated, just create the line one as a variable and output it twice.

<?php

$contadorHTML = ''; //Variable to hold the total output
$contadorMax = 9;   //Max value of records
$contadorStep = 3;  //Max value for each step, i.e. row

for($contador=1; $contador<=$contadorMax; $contador+=$contadorStep)
{
    //Create the output for a single step (row)
    $contadorLine = '';
    for($step=0; $step<$contadorStep; $step++)
    {
        $value = $contador + $step;
        $contadorLine .= "<td>{$value}</td>\n";
    }
    $contadorLine .= "<tr>\n";

    //Add the content for the line to the output twice
    $contadorHTML .= $contadorLine . $contadorLine;
}

?>

<table width="100%" border="1" cellspacing="0" cellpadding="0">
<?php echo $contadorHTML; ?>
</table>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.