I'm creating a glossary page that has the letters of the alphabet listed across the top, each being a bookmark that takes you to the relevant letter section on the page. If there are no entries for a letter in the DB, I don't want the letter to be a link.
At the moment, i do 26 SELECT LIKE statements (e.g. 'a%', 'b%', etc.) - and it works, but it's grossly ineffcient.
Can I do this in a single MySQL statement and then do the processing in PHP? I'm assuming there is some kind of GROUP BY syntax I can use?
I know that thanks, but is there a way of efficiently processing those results, rather than iterating through the array and doing string compares and stuff?
It was so close! Just the substring call should have been (x, 1, 1) rather than (x, 0, 1). Yup, that's the totally inconsitent world of programming for you!
I'm the opposite of you, I've been living here since I was 18...
I've managed to use this code successfully but I now need to take the value of FirstLetter and use it in my PHP code. I've tried searching on the web for tips but as I don't quite know what you call the FirstLetter 'variable' (a temp store?) it's making it a little difficult! Please can someone tell me how i would use this in PHP code or what I need to search on the web - I'm using dial up so it takes quite a while!
$rstLetters = mysql_query('SELECT substring(columnname,1,1) AS FirstLetter,count(*) As NumItems FROM table GROUP BY FirstLetter ORDER BY FirstLetter');
while($arrLetters = mysql_fetch_array($rstLetters)) {
.......
then the first letter is in $arrLetters['FirstLetter'] within the while loop and FirstLetter in this scenario is called an "alias".
Thanks Dai.
However, I don't completely understand how to fit this in with what I want to do. (bit of a PHP newbie!)
I am trying to create an alphabetical search facility for a music catalogue. So far, I have a page which outputs the first letter of all the artists in the database as a hyperlink. I then want them to be able to click on the hyperlinked letter which will then display all artists which start with that letter. I am a bit stuck on how to create the SQL query to do this using the alias. I've attached the code I have written so far - pls can you take a little look and tell me where I am going wrong!
Bookmarks