www.webdeveloper.com
Results 1 to 15 of 15

Thread: Variable help

  1. #1
    Join Date
    Nov 2010
    Posts
    57

    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

    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.

    Any help appreciated,

    cheers,

    lilfellabob

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,388
    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

    eBookworm.us

  3. #3
    Join Date
    Jul 2007
    Location
    Wisconsin
    Posts
    468
    If I'm understanding this right... You have the username from the db row:

    PHP Code:
    $username $row_3['username'];
    // $username = "lilfellabob"; 
    and you want to use that to create a new variable that contains the first:

    PHP Code:
    $checkbox "checkbox_" $username;
    //$checkbox = "checkbox_lilfellabob"; 

  4. #4
    Join Date
    Nov 2010
    Posts
    57
    @notdog haha yeah fair enough, it was kinda hard to explain. The array style sounds much simpler and should work how I want, thanks alot mate!

    @OctoberWind Yeah thats true, not sure why I didn't do that.. I have a look

    cheers

  5. #5
    Join Date
    Nov 2010
    Posts
    57
    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

  6. #6
    Join Date
    Oct 2007
    Posts
    371
    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

  7. #7
    Join Date
    Nov 2010
    Posts
    57
    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..

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,388
    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

    eBookworm.us

  9. #9
    Join Date
    Oct 2007
    Posts
    371
    $output_checkbox = $_REQUEST[$$newvar];

  10. #10
    Join Date
    Nov 2010
    Posts
    57
    thanks for all the replies
    I have another problem..
    I'm trying to make a function where if people check checkboxes, their values add to a string.
    Lets say I get the following string:
    Code:
    'checkbox1, checkbox2, checkbox3'
    (I added COMMASPACE after each one too)
    This part works. I then want to search another string for any of the values seperated via the comma (and space).. So say I'm searching in the string:
    Code:
    'If this string contains checkbox1, checkbox2 or checkbox3 I'll return true'
    if it contains any of checkbox1 checkbox2 or checkbox3 it will return true.
    I don't know if how to string search for 'words'.. if I can do that I think it will be the best.

    If I can't do that I was thinking I'll separate the words giving each it's own variable (via a while loop).. something like
    Code:
    string_$i = $row['checkbox']
    i++;
    I'm reading the checkbox values from a database (this is an addon to what I was doing at the start of this thread)

    Thanks again, you guys are awesome

  11. #11
    Join Date
    Oct 2007
    Posts
    371
    Someone will almost certainly correct me, but here's a quick and dirty method: (if I get the syntax right)
    PHP Code:
    $needle=('checkbox1, |checkbox2, |checkbox3, ');
    if (
    ereg($needle,$string)){
    //do whatever


  12. #12
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,388
    I think you need to get more array-oriented.

    Instead of this:
    PHP Code:
    string_$i $row['checkbox']; 
    Populate an array:
    PHP Code:
    $search[] = $row['checkbox']; 
    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

    eBookworm.us

  13. #13
    Join Date
    Nov 2010
    Posts
    57
    ok thanks, I understand most of this (I think...)

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

    I'm going to have a read through this http://www.webcheatsheet.com/php/reg...xpressions.php and hopefully then I'll understand enough to use it.
    Thanks again

  14. #14
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,388
    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

    eBookworm.us

  15. #15
    Join Date
    Nov 2010
    Posts
    57
    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)

    if($_REQUEST['checkbox_'][$username] == "on"){$show_posts_with_username $show_posts_with_username "|@<u>" $username "</u>";}

    //note: the above will only run if more than one checkbox is checked else it will not use the '|'

    //Then I put that string into something to search with

    $search_terms "/(" $show_posts_with_username ") /i";

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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles