List and search users with specific role id and certain values in profile fields
Last modified: January 15, 2008 - 15:11
This page snippet shows a list of users with a specific role ID and certain values in their profile fields. Because the search keyword is obtained from a form, this page snippet implements a kind of search engine for user profiles. If a user have uploaded its picture, it is also shown in te resulting user list. The pager query in the following php code snippet shows 20 users on each page. The html form points to node with id 999, please change this value to the id of your own drupal page.
<h3>Search User Profiles</h3>
<p>Type your search keyword in the followong form and click on the "Search" button.</p>
<form action="index.php?q=node/999" method="post">
Search for keyword: <input name="searchkeyword" type="text" size="30" maxlength="128">
<input type="submit" value=" Search ">
</form>
<?php
//choose the role to list by value.
// Note ID 1 = anonymous, ID 2 = authenticated user
// so valid values here are > 2.
$searchkeyword = $_POST["searchkeyword"];
if ( $searchkeyword!="" ) {
print "Hier die Liste aller User, bei denen in einem der Profilfelder der Suchbegriff <strong>" . $suchbegriff ."</strong> vorkommt";
$rid=5;
$header = array(
array('data' => t('Username'), 'field' => 'name', 'sort' => 'asc'),
array('data' => t('Status'), 'field' => 'status'),
array('data' => t('Member for'), 'field' => 'created'),
array('data' => t('Last access'), 'field' => 'access'),
array('data' => t('Datenfeld'), 'field' => 'p_feldname'),
array('data' => t('Picture'), 'field' => 'picture')
);
$sql = "SELECT u.uid, u.name, u.status, u.created, u.access, u.picture, MAX(pf.name) p_feldname FROM users u INNER JOIN users_roles ur ON u.uid = ur.uid INNER JOIN ( profile_values pv INNER JOIN profile_fields pf ON pv.fid = pf.fid ) ON u.uid = pv.uid WHERE ur.rid=$rid AND pv.value LIKE '%".$searchkeyword."%' GROUP BY 1,2,3,4,5";
$sql_count = "SELECT COUNT(DISTINCT u.uid) row_count FROM users u INNER JOIN users_roles ur ON u.uid = ur.uid INNER JOIN ( profile_values pv INNER JOIN profile_fields pf ON pv.fid = pf.fid ) ON u.uid = pv.uid WHERE ur.rid=$rid AND pv.value LIKE '%".$searchkeyword."%'";
$sql .= tablesort_sql($header);
$result = pager_query($sql, 20,0,$sql_count );
$status = array(t('blocked'), t('active'));
while ($account = db_fetch_object($result)) {
$rows[] = array(theme('username', $account),
$status[$account->status],
format_interval(time() - $account->created),
$account->access ? t('%time ago', array('%time' => format_interval(time() - $account->access))) : t('never'),
$account->p_feldname,
theme_user_picture($account)
);
}
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 10, 0);
print ($output);
}
?>
<p>If yuo have some <strong >feedback/proposals/wishes</strong> concerning this page, please post a comment or contact the admin directly via <a href="?q=user/1/contact">e-mail</a>.</p>