PDA

View Full Version : variable problem



nicksalad
06-08-2007, 09:30 PM
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'.

thetestingsite
06-08-2007, 09:38 PM
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:



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

nicksalad
06-08-2007, 09:41 PM
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?

thetestingsite
06-08-2007, 09:54 PM
You should be able to do that by using something like this:



<?php

$var = "John";

$$var = "variable";


echo $John;
?>


I have tested the above code and it works fine.

Hope this helps.

nicksalad
06-08-2007, 10:21 PM
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.

Twey
06-08-2007, 11:02 PM
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:
$namevals = array(
'john' => array('0', '1'),
'rocky' => array('5', '6')
);Then:
$namevals[$name];

nicksalad
06-08-2007, 11:24 PM
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'.

Twey
06-08-2007, 11:36 PM
if i do it $namesvals[$name][0] it gives me an error...What error?

nicksalad
06-08-2007, 11:41 PM
What error?

Incorrect syntax near '0'.

Twey
06-08-2007, 11:55 PM
Try:
$v = $namesvals[$name];
echo $v[0];

nicksalad
06-09-2007, 12:02 AM
Try:
$v = $namesvals[$name];
echo $v[0];

Amazing!!! now it works great! you rock dude.

Thanks you all for this great forum and comunity.

Im taking some classes of PHP5, i will try to retrieve to comunity as much as i can.