View Full Version : Print Variable From Database

01-02-2006, 09:47 PM
Hi all,

i have a sql database and i want to add 100% customisable phrases to my product.

but now n then there needs to be a variable :)

e.g. atm i have:

print "Welcome To $sitename's Support Helpdesk";

this displays:
Welcome To FLCup's Support Helpdesk

now, to make that customisable i would need

print "$phrase[headertext]"; // of corse i'll have a sql query i aint that dumb.

so to the sql database i go and insert "$sitename's Support Helpdesk" into a field on the phrases database.

i refeshed my site and now it says
$sitename's Support Helpdesk // obviously not taking $sitename as a variable but as text

.: so what am i asking? :.

i am askin if there is a way to have things like $sitename in a field in the database and a php script notice that and print it as a variable and not text.

so that $phrase[headertext] prints: Welcome To FLCup's Support Helpdesk not Welcome To $sitename's Support Helpdesk

any help would be great,


p.s a 2nd example if needed:

print "Welcome to $sitename's support center, choose an option below";
prints: Welcome to SupportCenter's support center, choose an option below

how can i have $phrase[welcome] not say
Welcome to $sitename's support center, choose an option below

Velox Letum
01-02-2006, 10:05 PM
Instead, you could insert [sitename]'s Support Helpdesk into the database, then...

$sitename = 'SupportCenter'; // I suppose this would be set by a SQL query result.
$phrase['headertext'] = str_replace('[sitename]', $sitename, $phrase['headertext']);
echo $phrase['headertext']; // Prints 'SupportCenter's Support Helpdesk' (without quotes).

This is a relatively secure solution to this problem...using eval() would be a security nightmare.

01-02-2006, 10:07 PM
yes, when you insert the phrase, you need to escape the string, assuming it isn't showing up as the real site name becuase you use single quotes on the query, so you would need to do something like

headertext = `Welcome to '.$sitename.'\'s HelpDesk!`

or the same methed in the variable your using on headertext, if you are.

And its best when you call the row to use: $phrase['headertext']

Was writing mine while Velox already was apparently, but this is another solution, to add the name before you call it back, so the str_replace() wouldn't be nessessary if you use this.

Velox Letum
01-02-2006, 10:19 PM
However, if you did use Element's method, the phrase wouldn't be updated if you changed the name. If you want to go the DB method, my way would be a good way. Another way might to be to just make a large language file with all the phrases, that'd make it pretty easy.