Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: variable problem

  1. #1
    Join Date
    Jan 2007
    Posts
    58
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default variable problem

    hello, i want to make a string into a variable ,lets say i have a form, in $name (text field name) i typed john, so, now $name = "john".

    How can i turn it into a variable? i think it should be smth like this $$name but doesn't work, it says: Incorrect syntax near 'j'.

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    If I understand you correctly, you want to make john (which is the string) into the variable $name. You have just done so, but here is another example:

    Code:
    <?php
    
    $str = "String Here";
    
    echo $str; //displays String Here
    
    $var = $str;
    
    echo $var; //displays String Here
    ?>
    If this is not what you mean, could you be a little more specific.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Jan 2007
    Posts
    58
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    hello, thanks for reply, but no, i want to create a variable $john or if i entered bobby on the text field, it should be $bobby, do you understand now?

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    You should be able to do that by using something like this:

    Code:
    <?php
    
    $var = "John";
    
    $$var = "variable";
    
    
    echo $John;
    ?>
    I have tested the above code and it works fine.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Jan 2007
    Posts
    58
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    yes it works, but is not what i want, let me explain better, i got 2 .php files, on the file one.php i got a form with a single text field named "name", all the names will be stored on mssql, so if you enter john, $name = trim($_POST[name]);
    $name = "john", if you enter rocky, $name = "rocky".

    Now, i have this variable $name with a string value.

    In the second php file named two.php, lets say i have 2 arrays, $john = array ("0","1"); and $rocky = array ("5","6");

    Now what i need is to get the first value from those arrays, if $name = "john"; i want to save on mssql the first value of $john array.

    So, i got smth like this:

    function add_name($name)
    include 'two.php';

    mssql_query("UPDATE table SET names = $name number = $name[0]

    now here we got a problem, because $name = "john";

    its missing the "$", i dont know if you understand, i need to make it look like this $$name[0]. I hope i finally made myself clear, sorry if not because my english sucks a bit... Thanks again.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    PHP is odd about array syntax sometimes, you may need to put it in a separate statement.

    More importantly, though, don't do this. If you want this kind of setup, use an associative array:
    Code:
    $namevals = array(
      'john' => array('0', '1'),
      'rocky' => array('5', '6')
    );
    Then:
    Code:
    $namevals[$name];
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Jan 2007
    Posts
    58
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    but if i do it $namesvals[$name][0] it gives me an error... and if i do it $namevals[$name] it says: Invalid column name 'Array'.
    Last edited by nicksalad; 06-09-2007 at 12:32 AM.

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    if i do it $namesvals[$name][0] it gives me an error...
    What error?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jan 2007
    Posts
    58
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    What error?
    Incorrect syntax near '0'.

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Try:
    Code:
    $v = $namesvals[$name];
    echo $v[0];
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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