Forum Moderators: coopster

Message Too Old, No Replies

PHP $Variable nested in MySQL field help

         

gargoyle1

8:24 pm on Jun 16, 2005 (gmt 0)

10+ Year Member



OK so I have a field in my database that has a variable in it '$variable'

When I run the script using php it is displayed as the text '$variable' instead of the assigned value.

Is there a way to ensure that it will be parsed as a php variable and take the value assigned to it earlier in the code?

[edited by: gargoyle1 at 8:38 pm (utc) on June 16, 2005]

bennymack

8:31 pm on Jun 16, 2005 (gmt 0)

10+ Year Member



What you need is the double dollar sign syntax.

For example:

$foo = "bar";
$bar = "baz";

print $$foo;

Output: baz

AFAIK. Haven't played with PHP in a while. Please correct me if I'm wrong.

coopster

8:42 pm on Jun 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, gargoyle1.

So you have a database column that you are retrieving and assigning it to a variable, something like this ...


$dog = 'Bingo';
while ($row = fetching data from database result set) {
$my_text = $row['my_text'];
}

And in the variable $my_text you now have a string that maybe looks something like this ... 'My dog has a name and that name is $dog'

To parse the variable "$dog" within that text string, you have some options, eval() being one of them, but the better option is probably str_replace.

print str_replace('$dog', $dog, $my_text);

gargoyle1

8:48 pm on Jun 16, 2005 (gmt 0)

10+ Year Member



Ok, I'm not sure that that is the solution as i have tried the double dollarsign variable a few different ways with no success. I could be doing it wrong though.

Here is a rough Idea of what I'm dealing with:
<?
$variable = 'cheeky monkey';

$example='a whole bunch of html formatting and text and this one $variable';

echo $example
?>
//Outputs: 'a whole bunch of html formatting and text and this one cheeky monkey'

This code works no problem. Its when I do this:

<?
MySQL->go get me an array
etc
$example=$row["example"]
(you get the idea here, the $example variable is stored as I had it above, but now it's in the database)

$variable='cheeky monkey';

echo $example
?>

//that THIS happens:
'a whole bunch of html formatting and text and this one $example'

I'm not sure where to put the exta '$' if that is the problem.

mcibor

8:58 pm on Jun 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One way to do that is to use function eval. Double dollar won't do any good

<?
MySQL->go get me an array
etc
$example=$row["example"]
(you get the idea here, the $example variable is stored as I had it above, but now it's in the database)

$variable='cheeky monkey';
eval("\$example = \"$example\";"); //$example = "a whole text and this $variable";
echo $example
?>

However your previous code with
$example='a whole bunch of html formatting and text and this one $variable';

Will not work really. If you use single quotes ' ', then the $vars aren't parsed. You need to use double quotes: " ".

Best regards
Michal Cibor

Look at eval() here: [php.net...]
Or use the coops function. And yes, you can replace array with array

[edited by: mcibor at 9:01 pm (utc) on June 16, 2005]

gargoyle1

8:59 pm on Jun 16, 2005 (gmt 0)

10+ Year Member



Thanks for the welcome Coopster,
i've been browsing your forums for a while now, and surprisingly, this is the first time I couldn't find the answer.

Ok so let me get this straight, I would do this:

print str_replace('$variable', $variable, $example);

or if I had several things to replace, I could do this:

$find = array( bunch of variables )
$replace = array (bunch of variables2 )

print str_replace($find, $replace, $example);

coopster

9:02 pm on Jun 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Give 'er a whirl, let us know ;) str_replace [php.net].

gargoyle1

9:07 pm on Jun 16, 2005 (gmt 0)

10+ Year Member



Well right after I posted that I tried it out.
It works!

Thanks everyone for all your help and prompt responses.

bennymack

10:18 pm on Jun 16, 2005 (gmt 0)

10+ Year Member



Oops I was way off. Guess I misunderstood the question. mcibor knows the way!