Jikson26 Posted March 25, 2007 Share Posted March 25, 2007 Hi everyone, I have this code: $counter = 0; $lastletter = ''; echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"1\">\n"; $counter = 0; $lastletter = ''; while ($entry = mysql_fetch_assoc($result)) { $first_char = strtoupper($entry['name'][0]); if ($first_char != $lastletter) { $lastletter = $first_char; echo "<tr>\n"; echo '<td colspan="2" style="text-align: center; font-weight: bold;">'. $first_char .'</tr>'. "\n"; echo "</tr>\n"; echo "<tr>\n"; } echo "\t<td>{$entry['name']}</td>\n"; if (++$counter % 2 == 0) { echo "</tr>\n<tr>\n"; } } echo "</tr> </table>\n"; It display like this The "B" group is incorrectly displayed. I would like it to display: A apples B bananas birds bamboo How would I go about correcting this error? Your help will be greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/44247-mysql-results-two-column-alph-grouping/ Share on other sites More sharing options...
Barand Posted March 25, 2007 Share Posted March 25, 2007 try <?php echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"1\">\n"; $counter = 0; $lastletter = ''; while ($entry = mysql_fetch_assoc($res)) { $first_char = strtoupper($entry['name'][0]); if ($first_char != $lastletter) { $lastletter = $first_char; if ($counter % 2 != 0) echo "</tr>\n"; echo "<tr>\n"; echo '<td colspan="2" style="text-align: center; font-weight: bold;">'. $first_char .'</td></tr>'. "\n"; $counter = 0; } if ($counter % 2 == 0) echo "<tr>\n"; echo "\t<td>{$entry['name']}</td>\n"; ++$counter; if ($counter % 2 == 0) echo "</tr>\n"; } if ($counter % 2 != 0) echo "</tr>\n"; echo "</table>\n"; ?> Link to comment https://forums.phpfreaks.com/topic/44247-mysql-results-two-column-alph-grouping/#findComment-214997 Share on other sites More sharing options...
Waldir Posted March 25, 2007 Share Posted March 25, 2007 while ($entry = mysql_fetch_assoc($res)) { $first_char = strtoupper($entry['name']{0}); echo (($first_char != $lastletter) ? '<tr><td colspan="2" style="text-align: center; font-weight: bold;">'.$first_char.'</tr>'. "\n"; : '' ); $lastletter = $first_char; if ($counter % 2 == 0) echo "<tr>\n"; echo "\t<td>{$entry['name']}</td>\n"; ++$counter; if ($counter % 2 == 0) echo "</tr>\n"; } if ($counter % 2 != 0) echo "</tr>\n"; echo "</table>\n"; Link to comment https://forums.phpfreaks.com/topic/44247-mysql-results-two-column-alph-grouping/#findComment-215001 Share on other sites More sharing options...
Barand Posted March 25, 2007 Share Posted March 25, 2007 Waldir, If there is an odd number beginning with a particular letter, then yours too, like Jiksons, goes wrong on the next letter [pre] | A | | | Abdy | Akbar | | | Aldcroft | Alexander | | | Allen | | | | B | Banfield | | Bangudu | Banks | | | Banks | Barbour | | | Barcroft | Barkley | | | C | | | Calder | Carr | | | Carter | Cartwright | | [/pre] Link to comment https://forums.phpfreaks.com/topic/44247-mysql-results-two-column-alph-grouping/#findComment-215008 Share on other sites More sharing options...
Waldir Posted March 25, 2007 Share Posted March 25, 2007 i missread the question, Barand is totaly right ^^ Link to comment https://forums.phpfreaks.com/topic/44247-mysql-results-two-column-alph-grouping/#findComment-215012 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.