Skip to content
Advertisement

How to display numbers vertically 1 to 20 using php

this is the sample logic i want to use.

1  6  11  16
2  7  12  17
3  8  13  18
4  9  14  19
5  10 15  20

this is my sample code. i’m just getting the logic from numbers so i can use to to what i want to display. thank you in advance to those who can help me.

<?php 
$counter = 0;
$columnctr = 0;
for ($x = 0; $x <= 4; $x++) :?>
    <tr>
    <?php for ($j = 0; $j <= 17; $j++) :?>
        
        <?php if($counter == $x) : ?>
            <?php 
                $i = 0;
            ?>
            <?php foreach ($iterator as $val) :?>
                <?php if($x == $i) : ?>
                    <td>
                        <?php if($val == "meron") : ?>
                            <span class="circle-blue">
                                
                            </span>
                        <?php $counter++;?>
                        <?php else : ?>
                            <span class="circle-red">
                                
                            </span>
                        <?php $counter++;?>
                        <?php endif; ?> 
                    </td>
                <?php endif; ?> 
                <?php $i++;?>   
            <?php endforeach;?>
            
            
        <?php else : ?>
            <td>
                
            </td>
        <?php endif; ?> 
    <?php endfor;?>
</tr>

Advertisement

Answer

$total = 20;
$totalRow = 5;

$totalCol = ceil($total/$totalRow);

echo '<pre>';

for($x = 1; $x <= $totalRow; $x++){
    for($y=1; $y <= $totalCol; $y++){
        $number = $x + (($y -1) * $totalRow);
        // don't print extra number for last column, check for $total = 21 or 22
        if($number <= $total)
            echo $number. ' ';
    }
  
    echo PHP_EOL;
}

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement