Hi I'm having trouble using variables that vary due to a loop..
I have a loop that creates checkboxes with the names checkbox_name where name is read from a database.(this part works successfully)
I want to later echo all names from my database plus the result of it's checkbox.
E.g. I have checkbox_lilfellabob and checkbox_bill - I want to echo lilfellabob and the result of checkbox_lilfellabob then echo bill and the result of checkbox_bill
I've tried using this in my loop:
$username = $row_3['username']; (this part sucessfully gets the username)
${"checkbox_$username"} = $checkbox_username;
so that I can use $checkbox_username in the loop which equals checkbox_namefromdatabase for that cycle. But I can't seem to request it, $_REQUEST['$checkbox_username'] doesn't give me that checkbox's result.
Sorry if this is hard to understand I'm not sure the best way to explain it..
I think this is the same problem in different works though http://bytes.com/topic/php/answers/5...-constant-name
only I can't understand how he solved it.
Afraid my brain isn't quite up to following all that, but one thing I'll throw out there for you to consider is that you can use an array notation for your form elements, and they will create sub-arrays withing $_REQUEST, $_POST, and/or $_GET.
Then in your form-handler, you would simply reference $_REQUEST['foo'][$username] for that form element.
"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
The reason I couldn't join them like thay (''checkbox_" . $username) is because it wouldn't let me request it later.
The array variables works 100%, thanks a million
I agree that the array version is the better method, but if you find a similar situation where an array is not available,(working with someone else's code, perhaps) this might help.
PHP Code:
$username = $row_3['username']; (this part sucessfully gets the username)
$checkbox = 'checkbox_';
$newvar = $checkbox.$username;
echo $$newvar;
cheers, how would you go about requesting that though?
I just tried
$output_checkbox = $_REQUEST['$checkbox.$username'];
and
$output_checkbox = $checkbox.$username;
$_REQUEST[$output_checkbox]
but they don't return the checkbox value..
Remember that variables are not interpolated within single quotes. Besides, since you are just concatenating two variables, there is no need for any quotes at all.
PHP Code:
$output_checkbox = $_REQUEST[$checkbox . $username];
// you could use double quotes, but then you would not concatenate:
$output_checkbox = $_REQUEST["$checkbox$username"];
"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
Now you can loop through $search via foreach(), or use any of the dozens of array-friendly PHP functions to manipulate it, e.g.:
PHP Code:
$found = false;
if(count($search)) {
$regexpWords = array();
foreach($search as $searchWord) {
$regexpWords[] = preg_quote($searchWord);
}
$regexp = '/\b(' . implode('|', $regexpWords) . ')\b/i';
$found = preg_match($regexp, $stringToSearch);
}
if($found) {
// do whatever you want if any of the words is found
}
"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
$search[] = $row['checkbox']; #create an array of checkbox values from db
$found = false; #start out not having found the word
if(count($search)) { #is this if search isn't empty?
$regexpWords = array(); #make an array to deal with special characters
foreach($search as $searchWord) { #loop through the search array
$regexpWords[] = preg_quote($searchWord); #put the search array into regexpWords with \'s where special chars appear
}
$regexp = '/\b(' . implode('|', $regexpWords) . ')\b/i'; #not sure what this does..?
$found = preg_match($regexp, $stringToSearch); #searching for the word (but I haven't seen this method before)
}
if($found) {
// do whatever you want if any of the words is found
}
The goal was to build up a PCRE regular expression that would look something like:
Code:
'/\b(word1|word2|word3|et cetera)\b/i'
The "/" is the pattern delimiter. The "i" that follows says to make the pattern case-insensitive (omit it if you want it to be case-sensitive). The "\b" near each end is an assertion saying that we want the word to match only between word boundaries. The parentheses will then define a sub-pattern, which will consist of a list of pipe-delimited words/phrases from our array (via the implode()).
Therefore, if my calculations are correct, the preg_match will match if any of those words appears in the string as a whole word (i.e. if 'foo' is one of the search words, it will match "Abc Foo Xyz" but not "Abc Foobar Xyz".
"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
Ok sweet, that's working. Thanks again.
Last question (for now at least :P)..
I had it so that names would get underlined.. but when I search the string for <u>name</u> it doesn't include the underline tags (I think instead, it uses them).
my code
PHP Code:
//The string is in a db but say it contains 'the following has some underlined names <u>user_x</u> and also <u>user_y</u>' //I'll call it $row['string']
//I check if the checkbox is checked, if so it will add that name to a string ($show_posts_with_username)
//Then I want to run the search if(preg_match($search_terms, $row['string'])) { //do stuff }
Instead of searching the $row['string'] for a string of:
'<u>' . $user_x . '</u>|@<u>' . $user_y . '</u>'
I get this:
Warning: preg_match() [function.preg-match]: Unknown modifier '>' in /dir/index.php on line 318
If I echo $search_terms it outputs:
Code:
/(@user_x|@user_y) /i
with user_x and user_y underlined
I think instead of reading the underline tags as text, it's reading them as code..
I can just not use underlining but I figure there should be a simple solution..?
Last edited by lilfellabob; 12-26-2010 at 08:30 AM.
Bookmarks