Jump to content

MYSQL results two column + alph. grouping


Jikson26

Recommended Posts

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

 

ssax6.jpg

 

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

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

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

 

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]

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.