Printing letters with php

I’m trying to print letters a-z in the first column with the code below. but for some reason I can’t make it work. (it only prints the first letter “a” down the column). What is it I am doing wrong here? please. Thanks!

function print_table_tb ($conn, $id) {
    $sql = "SELECT stata,statb,statc FROM dbA";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {

            $range = range('a', 'z');
            $i = 0;

        echo "<table class='tbr' id='tb$id'>";
        while($row = $result->fetch_assoc()) {
            $data = array_reduce($row, function($carry, $value) {
                $carry[] = "<td dbval='{$value}'>{$value}</td>";
                return $carry;
            }, []);

            echo '<tr><td class="tbe">'.$range[$i++ % 26].'</td>'. implode('', $data) . "</tr>\n" ;

        }

        echo "</table>";
    }
}

Break it down to what works and build it back up testing along the way.

<?php
function print_table_tb()
{
    $range = range('a', 'z');

    echo "<table class='tbr' id='tb'>";
    foreach ($range as $letter) {
        echo "<tr><td class='tbe'>$letter</td></tr>\n";
    }
    echo "</table>";
}

print_table_tb();
?>

Your code works, there is no problem. The formula I use works normally, but here I can’t get the expected results.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.