I recently did a similar function, and heres what I used to pull the letters.
SELECT COUNT(title) as count, LEFT(title,1) as letter FROM entries GROUP BY letter ORDER BY title ASC
(Where in you case, title would be company_name and entries companies or similar.)
With this, all entries will be ordered correctly (A-Z) and you'll get the first letter and the number of times it occured, and you won't get the letters which no entry begins with.
Then I chose to return an array with all the letters as keys and the count as value, as in
PHP Code:
array( 'A' => 3, 'B' => 1, 'F' => 7, // and so on );
So in your loop, you can easly check if there are any entries for a certain letter by checking if that letter is in the array with the key_array_exists() function. Plus, you can get the number of entries for that letter by using the letter as index for the array, as in $array[$letter].
In my case I also specially handled entries starting with a digit, and added them all to a "letter" called '0-9', giving me
PHP Code:
array( '0-9' => 3, 'A' => 3, 'B' => 1, 'F' => 7, // and so on );
Hope this helps!
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
|