Jump to content

how to generate an A-to-Z list?


ivytony

Recommended Posts

I want to retrieve merchant names stored in my database in an A-Z list fashion. The column of merchant names has single-word names and multi-word names. I'd like to make the A-Z list by the first letter in the names. The number of merchants is around 10,000.

 

 

Can someone teach me how to do it?

Link to comment
https://forums.phpfreaks.com/topic/88067-how-to-generate-an-a-to-z-list/
Share on other sites

I'd so something like this:

 

<?php
$sql = "SELECT DISTINCT UPPER(SUBSTRING(merchant,1,1)) AS first_letter FROM tbl ORDER BY first_letter";
$result = mysql_query($sql) or die(mysql_error());
while(list($letter) = mysql_fetch_row($result)){
    echo '<a href="'.$_SERVER['PHP_SELF'].'?letter='.$letter.'">'.$letter.'</a> ';
}
if(isset($_GET['letter'])){
    $letter = mysql_real_escape_string($_GET['letter']);
    $sql = "SELECT merchant FROM tbl WHERE merchant LIKE '$letter%'";
    $result = mysql_query($sql) or die(mysql_error());
    echo '<br /><br />';
    while(list($merchant) = mysql_fetch_row($result)){
        echo $merchant.'<br />';
    }
}
?>

 

I know you said about using anchors; but with 10,000 records, there's going to be a lot of merchants beginning with some letters, meaning a lot of scrolling for the user. Therefore, i'd use the above with some pagination.

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.