-1

I have the following code where I try to print alphabets from a to z in the first column. but I only get the letter "a" is printed through the entire column.

function print_table_tb ($conn, $id) {
    $sql = "SELECT stata,statb,statc FROM dbA";
    $result = $conn->query($sql);
    if ($result->num_rows > 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;
            }, []);

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

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

        }

        echo "</table>";
    }
}  

What am I doing wrong here? thanks for your help! My question is different. I don't seek a solution to get alphabetical listing, I have the solution, but I couldn't fix an error.

I tried this but it doesn't work either.

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

        $range = array("a", "b", "c", "d");
        $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] . '</td>'
                . implode('', $data) . "</tr>\n" ;
            $i++;    
        }

        echo "</table>";
    }
}  
helvete
  • 2,455
  • 13
  • 33
  • 37
Jason
  • 163
  • 9
  • Possible duplicate of [PHP range() from A to ZZ?](https://stackoverflow.com/questions/14278603/php-range-from-a-to-zz) – Adam Oct 10 '18 at 18:29
  • 2
    If you use proper indenting, you'll see that you're defining $range and $i within your while loop, so you'll only ever get the first entry. I've fixed the formatting in your post so that you can see it better. – aynber Oct 10 '18 at 18:29
  • U want to have 26 rows in your html table and the last one will be with z character, right? What if there will be more then 26 rows in database? – sklwebdev Oct 10 '18 at 18:41
  • yes, that's right. Alphabetical listing starts over from "a" again. – Jason Oct 10 '18 at 18:42
  • @aynber, I tried your suggestion but I couldn't make it work. – Jason Oct 10 '18 at 18:48
  • Why should I change $i++ % 26? it works normally. – Jason Oct 10 '18 at 18:53

1 Answers1

0

Try this

$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;
        }, []);
        $temp = ($i % 26);
        $i++;
        echo '<tr><td class="tbe">'.$range[$temp].'</td>'. implode('', $data) . "</tr>\n" ;

  }
Sachin
  • 789
  • 5
  • 18