PriMoThemes — now s2Member® (official notice)

This is now a very OLD forum system. It's in READ-ONLY mode.
All community interaction now occurs at WP Sharks™. See: new forums @ WP Sharks™

Working with custom registration fields using PHP

s2Member Plugin. A Membership plugin for WordPress®.

Working with custom registration fields using PHP

Postby dwdutch » January 16th, 2011, 11:24 am

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?
User avatar
dwdutch
Registered User
Registered User
 
Posts: 24
Joined: January 16, 2011

Re: Working with custom registration fields using PHP

Postby dwbiz05 » April 19th, 2011, 4:05 pm

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.
User avatar
dwbiz05
Registered User
Registered User
 
Posts: 11
Joined: March 17, 2011

Re: Working with custom registration fields using PHP

Postby dwbiz05 » April 19th, 2011, 4:45 pm

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.
Last edited by Cristián Lávaque on April 19th, 2011, 9:45 pm, edited 1 time in total.
Reason: Improve code readability. http://www.primothemes.com/forums/viewtopic.php?f=36&t=2780
User avatar
dwbiz05
Registered User
Registered User
 
Posts: 11
Joined: March 17, 2011

Re: Working with custom registration fields using PHP

Postby Cristián Lávaque » April 19th, 2011, 10:10 pm

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.
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010


Return to s2Member Plugin

Who is online

Users browsing this forum: Google [Bot] and 3 guests

cron