
March 30th, 2007, 01:32 PM
|
|
Registered User
|
|
Join Date: Apr 2007
Posts: 22,309
Time spent in forums: < 1 sec
Reputation Power: 24
|
|
|
RE: REQUEST: A-Z index in PHP
The following code is a generic "link" page. This code does a directory listing, but you would replace the directory portion of the code with your database query and display code. If you want a separate link for each number/letter, just change the $link_array[] setup -
php Code:
Original
- php Code |
|
|
|
<?php // function to create an index link given the index name return "<a href="{$_SERVER['PHP_SELF']}?letter=$letter">$letter</a>"; } // create a general purpose array of index names/links // change the elements of this array to change how the links/files are organized $link_array[] = "0-9"; $link_array[] = "A"; $link_array[] = "B"; $link_array[] = "C"; $link_array[] = "D-F"; $link_array[] = "G-H"; $link_array[] = "I"; $link_array[] = "J-L"; $link_array[] = "M"; $link_array[] = "N-O"; $link_array[] = "P"; $link_array[] = "Q-S"; $link_array[] = "T"; $link_array[] = "U"; $link_array[] = "V-Z"; // form the index link $content =""; foreach($link_array as $value){ $content .= " ". link($value). " |"; } $content = rtrim($content, "|"); // output the index link at the top of the page echo "<h4 style="text-align: center ">Jump to: [". $content. "]</h4>"; If(isset($_GET['letter'])){ $letter = trim($_GET['letter']); } else { exit; // on the first pass, exit at this point } // echo the current index letter selected echo "<h2 style="text-align: center ">$letter</h2>"; // process this for a range of letters $first_letter = strtoupper(substr($letter, 0, 1)); // get first letter and insure it is upper case $last_letter = strtoupper(substr($letter, - 1, 1)); // get last letter and insure it is upper case $current_letter = $first_letter; // get the starting letter (this will be upper case) // loop for all letters in a range while($current_letter <= $last_letter){ // ********************* change the following code, up to the next ****** comment, to retrieve from your database // get a directory listing of all lower and upper case files that start with the current letter $lowercase = glob("$current_letter*.*"); $uppercase = glob("$current_letter*.*"); // sort the array of file names, ignoring case and handling numbers correctly // display a link for each file name foreach ($bothcase as $filename) { echo "<a href="fdownload.php? file= ".urlencode($filename)."">$filename</a><br />n"; } // ********************** the following code remains the same // step to the next letter in a range (if any) $current_letter++; // note, when this is Z, it becomes AA // special handling for incrementing "Z" if($current_letter == "AA"){ break; } } // end of while loop // output the index link at the bottom of the page echo "<h4 style="text-align: center ">Jump to: [". $content. "]</h4>"; ?>
|