SunQuest
           Tutorials
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOtherTutorials

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old March 30th, 2007, 09:01 AM
CreativeCozza CreativeCozza is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: England
Posts: 1 CreativeCozza User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
REQUEST: A-Z index in PHP

Hello. My first post, so please be nice to me!

I am creating a website for an assessment and I have an advanced search on there. I would like to have an alphabetic search where the user clicks on the A and it brings up everything in the database beginning with A. I have an idea on how it works but can someone PLEASE help me.

Thank you.
Cara :-)

Reply With Quote
  #2  
Old March 30th, 2007, 01:32 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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
  1.  
  2. <?php
  3. // function to create an index link given the index name
  4. function link($letter){
  5. return "<a href="{$_SERVER['PHP_SELF']}?letter=$letter">$letter</a>";
  6. }
  7.  
  8. // create a general purpose array of index names/links
  9. // change the elements of this array to change how the links/files are organized
  10. $link_array = array();
  11. $link_array[] = "0-9";
  12. $link_array[] = "A";
  13. $link_array[] = "B";
  14. $link_array[] = "C";
  15. $link_array[] = "D-F";
  16. $link_array[] = "G-H";
  17. $link_array[] = "I";
  18. $link_array[] = "J-L";
  19. $link_array[] = "M";
  20. $link_array[] = "N-O";
  21. $link_array[] = "P";
  22. $link_array[] = "Q-S";
  23. $link_array[] = "T";
  24. $link_array[] = "U";
  25. $link_array[] = "V-Z";
  26.  
  27. // form the index link
  28. $content ="";
  29. foreach($link_array as $value){
  30. $content .= " ".link($value)." |";
  31. }
  32. $content = rtrim($content, "|");
  33.  
  34. // output the index link at the top of the page
  35. echo "<h4 style="text-align: center">Jump to: [".$content."]</h4>";
  36.  
  37. If(isset($_GET['letter'])){
  38. $letter = trim($_GET['letter']);
  39. } else {
  40. exit; // on the first pass, exit at this point
  41. }
  42.  
  43. // echo the current index letter selected
  44. echo "<h2 style="text-align: center">$letter</h2>";
  45.  
  46. // process this for a range of letters
  47. $first_letter = strtoupper(substr($letter, 0, 1)); // get first letter and insure it is upper case
  48. $last_letter = strtoupper(substr($letter, -1, 1)); // get last letter and insure it is upper case
  49.  
  50. $current_letter = $first_letter; // get the starting letter (this will be upper case)
  51.  
  52. // loop for all letters in a range
  53. while($current_letter <= $last_letter){
  54.  
  55. // ********************* change the following code, up to the next ****** comment, to retrieve from your database
  56. // get a directory listing of all lower and upper case files that start with the current letter
  57. $current_letter = strtolower($current_letter);
  58. $lowercase = glob("$current_letter*.*");
  59. $current_letter = strtoupper($current_letter);
  60. $uppercase = glob("$current_letter*.*");
  61. $bothcase = array_merge($lowercase,$uppercase);
  62.  
  63. // sort the array of file names, ignoring case and handling numbers correctly
  64. natcasesort($bothcase);
  65.  
  66. // display a link for each file name
  67. foreach ($bothcase as $filename) {
  68. echo "<a href="fdownload.php?file=".urlencode($filename)."">$filename</a><br />n";
  69. }
  70.  
  71. // ********************** the following code remains the same
  72. // step to the next letter in a range (if any)
  73. $current_letter++; // note, when this is Z, it becomes AA
  74.  
  75. // special handling for incrementing "Z"
  76. if($current_letter == "AA"){
  77. break;
  78. }
  79.  
  80. } // end of while loop
  81.  
  82. // output the index link at the bottom of the page
  83. echo "<h4 style="text-align: center">Jump to: [".$content."]</h4>";
  84. ?>

Reply With Quote
Reply

Viewing: Codewalkers ForumsOtherTutorials > REQUEST: A-Z index in PHP


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway