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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.