Community Support Forums — WordPress® ( Users Helping Users ) — 2011-04-19T22:10:05-05:00 http://www.primothemes.com/forums/feed.php?f=4&t=1668 2011-04-19T22:10:05-05:00 http://www.primothemes.com/forums/viewtopic.php?t=1668&p=12968#p12968 <![CDATA[Re: Working with custom registration fields using PHP]]>

I'd probably write it like this, though

Code:
$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.

Statistics: Posted by Cristián Lávaque — April 19th, 2011, 10:10 pm


]]>
2011-04-19T16:45:03-05:00 http://www.primothemes.com/forums/viewtopic.php?t=1668&p=12950#p12950 <![CDATA[Re: Working with custom registration fields using PHP]]>
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:
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:
// $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:
//I used this to get the character length of the $user or value I am searching for.

    $length = strlen($user); 
 

Code:

    
//Add the character length to the query string

$value .= $length; 
 

Code:

    
// finish up the query string.

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

Code:

    
//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:

    
// 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:

    $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.

Statistics: Posted by dwbiz05 — April 19th, 2011, 4:45 pm


]]>
2011-04-19T16:05:03-05:00 http://www.primothemes.com/forums/viewtopic.php?t=1668&p=12946#p12946 <![CDATA[Re: Working with custom registration fields using PHP]]>
I'll let you know if I find something.

D.

Statistics: Posted by dwbiz05 — April 19th, 2011, 4:05 pm


]]>
2011-01-16T11:24:12-05:00 http://www.primothemes.com/forums/viewtopic.php?t=1668&p=5663#p5663 <![CDATA[Working with custom registration fields using PHP]]>
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:
$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?

Statistics: Posted by dwdutch — January 16th, 2011, 11:24 am


]]>