Need help creating a PHP 'A to Z'

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
demonseed101
Forum Newbie
Posts: 3
Joined: Tue Jan 31, 2006 1:00 am

Need help creating a PHP 'A to Z'

Post by demonseed101 »

Hi

I'm new to using PHP and MySQL and I'm trying to create a web-based database of my music collection.

I have all of the artists in a DB table, and from this am trying to produce an unordered list A to Z, and where the letter exists as the initial letter of an artist, to make this a link to refine the view - the letters that don't exist in the DB must still be shown - just not be links.

To select the letters of the artists I'm using:

Code: Select all

SELECT distinct left(artist,1) FROM bb_artists
I have also put each letter of the alphabet as individual rows in another table called alphabet.

I'm not quite sure what I need to be doing. I've been using a mixture of while and for loops, but to no real avail. At my worst I have got 26 of each letter echoed with one a link, and at my best, 1 of A, 2 of B.... 26 of Z (they both sound bad really :wink:) .

Thanks in advance if anyone can tell me how to solve this.

:: demonseed101 ::
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

for($i=ord('A');$i<=ord('Z');$ord++) {
echo chr($i).' ';
}
Selecting the distinct letters from the table is unneeded overhead each time.


To get artists by a certain letter do:

Code: Select all

select * from artists where name like 'A%'
for instance. You should verify that the data passed to you is a valid letter between A and Z or someone could pass you a % (the wildcard) and list the entire table
demonseed101
Forum Newbie
Posts: 3
Joined: Tue Jan 31, 2006 1:00 am

Post by demonseed101 »

Thanks for the reply.

However, the code to echo the alphabet created 1000s of each letter and crashed my browser.

:: demonseed101 ::
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

It should not, did you put my code inside of another loop or something?
demonseed101
Forum Newbie
Posts: 3
Joined: Tue Jan 31, 2006 1:00 am

Post by demonseed101 »

No, I did on a fresh page.

Code: Select all

<?php for($i=ord('A');$i<=ord('Z');$ord++) {
echo chr($i).' ';
} ?>
Post Reply