Search code examples
phphtml-table

How to divide table of numbers into rows grouped by Tens


In this code Im trying to get prime numbers and order them in a table.

//This for printing Th ligne.
for ($l = 1; $l <= 9; $l++) {
    echo "<th id='" . $l . "' class='td-class'> " . $l . "</th>";
}

$n = $_POST['prime'];  // prime from input i send number 50
$ct = 0;
$num = "2";
$l = "1";

while ($ct < $n) {
    $dCt = 0;
    $dig1 = $num % 10;
    $length = strlen((string)$num);
    substr($num, $length - ($length - 1), 1);
    for ($i = 2; $i <= $num; $i++) {
        if (($num % $i) == 0) {
            $dCt++;
        }
    }
    echo "<tr class='td-class'>";
    if ($dCt == 1) {
        for ($m = 1; $m < 10; $m++) {

            if ($num % 10 == $m) {
                echo "<td class='td-class'>" . $num . " </td>";
            } else {
                echo "<td class='td-class'>  </td>";
            }
        }
        echo "</tr>";
    }

    $ct++;

    $num++;
}

the result im getting is like that:

     1      2   3    4  5   6   7   8   9
            2                           
                3                       
                        5               
                                7       
    11                              
               13                       
                                17      
                                       19
               23                       
                                       29
    31                              
                                37      
    41                              
               43                       
                                47  

As you can see im getting numbers which ends with same number in very new Tr line. What i want is to get every ten numbers in same line like that:

     1      2    3    4     5   6   7   8   9
            2    3          5       7       
    11          13                 17      19
                23                         29
    31                             37       
    41          43                 47   

What i tryed and was my thinking is to compare the next last number

      $digg = substr($num, -2, 1) ;  

But i didnt get numbers to be in same Tr . Any idee would be much appreciated.


Solution

  • First determining if $num is a prime number. Then, using the ones value to find the beginning and end of each tens row. Finally, if $num is prime writing it to the cell, otherwise writing an empty string.

    <?php
    
    echo '<tr class="th-class">';
    for ($l = 1; $l <= 9; $l++) {
        echo "<th id='" . $l . "' class='td-class'> " . $l . "</th>";
    }
    echo "</tr>\n";
    
    $n = 50; //$_POST['prime'];
    $ct = 0;
    $num = 0;
    
    while ($ct < $n) {
        $isprime = (1 === $num)? false: true; // temporarily true
        for ( $i = 2; $i <= $num/2; $i++ ) {
            if (0 === $num % $i) { $isprime = false; break; } // not prime
        }
    
        $ones = $num % 10;
        if ( 0 === $ones ) { echo '<tr class="td-class">'; } // start tens row
        else { echo '<td class="td-class">' . (($isprime)? $num: '') . '</td>'; }
        if ( 9 === $ones ) { echo "</tr>\n"; } // end tens row
    
        $ct++;
        $num++;
    }
    
    ?>
    

    Outputs:

    1 2 3 4 5 6 7 8 9
    2 3 5 7
    11 13 17 19
    23 29
    31 37
    41 43 47