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
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>
Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.