简体   繁体   中英

PHP/SQL/Wordpress: Group a user list by alphabet

I want to create a (fairly big) Wordpress user index with the users categorized alphabetically, like this:

A

  • Amy
  • Adam

B

  • Bernard
  • Bianca

and so on.

I've created a custom Wordpress query which works fine for this, except for one problem: It also displays "empty" letters, letters where there aren't any users whose name begins with that letter. I'd be glad if you could help me fix this code so that it only displays the letter if there's actually a user with a name of that letter :) I've tried my luck by checking how many results there are for that letter, but somehow that's not working.

(FYI, I use the user photo plugin and only want to show users in the list who have an approved picture, hence the stuff in the SQL query).

<?php
    $alphabet = range('A', 'Z');
    foreach ($alphabet as $letter) {
      $user_count = $wpdb->get_results("SELECT COUNT(*) FROM wp_users WHERE display_name LIKE '".$letter."%' ORDER BY display_name ASC");
      if ($user_count > 0) {
        $user_row = $wpdb->get_results("SELECT wp_users.user_login, wp_users.display_name 
        FROM wp_users, wp_usermeta
        WHERE wp_users.display_name LIKE '".$letter."%' 
        AND wp_usermeta.meta_key = 'userphoto_approvalstatus'
        AND wp_usermeta.meta_value = '2'
        AND wp_usermeta.user_id = wp_users.ID
        ORDER BY wp_users.display_name ASC");
        echo '<li class="letter">'.$letter.'';
        echo '<ul>';
        foreach ($user_row as $user) { 
          echo '<li><a href="/author/'.$user->user_login.'">'.$user->display_name.'</a></li>';
        }
      echo '</ul></li>';
      }                     
    }
    ?>

Thanks in advance!


Update:

I edited the SQL into this, but now I have the problem that it spits out an </ul> before the first li, which screws up the layout, like this:

</ul>
</li>
<li class="letter">
    <div class="letter_head">A</div>
  <ul>
    <li>Abigail</li>
  </ul>
</li>

<li class="letter">
<div class="letter_head">B</div>
<ul>
  <li>Bernard</li>      
  <li>Bianca</li>       
</li>
</ul>

It's in general pretty wonky about the ul and li nesting. How can I fix it?

            <?php 
    $qry = "SELECT DISTINCT wp_users.user_login, wp_users.display_name, 
            LEFT(UPPER(wp_users.display_name), 1) AS first_char 
            FROM wp_users, wp_usermeta
            WHERE UPPER(wp_users.display_name) BETWEEN 'A' AND 'Z'
            OR wp_users.display_name BETWEEN '0' AND '9'  
            AND wp_usermeta.meta_key = 'userphoto_approvalstatus'
            AND wp_usermeta.meta_value = '2'
            AND wp_usermeta.user_id = wp_users.ID
            ORDER BY wp_users.display_name ASC";
            $result = mysql_query($qry);
            $current_char = '';
            while ($row = mysql_fetch_assoc($result)) {
                if (!isset($current_char)) {
                        echo '<li class="letter"><div class="letter_head">'.$current_char.'</div>';
                        echo '<ul>';
                } elseif ($row['first_char'] != $current_char) {
                        echo '</ul>';
                        $current_char = $row['first_char'];
                        echo '<li class="letter"><div class="letter_head">'.$current_char.'</div>';
                        echo '<ul>';
                }
                echo '<li><a href="/member/'.$row['user_login'].'">'.$row['display_name'].'</a></li>';
            }  
    ?>

A better solution would be to loop through all users in alphabetical order and for every record check if the first letter differs from that of the last record. If different, you echo out the new letter.

You are doing 52 database calls. Is the user table too big to grab all of them in one query? Also, I believe the order by on the count is unnecessary. I do not know if MySQL ignores it or not.

Your problem is get_results returns an single element array every time for the count query. So $user_count > 0 is always returning true. (I don't understand PHP logic most of the time. Is an array greater than zero?). You need to do something like $user_count[0] to get at the actual number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM