MyBB Community Forums

Full Version: Search memberlist for custom profile fields
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi,

this is a hack for Advanced Memberlist Search. By default it is not possible to filter the search result with the help custom profile fields. But with this simple code modification it becomes possible! Smile

  1. Open file memberlist.php in an appropriate file editor.
    1. Find:
      // Showing advanced search page?
      if($mybb->input['action'] == "search")
      {
      	eval("\$search_page = \"".$templates->get("memberlist_search")."\";");
      	$plugins->run_hooks("memberlist_search");
      	output_page($search_page);	
      }
      

    2. Replace with:
      // Showing advanced search page?
      if($mybb->input['action'] == "search")
      {
      	$altbg = "trow2";
      	$query = $db->simple_select("profilefields", "*", "type!='checkbox' AND type!='multiselect' AND type!='textarea' AND editable=1 AND hidden=0", array('order_by' => 'disporder'));
      	while($profilefield = $db->fetch_array($query))
      	{
      		$profilefield['type'] = htmlspecialchars_uni($profilefield['type']);
      		$thing = explode("\n", $profilefield['type'], "2");
      		$type = $thing[0];
      		$options = $thing[1];
      		$field = "fid{$profilefield['fid']}";
      		$select = '<option value=""></option>';
      		$userfield = $user[$field];
      		if($type == "select")
      		{
      			$expoptions = explode("\n", $options);
      			if(is_array($expoptions))
      			{
      				foreach($expoptions as $key => $val)
      				{
      					$val = trim($val);
      					$val = str_replace("\n", "\\n", $val);
      					$select .= "<option value=\"{$val}\">{$val}</option>";
      				}
      				if(!$profilefield['length'])
      				{
      					$profilefield['length'] = 1;
      				}
      				$code = "<select id=\"{$field}\" name=\"{$field}\" size=\"{$profilefield['length']}\">{$select}</select>";
      			}
      		}
      		elseif($type == "radio")
      		{
      			$expoptions = explode("\n", $options);
      			if(is_array($expoptions))
      			{
      				foreach($expoptions as $key => $val)
      				{
      					$code .= "<input type=\"radio\" class=\"radio\" name=\"{$field}\" value=\"{$val}\" /> <span class=\"smalltext\">{$val}</span><br />";
      				}
      			}
      		}
      		else
      		{
      			$maxlength = "";
      			if($profilefield['maxlength'] > 0)
      			{
      				$maxlength = " maxlength=\"{$profilefield['maxlength']}\"";
      			}
      			$code = "<input id=\"{$field}\" type=\"text\" name=\"{$field}\" class=\"textbox\" size=\"{$profilefield['length']}\"{$maxlength} value=\"\" />";
      		}
      		if($type != "radio")
      		{
      			$profilefield['name']="<label for=\"{$field}\">{$profilefield['name']}</label>";
      		}
      		$customfields.="<tr><td class=\"{$altbg}\"><strong>{$profilefield['name']}</strong></td><td class=\"{$altbg}\">{$code}</td></tr>";
      		$altbg = alt_trow();
      		$code = "";
      		$select = "";
      		$val = "";
      		$options = "";
      		$expoptions = "";
      	}
      	eval("\$search_page = \"".$templates->get("memberlist_search")."\";");
      	$plugins->run_hooks("memberlist_search");
      	output_page($search_page);	
      }
      

    3. Find:
      	$query = $db->simple_select("users u", "COUNT(*) AS users", "{$search_query}");
      

    4. Replace with:
      	$query = $db->query("
      		SHOW FULL COLUMNS
      		FROM ".TABLE_PREFIX."userfields
      	");
      	$columns = 1;
      	while($rows = $db->fetch_array($query))
      	{
      		if(trim($mybb->input['fid'.$columns]))
      		{
      			$search_query .= " AND f.fid".$columns." LIKE '%".$db->escape_string_like($mybb->input['fid'.$columns])."%'";
      			$search_url .= "&fid".$columns."=".urlencode($mybb->input['fid'.$columns]);
      		}
      		$columns++;
      	}
      	$query = $db->query("
      		SELECT COUNT(*) AS users
      		FROM ".TABLE_PREFIX."users u
      		LEFT JOIN ".TABLE_PREFIX."userfields f ON (f.ufid=u.uid)
      		WHERE {$search_query}
      	");
      
  2. Go into your ACP and open the template memberlist_search.
    1. Find:
      <tr>
         <td class="trow1" style="vertical-align: top;" width="20%"><strong><label for="username">{$lang->username}</label></strong></td>
         <td class="trow1">
             <select name="username_match">
                 <option value="begins">{$lang->begins_with}</option>
                 <option value="contains">{$lang->username_contains}</option>
             </select>
             &nbsp;
             <input type="text" class="textbox" name="username" id="username" />
         </td>
      </tr>
      <tr>
         <td class="trow2" width="20%"><strong><label for="website">{$lang->search_website}</label></strong></td>
         <td class="trow2">
             <input type="text" class="textbox" name="website" id="website" />
         </td>
      </tr>
      

    2. Replace with:
      <tr>
         <td class="trow1" style="vertical-align: top;" width="20%"><strong><label for="username">{$lang->username}</label></strong></td>
         <td class="trow1">
             <select name="username_match">
                 <option value="begins">{$lang->begins_with}</option>
                 <option value="contains">{$lang->username_contains}</option>
             </select>
             &nbsp;
             <input type="text" class="textbox" name="username" id="username" />
         </td>
      </tr>
      {$customfields}
      <tr>
         <td class="trow2" width="20%"><strong><label for="website">{$lang->search_website}</label></strong></td>
         <td class="trow2">
             <input type="text" class="textbox" name="website" id="website" />
         </td>
      </tr>
      
      (or add the variable {$customfields} wherever you want)

Known limitations:
  • Multi value fields are not supported
  • Textareas are not supported (because I think it's pointless to search within a textarea)*
  • It's not a plugin*

*If you really need to search in textareas just ask for it and I will provide the code modification.

*There exist no hooks, so it's not possible to make it as a plugin. Of course you can copy the whole code of memberlist.php into a plugin file and use the memberlist_start hook, but this would be a crappy way...

If you are not able to do the hack yourself you can use the attached memberlist.php (wow, we are at MyBB 1.4.11 and this file wasn't modified since 2009-01-31 Big Grin ). But you still have to add the variable {$customfields} to the template.

License: GPL
Could i please have the text area search code
bump

I really need this
Perfect.
Works divinely.
Sounds like a good plugin. Good share Smile
I like it!

What would I have to include searching the custom field at the bottom of the member list page, instead of the default search box for website?
This stopped working for me when I updated to the latest MyBB. Now, when I went back in to implement it again, the first part to find and replace is a little different. Is it still going to work?
Hello.. Someone has the new version for Mybb 1.8?
(2016-01-30, 11:20 AM)gokufg Wrote: [ -> ]Hello.. Someone has the new version for Mybb 1.8?

Also a way to make this work:

"Textareas are not supported (because I think it's pointless to search within a textarea)*"
Bump, need to add one field to be searchable - select box - anybody? Wink
Pages: 1 2