I have a piece of code that loops through a collection of fields. I want it to "skip" a field if the condition I set is true. My code is working for all other conditions except this one.
Let's say I have three fields "first name", "last name", and "username". I want it to skip a field if the field name is "username".
I thought this should work:
PHP Code:
if ( preg_match('/username/i', $field_name = bp_get_the_profile_field_name() ) ) {
global $profile_template, $field;
$field = $profile_template->next_field();
}
But instead it's getting rid of all three fields with "name" in it. I've tried all kinds of combinations as well. Any idea on the reg exp notation I need to use?
1. try to debug this, for example, by outputting the value of $field_name. make sure preg_match is actually evaluating what you expect it to be evaluating.
2. globals are bad practice. try to rewrite your code in such a way that you don't need them anymore.
1. try to debug this, for example, by outputting the value of $field_name. make sure preg_match is actually evaluating what you expect it to be evaluating.
2. globals are bad practice. try to rewrite your code in such a way that you don't need them anymore.
I really would want to avoid using a global here, but I have no choice. This is a Wordpress API that I'm customizing.
I "know" that the variable $field_name works because it works here:
PHP Code:
if ( preg_match('/Date(\s+)Of(\s+)Birth/i', $field_name = bp_get_the_profile_field_name() ) ) {
global $profile_template, $field;
$field = $profile_template->next_field();
}
where it looks for the field "Date Of Birth" and it skips to the next field...
I think the issue has to do with the regular expression if anything..
My sneaky suspicion is that something in the logic is wrong, but since I don't know what any of those method calls actually does, I can't be sure, but my spidey senses make me wonder if the next_field() is moving an array or result row pointer before returning the value, and actually giving you the next row, which might be empty, and therefore is returning null or false. But that's pure speculation on my part for now.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks