Jim R Posted April 20, 2011 Share Posted April 20, 2011 Here is where I'm starting: SELECT * FROM wp_usermeta WHERE meta_key = 'wp_1_s2member_custom_fields' AND meta_value LIKE '%3%' As the PHP produces the page, I'm going to have 15 different options coming from the meta_value LIKE part. I'm assuming I can set up my 15 different DIVs, each with their own IF statement involving the meta_value. Right? I'm not going to have to have 15 different queries, am I? I assume it would start like this: $query = 'SELECT * FROM wp_usermeta WHERE meta_key = "wp_1_s2member_custom_fields"'; $results = mysql_query($query); while($line = mysql_fetch_assoc($results)) But from there, I don't know if it's possible to deal with partial data with IF statements. Quote Link to comment https://forums.phpfreaks.com/topic/234298-need-help-with-a-querying-a-partial-column/ Share on other sites More sharing options...
Muddy_Funster Posted April 20, 2011 Share Posted April 20, 2011 You only need the one query, and you shouldn't use SELECT * - name the field(s) that you want to return explicitly. what you want can be done using if and substr. see http://php.net/manual/en/function.substr.php for info about substr use Quote Link to comment https://forums.phpfreaks.com/topic/234298-need-help-with-a-querying-a-partial-column/#findComment-1204216 Share on other sites More sharing options...
btherl Posted April 20, 2011 Share Posted April 20, 2011 Yes your one query should be fine. You can make an "if" using $line['meta_value'] inside the loop. If you want the results in a particular order you can use an "ORDER BY" clause in your SQL query. Quote Link to comment https://forums.phpfreaks.com/topic/234298-need-help-with-a-querying-a-partial-column/#findComment-1204256 Share on other sites More sharing options...
Jim R Posted April 21, 2011 Author Share Posted April 21, 2011 Yes your one query should be fine. You can make an "if" using $line['meta_value'] inside the loop. If you want the results in a particular order you can use an "ORDER BY" clause in your SQL query. I haven't read what Muddy linked yet, but it might answer this question. Does the *if*....$line['meta_value'] work if I'm just trying to get part of the data in that field? Here is the raw data: a:1:{s:6:"county";s:1:"4";} From that, I'm going to look for "county" and then figure out if the "#" at the end is 1,2,3,4, or 5. Quote Link to comment https://forums.phpfreaks.com/topic/234298-need-help-with-a-querying-a-partial-column/#findComment-1204318 Share on other sites More sharing options...
btherl Posted April 21, 2011 Share Posted April 21, 2011 That looks like serialized data to me. In which case you can do this: $meta_value = unserialize($line['meta_value']); if ($meta_value['county'] == '4') { ... } Quote Link to comment https://forums.phpfreaks.com/topic/234298-need-help-with-a-querying-a-partial-column/#findComment-1204325 Share on other sites More sharing options...
Jim R Posted April 22, 2011 Author Share Posted April 22, 2011 Ok...I'm trying to get it to count the number of relevant items, display the User ID's then show the total number that match. $query = 'SELECT * FROM wp_usermeta WHERE meta_key = "wp_1_s2member_custom_fields"'; $results = mysql_query($query); while($line = mysql_fetch_assoc($results)) { $meta_value = unserialize($line['meta_value']); if ($meta_value['county'] == '1' || $meta_value['county'] == '2') { $count1=0; foreach ($line as $user) { $count1=$count1+1; echo $user['user_id'] . '<br> '; } echo $count1; } } That code produced: 1 4 w a 41 4 w a 4 It should show: 439 440 2 Quote Link to comment https://forums.phpfreaks.com/topic/234298-need-help-with-a-querying-a-partial-column/#findComment-1204835 Share on other sites More sharing options...
btherl Posted April 27, 2011 Share Posted April 27, 2011 Do you want to count the number of entries with county = 1 or county = 2 and get a total for each user id? Quote Link to comment https://forums.phpfreaks.com/topic/234298-need-help-with-a-querying-a-partial-column/#findComment-1206680 Share on other sites More sharing options...
Jim R Posted April 27, 2011 Author Share Posted April 27, 2011 Hey, thanks for replying. This sort of flowed into a new problem. http://www.phpfreaks.com/forums/index.php?topic=331174.0 Quote Link to comment https://forums.phpfreaks.com/topic/234298-need-help-with-a-querying-a-partial-column/#findComment-1206698 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.