Cross Vander
Cross Vander

Reputation: 2157

Display Table From PHP from large number to small number

I want to create 10x10 table from 100 to 1. I can create 1 to 100, but how to create 100 to 1? Here's my code:

<table cellspacing="0">
        <tr>
<?php
    for($i=1; $i<=100;$i++)
    {

        echo "<td class='kotak kotak".rand(1,10)."' >$i</td>";
        if($i % 10 == 0)
        {
            echo "</tr>";
        }
    }
?>
</table>

I have already tried change the for loop to this:

$i=100; $i>= 1; $i--

but the code for changing row isn't working.

Upvotes: 0

Views: 134

Answers (2)

Mi-Creativity
Mi-Creativity

Reputation: 9654

change to this lineif(($i-1) % 10 == 0), this will render the table correctly: PHP Fiddle

<table cellspacing="0">
    <tr>
    <?php
    for($i=100; $i > 0; $i--)
    {

        echo "<td class='kotak kotak".rand(1,10)."' >$i</td>";
        if(($i-1) % 10 == 0)
        {
            echo "</tr><tr>";
        }
    }
    ?>
    </tr>
</table>

Upvotes: 1

Sougata Bose
Sougata Bose

Reputation: 31749

Try with -

<table cellspacing="0">
    <tr>
    <?php
    for($i=1; $i<=100;$i++)
    {

        echo "<td class='kotak kotak".rand(1,10)."' >$i</td>";
        if($i % 10 == 0)
        {
            echo "</tr><tr>";
        }
    }
    ?>
    </tr>
</table>

And

<table cellspacing="0">
    <tr>
    <?php
    for($i=100; $i > 0; $i--)
    {

        echo "<td class='kotak kotak".rand(1,10)."' >$i</td>";
        if($i % 10 == 0)
        {
            echo "</tr><tr>";
        }
    }
    ?>
    </tr>
</table>

Upvotes: 2

Related Questions