We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

coggla on 01/20/08


Tagged

mysql


Versions (?)


Who likes this?

4 people have marked this snippet as a favorite

mbcdg
DeadLy
Morgano
hochitom


A-Z Index


Published in: PHP 


Get an A-Z index from MySQL query. Link only letters where there are matches.

  1. $query = "SELECT DISTINCT UPPER(LEFT(LNAME,1)) as letters FROM $table ORDER BY letters";
  2.  
  3. if ($result = mysql_query($query)) {
  4. while ($row = mysql_fetch_array($result)) {
  5. $letter_string .= $row["letters"];
  6. }
  7. }
  8.  
  9. $letter_array = range('A','Z');
  10.  
  11. foreach ($letter_array as $letter) {
  12. if (stristr($letter_string, $letter) === FALSE) echo $letter . " | ";
  13. else echo "<a href='list.php?ltr=".$letter."'>".$letter."</a> | ";
  14. }

Report this snippet 

You need to login to post a comment.