Facebook  Twitter 

SMFHacks.com

+-

SMFHacks.com

+- User Information

Welcome, Guest.
Please login or register.
 
 
 
Forgot your password?

+- Forum Stats

Members
Total Members: 4255
Latest: andreios
New This Month: 3
New This Week: 1
New Today: 0
Stats
Total Posts: 43259
Total Topics: 7518
Most Online Today: 171
Most Online Ever: 2482
(April 09, 2011, 07:02:45 pm)
Users Online
Members: 0
Guests: 172
Total: 172

Author Topic: Search memberlist  (Read 3553 times)

0 Members and 1 Guest are viewing this topic.

Offline shuban

  • Hero Member
  • *****
  • Posts: 665
    • View Profile
    • Biology Forums
Search memberlist
« on: September 17, 2014, 05:10:47 pm »
I would like to add an option to search memberlist where the person searching can choose the exact username being search. Currently, if I search Chris, for instance, it will provide all members with the word Chris in it, like Gchris, Techrisye, etc. I provided an example of what I would like. Here is the function that deals with searching the memberlist.

Code: [Select]
// Search for members...
function MLSearch()
{
global $txt, $scripturl, $db_prefix, $context, $user_info, $modSettings;

$context['page_title'] = $txt['mlist_search'];

// They're searching..
if (isset($_REQUEST['search']) && isset($_REQUEST['fields']))
{
$_POST['search'] = trim(isset($_GET['search']) ? $_GET['search'] : $_POST['search']);
$_POST['fields'] = isset($_GET['fields']) ? explode(',', $_GET['fields']) : $_POST['fields'];

$context['old_search'] = $_REQUEST['search'];
$context['old_search_value'] = urlencode($_REQUEST['search']);

// No fields?  Use default...
if (empty($_POST['fields']))
$_POST['fields'] = array('name');

// Search for a name?
if (in_array('name', $_POST['fields']))
$fields = array('memberName', 'realName');
else
$fields = array();
// Search for messengers...
if (in_array('messenger', $_POST['fields']) && (!$user_info['is_guest'] || empty($modSettings['guest_hideContacts'])))
$fields += array(3 => 'MSN', 'AIM', 'ICQ', 'YIM');
// Search for websites.
if (in_array('website', $_POST['fields']))
$fields += array(7 => 'websiteTitle', 'websiteUrl');
// Search for groups.
if (in_array('group', $_POST['fields']))
$fields += array(9 => 'IFNULL(groupName, \'\')');
// Search for an email address?
if (in_array('email', $_POST['fields']))
{
$fields += array(2 => allowedTo('moderate_forum') ? 'emailAddress' : '(hideEmail = 0 AND emailAddress');
$condition = allowedTo('moderate_forum') ? '' : ')';
}
else
$condition = '';

$query = $_POST['search'] == '' ? "= ''" : "LIKE '%" . strtr($_POST['search'], array('_' => '\\_', '%' => '\\%', '*' => '%')) . "%'";

$request = db_query("
SELECT COUNT(*)
FROM {$db_prefix}members AS mem
LEFT JOIN {$db_prefix}membergroups AS mg ON (mg.ID_GROUP = IF(mem.ID_GROUP = 0, mem.ID_POST_GROUP, mem.ID_GROUP))
WHERE " . implode(" $query OR ", $fields) . " $query$condition
AND is_activated = 1", __FILE__, __LINE__);
list ($numResults) = mysql_fetch_row($request);
mysql_free_result($request);

$context['page_index'] = constructPageIndex($scripturl . '?action=mlist;sa=search;search=' . $_POST['search'] . ';fields=' . implode(',', $_POST['fields']), $_REQUEST['start'], $numResults, $modSettings['defaultMaxMembers']);

// Find the members from the database.
// !!!SLOW This query is slow.
$request = db_query("
SELECT mem.ID_MEMBER
FROM {$db_prefix}members AS mem
LEFT JOIN {$db_prefix}log_online AS lo ON (lo.ID_MEMBER = mem.ID_MEMBER)
LEFT JOIN {$db_prefix}membergroups AS mg ON (mg.ID_GROUP = IF(mem.ID_GROUP = 0, mem.ID_POST_GROUP, mem.ID_GROUP))
WHERE " . implode(" $query OR ", $fields) . " $query$condition
AND is_activated = 1
LIMIT $_REQUEST[start], $modSettings[defaultMaxMembers]", __FILE__, __LINE__);
printMemberListRows($request);
mysql_free_result($request);
}
else
{
$context['sub_template'] = 'search';
$context['old_search'] = isset($_REQUEST['search']) ? htmlspecialchars($_REQUEST['search']) : '';
}

$context['linktree'][] = array(
'url' => $scripturl . '?action=mlist;sa=search',
'name' => &$context['page_title']
);
}

This is the code for the template:

Code: [Select]
<tr>
<td align="left">
<label for="fields-email"><input type="checkbox" name="fields[]" id="fields-email" value="email" checked="checked" class="check" /> ', $txt['mlist_search_email'], '</label><br />
<label for="fields-messenger"><input type="checkbox" name="fields[]" id="fields-messenger" value="messenger" class="check" /> ', $txt['mlist_search_messenger'], '</label><br />
<label for="fields-group"><input type="checkbox" name="fields[]" id="fields-group" value="group" class="check" /> ', $txt['mlist_search_group'], '</label>
</td>
<td align="left" valign="top">
<label for="fields-name"><input type="checkbox" name="fields[]" id="fields-name" value="name" checked="checked" class="check" /> ', $txt['mlist_search_name'], '</label><br />
<label for="fields-website"><input type="checkbox" name="fields[]" id="fields-website" value="website" class="check" /> ', $txt['mlist_search_website'], '</label>
</td>
</tr>

Any help would be really helpful.

Offline SMFHacks

  • Administrator
  • Hero Member
  • *****
  • Posts: 16436
    • View Profile
Re: Search memberlist
« Reply #1 on: September 17, 2014, 05:20:20 pm »
This is code that does the wildcards
Code: [Select]
$query = $_POST['search'] == '' ? "= ''" : "LIKE '%" . strtr($_POST['search'], array('_' => '\\_', '%' => '\\%', '*' => '%')) . "%'";
To make it exact change to probably
Code: [Select]
$query = $_POST['search'] == '' ? "= ''" : "= '" . strtr($_POST['search'], array('_' => '\\_', '%' => '\\%', '*' => '%')) . "'";
Get your Forum Ranked! at https://www.forumrankings.net - find out how your forum compares with others!

Like What I do? Support me at https://www.patreon.com/vbgamer45/

Offline shuban

  • Hero Member
  • *****
  • Posts: 665
    • View Profile
    • Biology Forums
Re: Search memberlist
« Reply #2 on: September 17, 2014, 07:40:49 pm »
Thanks. Here's what I did:

Code: [Select]
// Search for a name?
if (in_array('name', $_POST['fields']) || in_array('exact', $_POST['fields']))
$fields = array('memberName', 'realName');

Code: [Select]
if (in_array('exact', $_POST['fields']))
$query = $_POST['search'] == '' ? "= ''" : "= '" . strtr($_POST['search'], array('_' => '\\_', '%' => '\\%', '*' => '%')) . "'";
else
$query = $_POST['search'] == '' ? "= ''" : "LIKE '%" . strtr($_POST['search'], array('_' => '\\_', '%' => '\\%', '*' => '%')) . "%'";

In template:

Code: [Select]
<label for="fields-exact"><input type="checkbox" name="fields[]" id="fields-exact" value="exact" class="check" /> Search exact username</label>
Now you can search exact usernames!

 

Related Topics

  Subject / Started by Replies Last post
0 Replies
6637 Views
Last post October 04, 2006, 09:20:49 pm
by SMFHacks
3 Replies
4953 Views
Last post May 09, 2009, 06:31:12 pm
by Matth41
0 Replies
7830 Views
Last post September 11, 2009, 06:58:59 am
by Sicarius
2 Replies
14420 Views
Last post February 06, 2014, 08:21:06 am
by Jonas1975
0 Replies
2128 Views
Last post September 17, 2018, 05:00:59 pm
by SMFHacks

+- Recent Topics

No thumbnails on new uploads by SMFHacks
March 27, 2024, 02:10:41 pm

Display the Contact Page for guests by SMFHacks
March 27, 2024, 10:55:43 am

is it possible to add support for odysee.com by fvlog19
March 21, 2024, 08:47:51 am

Request for admin notification by davejo
March 10, 2024, 01:31:59 am

I need help with torrent upload by Ineedsmfhelp
March 09, 2024, 10:01:13 pm

an idea for new mod (( content type with different display )) by SMFHacks
February 27, 2024, 01:36:27 pm

[Mod] RSS Feed Poster by SMFHacks
February 27, 2024, 11:57:18 am

find duplicate pictures by fvlog19
February 14, 2024, 02:22:40 pm

Error uploading video. by SMFHacks
February 08, 2024, 02:04:16 pm

Gallery icon as last added image by fvlog19
February 01, 2024, 01:04:56 pm

Powered by EzPortal