mattix Posted October 12, 2018 Share Posted October 12, 2018 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 12, 2018 Share Posted October 12, 2018 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? Quote Link to comment Share on other sites More sharing options...
mattix Posted October 12, 2018 Author Share Posted October 12, 2018 (edited) 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 October 12, 2018 by mattix Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 12, 2018 Share Posted October 12, 2018 Once again - WHAT are you trying to do? Use plain English please without any reference to code Quote Link to comment Share on other sites More sharing options...
mattix Posted October 12, 2018 Author Share Posted October 12, 2018 Here I create monthly html tables for the data. So each month has its own table, and data. Each table has a unique id, and the cells too. Quote Link to comment Share on other sites More sharing options...
mattix Posted October 12, 2018 Author Share Posted October 12, 2018 I don't really seek an alternative code, the code works fine, I just can't make it work for the letters. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 12, 2018 Share Posted October 12, 2018 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. Quote Link to comment Share on other sites More sharing options...
mattix Posted October 12, 2018 Author Share Posted October 12, 2018 (edited) 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 October 12, 2018 by requinix fixed quoting Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.