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.

Edited by mattix
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.

 

Edited by requinix
fixed quoting
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.