Alphabet Pagination


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function sitehelper_get_membernav(){
  2.  
  3. //get current url to build relative links
  4. $base = trim($_GET['q']);
  5.  
  6. //url matching to prevent multiple arguments for first letter e.g "membership-list/key/a/b"
  7. if(strstr($base, 'membership-list/key')){
  8. $base = 'membership-list/key';
  9. }elseif (strstr($base, 'membership-list/sponsor')){
  10. $base = 'membership-list/sponsor';
  11. }elseif (strstr($base, 'membership-list/regular')){
  12. $base = 'membership-list/regular';
  13. }else {
  14. $base = 'membership-list/all';
  15. }
  16.  
  17.  
  18. $terms = '';
  19. $where = '';
  20. //join terms table if url has a taxonomy argument
  21. if($base != 'membership-list/all'){
  22. $terms = "LEFT JOIN {term_node} term_node ON node.vid = term_node.vid".
  23. " INNER JOIN {term_data} term_data ON term_node.tid = term_data.tid";
  24. $term = substr($base, 16);
  25. print $term;
  26. $where = "AND term_data.name = '".$term."'";
  27. }
  28. //Select distinct first letters of current nodes
  29. $result = db_query("SELECT DISTINCT substring( node.title, 1, 1 ) AS letter
  30. FROM {node} node
  31. LEFT JOIN {content_type_member} content_type_member ON content_type_member.nid = node.nid
  32. ".$terms."
  33. WHERE node.type = 'member'
  34. AND node.status = '1'
  35. AND content_type_member.nid = node.nid
  36. ".$where."
  37. ORDER BY node.title ASC");
  38.  
  39. //array to hold our first letters
  40. $items = array();
  41. while ($row = db_fetch_array($result)) {
  42. $items[$row['letter']] = 1;
  43. }
  44.  
  45. //Build output for A-Z matching all first letters from $items when possible
  46. $output = '<a href="/'.$base.'/all">All</a>';
  47. foreach(range('A','Z') as $i) {
  48. if($items[$i] == 1) {
  49. $output .= ' | <a href="/'.$base.'/'.strtolower($i).'">'.strtoupper($i).'</a>';
  50. } else {
  51. $output .= ' | <span>'.strtoupper($i).'</span>';
  52. }
  53. }
  54.  
  55. return $output;
  56. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.