views:

152

answers:

3

I have a text file that reads:

9123 Bellvue Court
5931 Walnut Creek rd.
Andrew
Bailey
Chris
Drew
Earl
Fred
Gerald
Henry
Ida
Jake
Koman
Larry
Manny
Nomar
Omar
Perry
Quest
Raphael
State
Telleman
Uruvian
Vixan
Whales
Xavier
Yellow
Zebra

What I need to do is I need to create a A-Z listing... so:

# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

and when you click on the letter it will bring up a table with only the words beginning with A's if I clicked A and only the words beginning with numbers if I clicked the # sign.

I was thinking of using a regular expression to accomplish this but I don't want to create 27 different pages. So is there a way to call the letter at the end of the url? like creating something that will do this

http://mywebsite/directory.php?letter=A
+2  A: 

A very simple approach:

Read in the text file:

$inputfile = file('words.txt');

Then, AFTER sanitizing the input ($letter = $_GET['letter']), you can build a regex:

$regex = '/^'.$letter.'/i';

and filter the rows you want to show:

$result = preg_grep($regex, $inputfile);

the rest is then simply a matter of outputting nice HTML (or whatever the output shall be)


Keep in mind: When the pages are frequently read, it is a lot faster to have the file stored in a database. You should also take a look into caching mechanisms if load should be a problem at some time in the future


Edit: forgot to mention: To get the # working, you need to add a line along the following:

if ($letter == '#') $letter = '[0-9]';

to get the regex working again.

Cassy
Alright I'll keep the database in mind, and thank you for the response. I'll let you know how it comes out!
D3V1NE
A: 

Yes.

You can access that variable to determine what to sort on by using

$letter = $_GET["letter"]
$arrayCount = preg_match('/^'.$letter."./", $textFileContents, $matches);

Something like that should work

Crowe T. Robot
A: 

That'd be mad unless you only have a few names in the file.

Unless you have to be terribly dynamic tell Cron to cache 26 text files from your central file each hour/day etc

a.htm etc

Once a day does me, I educated my users to understand that this is how their site would behave.

(A-Z is created from about 10 different applications' content)

Cups