tags:

views:

101

answers:

2

Can you read out the name of a PHP constant from a database and use it inside of a php variable, to display the value of the constant for use in a menu?

For example here's what I'm trying to accomplish

In SQL: select menu_name AS php_CONSTANT where menu_id=1 the value returned would be L_HOME which is the name of a CONSTANT in a php config page. The php config page looks like this define('L_HOME','Home'); and gets loaded before the database call. The php usage would be $db_returned_constant which has a value of L_HOME that came from the db call, then I would place this into a string such as $string = '<ul><li>' . $db_returned_constant . '</li></ul>' and thus return a string that looks like $string = '<ul><li><a href="#" onclick="path_from_db">Home</a></li></ul>'.

To sum up what I'm trying to do

  1. Load a config file based on the language preference
  2. query the db to return the menu name, which is the name of a CONSTANT in the config file loaded in step one, and also retrieve the menu_link which is used in the "onclick" event.
  3. Use a php variable to hold the name of the CONSTANT
  4. Place the variable into a string that gets echo'd out to create the menu displaying the value of the CONSTANT.

I hope this makes enough sense...is it even possible to use a constant like this?

Thanks.

+2  A: 

Sure, just retrieve the name of a constant into a string and then use constant.

Artefacto
Thank you for your help!
Ronedog
+2  A: 
define('L_HOME','Home');
$db_returned_constant = 'L_HOME'; // value actually retrieved from db
echo constant($db_returned_constant);

// will output 'Home'
MatW
This works perfect Thanks!
Ronedog