ivytony Posted February 23, 2008 Share Posted February 23, 2008 I'd like to make an A-Z list as this one: http://www.dealdigs.com/coupons.php I am wondering how to order the merchant names stored in mysql for output? Quote Link to comment https://forums.phpfreaks.com/topic/92605-how-can-i-output-an-a-z-list/ Share on other sites More sharing options...
fenway Posted February 23, 2008 Share Posted February 23, 2008 With an ORDER BY clause... how do you mean? Quote Link to comment https://forums.phpfreaks.com/topic/92605-how-can-i-output-an-a-z-list/#findComment-474652 Share on other sites More sharing options...
ivytony Posted February 23, 2008 Author Share Posted February 23, 2008 I mean to group the output by the first letter in each merchant name, like this: A: B: C: I am wondering if I need to make the 'ORDER BY clause' run 26 times for the 26 letters (A-Z). thanks Quote Link to comment https://forums.phpfreaks.com/topic/92605-how-can-i-output-an-a-z-list/#findComment-474657 Share on other sites More sharing options...
Barand Posted February 23, 2008 Share Posted February 23, 2008 <?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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/92605-how-can-i-output-an-a-z-list/#findComment-474665 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.