Jump to content

How can I output an A-Z list?


ivytony

Recommended Posts

<?php
mysql_connect('localhost');
mysql_select_db('test');

$sql = "SELECT name FROM merchants
        ORDER BY name";
$res = mysql_query($sql);

$prevLetter = '';
while ($row = mysql_fetch_row($res)){
    $name = $row[0];
    $letter = $name[0];                    // get first letter
    if ($letter != $prevLetter) {          // is it different from previous one?
        echo "<h4>$letter</h4>";           // if yes, output new letter
        $prevLetter = $letter;             // reset previous one
    }
    echo $name, '<br/>';                   // output the name
}

?>

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.