If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Variable help

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

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.

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.

In the form:

PHP Code:

echo "<input type='checkbox' name='foo[$user]' value='1' />";

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

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

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