Jump to content

Stuck with printing alphabetical listing with PHP


mattix

Recommended Posts

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!

Here is the code I'm using:

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>";
    }
}

The method to lists alphabets here normally works, but i couldn't make it work in this code.

I also tried this:

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>";
    }
}  

In this code, I also get the same result, can't make it work. any ideas please? thanks.

Link to comment
Share on other sites

You know - I read up on this 'array_reduce' function and I still don't get it.  Neither do I see why you see a need to use it.

How about you just explain what you are trying to do instead of showing us this code?  I don't understand for one.  I don't see how this reduce is needed to process some query results as well as produce some "alphabetical list".  What's the connection?

Link to comment
Share on other sites

I didn't write this part of code,  and  I don't have enough knowledge why it's preferred to use there. It assigns unique id's to each table, and unique dbval values to each td element.  I'm trying to assign letters to first column instead of numbers which I thought would be a simple thing. but I can't figure out what's wrong here.  Basically I want to list letters instead of numbers at the beginning of each row in the first cell.

Link to comment
Share on other sites

3 minutes ago, mattix said:

the code works fine, I just can't make it work for the letters

In what way does that statement make sense?

 

 

FYI, your original function worked when I tried it.

However, it could be simplified without the unnecessary array_reduce().

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

        $label = 'a';
        $i = 0;

        echo "<table class='tbr' id='tb$id'>";
        foreach ($result as $data) {
            echo '<tr><td class="tbe">' . $label++ . '</td><td>' . implode('</td><td>', $data) . "</td></tr>\n" ;
        }

        echo "</table>";
    }
}

With this version, when it runs out of a-z after 26 rows, it will move on to aa, ab, ac etc.

Link to comment
Share on other sites

1 hour ago, Barand said:

In what way does that statement make sense?

I meant the the code before adding alphabetical listing which I wanted to add later works. 

Thanks,  Barand, I will try your code. 

1 hour ago, Barand said:

FYI, your original function worked when I tried it.

However, it could be simplified without the unnecessary array_reduce().


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

        $label = 'a';
        $i = 0;

        echo "<table class='tbr' id='tb$id'>";
        foreach ($result as $data) {
            echo '<tr><td class="tbe">' . $label++ . '</td><td>' . implode('</td><td>', $data) . "</td></tr>\n" ;
        }

        echo "</table>";
    }
}

With this version, when it runs out of a-z after 26 rows, it will move on to aa, ab, ac etc.

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.