Jump to content

How do loop in vertical table?


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:



1 2 3

1 2 3

4 5 6

4 5 6

7 8 9



My code:


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

$contador = 1; 

    while ($contador <= 10) { 

        if($contador % 2){


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



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




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.


$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; ?>
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.

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.