ivytony Posted January 27, 2008 Share Posted January 27, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/88067-how-to-generate-an-a-to-z-list/ Share on other sites More sharing options...
therealwesfoster Posted January 27, 2008 Share Posted January 27, 2008 SELECT * FROM names ORDER BY names DESC Change the column names to whatever you need. DESC = Descending ASC = Ascending Quote Link to comment https://forums.phpfreaks.com/topic/88067-how-to-generate-an-a-to-z-list/#findComment-450603 Share on other sites More sharing options...
ivytony Posted January 27, 2008 Author Share Posted January 27, 2008 thanks! but I meant to do a list like this: A, B, C, D, .... X, Y, Z and the letters are linked to anchored individual section whose merchant names begin with the corresponding letter. Any smart way to do this? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/88067-how-to-generate-an-a-to-z-list/#findComment-450693 Share on other sites More sharing options...
fenway Posted January 27, 2008 Share Posted January 27, 2008 I'm quite confused... you'd have all of the letters regardless. Quote Link to comment https://forums.phpfreaks.com/topic/88067-how-to-generate-an-a-to-z-list/#findComment-450704 Share on other sites More sharing options...
fenway Posted January 27, 2008 Share Posted January 27, 2008 Or you you mean using LEFT( yourColumn, 1 ) Quote Link to comment https://forums.phpfreaks.com/topic/88067-how-to-generate-an-a-to-z-list/#findComment-450705 Share on other sites More sharing options...
GingerRobot Posted January 27, 2008 Share Posted January 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/88067-how-to-generate-an-a-to-z-list/#findComment-450727 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.