Reply
how to parse PHP vars stored in Db?
Old 12-28-2007, 12:44 PM how to parse PHP vars stored in Db?
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,169
Name: Dan
Location: Swindon
hey

okay i have a games script which i am re-vamping and i want to be able to control all the messages shown to the user through the databse so i been doing through all the scripts and on my way been replacing the comments and stuff with my function

get_msg(ID)

and i then have my database with the messgae:

You beat {$r['username']}!!<br />
for example and my function gets it from the db

My Function:
PHP Code:
function get_msg($id)
{
global 
$db$r;
$txt_res $db->query("SELECT * FROM msg_texts WHERE text_id='$id'");
$txt_info $db->fetch_row($txt_res);
return 
$txt_info['text'];

but i just noticed that the PHP in the messages aint being parsed.

do what am i doing wrong?

i thought adding $r to the globals would fix but obviouslt not...

Thanks TP for working answer.
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
When You Register, These Ads Go Away!
Old 12-28-2007, 01:04 PM Re: how to parse PHP vars stored in Db?
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
When you store the code in a DB, you need to eval() it if you want the PHP engine to parse it, otherwise, it's just a text string.

PHP Code:
function get_msg($id)
{
global 
$db$r;
$txt_res $db->query("SELECT * FROM msg_texts WHERE text_id='$id'");
$txt_info $db->fetch_row($txt_res);
$text=$txt_info['text'];
eval(
"\$outText=$text");
return 
$outText;

This should do it.
PHP will interpret what is passed into the eval function like a single file, so don't forget to escape every real variable you want to get back.
</span></span>
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 12-28-2007, 03:00 PM Re: how to parse PHP vars stored in Db?
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,169
Name: Dan
Location: Swindon
ok so just to clarify, i just add that eval function and as long as i have all my variables to be parsed saved as {$val} and any ' or " are escaped im okay?

Da
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 12-28-2007, 03:05 PM Re: how to parse PHP vars stored in Db?
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,169
Name: Dan
Location: Swindon
okay tried this:

PHP Code:
function get_msg($id)
{
global 
$db$r;
$txt_res $db->query("SELECT * FROM msg_texts WHERE text_id='$id'");
$txt_info $db->fetch_row($txt_res);
 
$txt_info['text'] = eval($txt_info['text']);  
  
return 
$txt_info['text'];

and got this error:

Code:
Parse error: syntax error, unexpected T_STRING in /home/danmike/public_html/global_func.php(509) : eval()'d code on line 1
so im guessing im doing something wrong can you correct?

Thanks,
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 12-28-2007, 03:59 PM Re: how to parse PHP vars stored in Db?
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
yep...
Try:
PHP Code:
$txt_info['text'] = eval("\$return={$txt_info['text']}"); 
Remember, you give eval a string, and it interprets it like a file.
You need to tell him where to store the result of the eval.
Here, in a $return variable. And to make that variable seen like a real PHP variable definition, you need to escape the $, to be sure it don't go translated to something before the eval call.
</span></span>
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 12-28-2007, 04:19 PM Re: how to parse PHP vars stored in Db?
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,169
Name: Dan
Location: Swindon
i got it in the end with a little help from a friend

thanks a lot

Dan
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to how to parse PHP vars stored in Db?
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13474 seconds with 12 queries