Page 1 of 1

Working with custom registration fields using PHP

PostPosted: January 16th, 2011, 11:24 am
by dwdutch
I'm experimenting with the custom registration fields.

I have created a field that contains checkboxes (for simplicity, let's say the field name is FAVORITE-FRUIT and the checkbox options are apple, orange and mango).

Using PHP, I present a page that lists all of the members and their individual preference choices for FAVORITE-FRUIT. Retrieving the information for a specific user was easy enough.

Code: Select all
$user = get_userdata($user_id);
$custom_fields = get_user_option('s2member_custom_fields', $user_id);
$fruitArray = (array) $custom_fields['favorite-fruit']
$fruitList = implode(',', $fruitArray)

Now, at the top of the page (above the list), I want to add a filter capability by presenting all of the checkbox options for FAVORITE-FRUIT so that someone can select a specific fruit THEN, after pressing a SUBMIT/FILTER button, see in the list only those users who have selected that same fruit.

I can't figure out how to retrieve the complete list of fruit options independent of a specific user who has selected all of them. Any suggestions?

Re: Working with custom registration fields using PHP

PostPosted: April 19th, 2011, 4:05 pm
by dwbiz05
s2Member saves all the custom data in a serialized array in the usermeta table. I'm having the same issue. I would love to be able to filter some settings and print the users who match those settings but I can't find a simple* solution to do this either.

I'll let you know if I find something.

D.

Re: Working with custom registration fields using PHP

PostPosted: April 19th, 2011, 4:45 pm
by dwbiz05
Ok, I'm not an expert, but this is how I solved this... I'm using the custom field "ref_id" to track referrals.

So here is how I search the serialized array. In a serialized array, you will see something like this for the field => value set up:
Code: Select all
s:6:"ref_id";s:1:"1";

the "s:" parts list the number of characters in the "" section. So for ref_id, there are six characters and for 1 obviously there is only 1 character. This is key for part of the following code.
Code: Select all
// $user comes from the function($user) variable
//I started the string at the opening " of the custom field name and ended it before the required character length of the value. 
//I also comment out the " and the ; to make sure it doesn't mess up the query.

$value = '\"ref_id\"\;s:'; 
 

Code: Select all
//I used this to get the character length of the $user or value I am searching for.

    $length = strlen($user); 
 

Code: Select all

    
//Add the character length to the query string

$value .= $length; 
 

Code: Select all

    
// finish up the query string.

$value .= ':\"'.$user.'\"'; 
 

Code: Select all

    
//Use the LIKE %% to search inside the serialized fields.

$sql = 'SELECT user_id FROM wp_usermeta WHERE meta_key = "wp_s2member_custom_fields" AND meta_value LIKE "%'.$value.'%"'; 
 

Code: Select all

    
// I'm just looking for a total but you could use the query to print out a list or whatever.

$total = mysql_num_rows(mysql_query($sql)); 
 

I'm sure there is a better way, but this is working for me right now!

This is the code without the comments:
Code: Select all

    $value 
= '\"ref_id\"\;s:';
    $length = strlen($user);
    $value .= $length;
    $value .= ':\"'.$user.'\"';
    $sql = 'SELECT user_id FROM wp_usermeta WHERE meta_key = "wp_s2member_custom_fields" AND meta_value LIKE "%'.$value.'%"';
    $total = mysql_num_rows(mysql_query($sql));
    echo $total;
 

D.

Re: Working with custom registration fields using PHP

PostPosted: April 19th, 2011, 10:10 pm
by Cristián Lávaque
Nice tip dwbiz05! Thanks for sharing it. :)

I'd probably write it like this, though

Code: Select all
$like = '\"ref_id\"\;s:' . strlen($user) . ':\"' .$user. '\"';
$query = 'SELECT user_id FROM wp_usermeta WHERE meta_key = "wp_s2member_custom_fields" AND meta_value LIKE "%' . $like . '%"';
echo mysql_num_rows(mysql_query($query));
 


but that's a personal preference, I guess.