Jump to content

[SOLVED] variables from mysql to echo page variable value on page


ched999uk

Recommended Posts

Hi everyone, I have a problem that is making me pull my hair out.

 

Here goes:

I have the following text in a mysql field '$City is in the County of  $County'

and within my php page I have $City = "Liverpool"  $County = "Merseyside"

 

What I want to happen is when the mysql field is echoed within the page it shows

'Liverpool is in the County of  Merseyside'

but it actually shows

'$City is in the County of  $County'

 

Any idea how I get the mysql variables to use the page variable values?

 

I hope that makes sense as it is driving me nuts.

 

Please help.......

Cheers Tom2008. Thanks but I cant do that because '$City is in the County of  $County' is the actual text in the mysql field and I need it to read the variable from the page and display the correct local page variables in place of the $City and $County.

 

This mysql field is used on lots of pages and is required to use the local variable values.

 

The actual mysql database tables are a more complex I am just using the above to try and simplify.

 

Thanks again for an amazingly quick reply.

 

 

Cheers Tom2008. Thanks but I cant do that because '$City is in the County of  $County' is the actual text in the mysql field and I need it to read the variable from the page and display the correct local page variables in place of the $City and $County.

 

This mysql field is used on lots of pages and is required to use the local variable values.

 

The actual mysql database tables are a more complex I am just using the above to try and simplify.

 

Thanks again for an amazingly quick reply.

so its litrally like  $City is in the County of  $County'  including the quotes?

 

if it is try this :

$try= '$City is in the County of  $County';

$try = str_replace ("$City", "'. $City . '", $try);

$try = str_replace ("$County", "'. $County . '", $try);

echo $try;

 

Long way of doing things but might get the job done i dont know

if it works you could make a function out of it:

 

funtion replacethings($string,$astr, $bstr)

{

$replaced ="'. " . $astr . " . '";

$replaced2 ="'. " . $bstr . " . '";

$string= str_replace ($astr, $replaced, $string);

$string = str_replace ($bstr, $replaced2, $string);

return $string

}

 

for example: $string = replacethings($oldstring,"$City","$County");

Cheers Tom2008. Thanks but I cant do that because '$City is in the County of  $County' is the actual text in the mysql field and I need it to read the variable from the page and display the correct local page variables in place of the $City and $County.

 

This mysql field is used on lots of pages and is required to use the local variable values.

 

The actual mysql database tables are a more complex I am just using the above to try and simplify.

 

Thanks again for an amazingly quick reply.

so its litrally like  '$City is in the County of  $County'  including the quotes?

 

It is literally just $City is in the County of  $County without the quotes in the mysql field.

The thing i posted above wont work then, And i cant edit it!

 

ive got it to work on my server, but your gunna have to adapt it to your needs:

 

 

<?

$City = "Liverpoole";

 

$County = "theotherthing";

//as if selecting from mysql database

$string = "$City is in the County of  $County";

 

echo $string;

 

?>

The thing i posted above wont work then, And i cant edit it!

 

ive got it to work on my server, but your gunna have to adapt it to your needs:

 

 

<?

$City = "Liverpoole";

 

$County = "theotherthing";

//as if selecting from mysql database

$string = "$City is in the County of  $County";

 

echo $string;

 

?>

 

Thanks very much I will have a play hopefully one of your answers will work. Thanks very much. I will report back.

Cheers Tom2008. Thanks but I cant do that because '$City is in the County of  $County' is the actual text in the mysql field and I need it to read the variable from the page and display the correct local page variables in place of the $City and $County.

 

This mysql field is used on lots of pages and is required to use the local variable values.

 

The actual mysql database tables are a more complex I am just using the above to try and simplify.

 

Thanks again for an amazingly quick reply.

so its litrally like  $City is in the County of  $County'  including the quotes?

 

if it is try this :

$try= '$City is in the County of  $County';

$try = str_replace ("$City", "'. $City . '", $try);

$try = str_replace ("$County", "'. $County . '", $try);

echo $try;

 

Long way of doing things but might get the job done i dont know

if it works you could make a function out of it:

 

funtion replacethings($string,$astr, $bstr)

{

$replaced ="'. " . $astr . " . '";

$replaced2 ="'. " . $bstr . " . '";

$string= str_replace ($astr, $replaced, $string);

$string = str_replace ($bstr, $replaced2, $string);

return $string

}

 

for example: $string = replacethings($oldstring,"$City","$County");

I managed to sort it in the end by using : $your_mysql_string = str_replace(array("\$City","\$County"),array($City,$County),$your_mysql_string);

 

So it needed double quotes and a backslash!!!!!

 

Thanks very much for your help.

I am now going to watch TV and have a Cool Beer.

Thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.