toobster Posted November 24, 2010 Share Posted November 24, 2010 Hey, I have a mysql table for my setups, it contains basically three fields: 'id','variable','value'. i want to create/name a variable from a result from the database and then from another field it should give the newly created variable its value The table is already filled with the variables i want to use, now i just want my index page to get the different variable out of the table with the value as its value. Is that even possible? so lets say i have a variable in my table called 'header_color' the value in that table row is 'FFF' i want my index to get all variable from the database (through a while??) with again its value as a value. This is my thought: $sql = mysql_query("SELECT * FROM setup"); while ($res = mysql_fetch_array($sql)){ $res[variable]; = $??my_variable??; $myvariable = $res[value]; } and so when i have my div for my header i want it to be <div style="background-color:#<?=$header_color?>"> resulting in <div style="background-color:#FFF> in my table under variable it has the name "header_color" and under value "FFF" maybe i need to create an array or something i have no idea. Hope you guys can help me. Quote Link to comment https://forums.phpfreaks.com/topic/219641-variable-from-table/ Share on other sites More sharing options...
trq Posted November 24, 2010 Share Posted November 24, 2010 You would be better of simply settling for a config array of sorts..... $sql = mysql_query("SELECT variable, value FROM setup"); $config = array(); while ($res = mysql_fetch_array($sql)) { $config[$res['variable']] = $res['value']; } then using.... <div style="background-color:#<?php echo $config['header_color'];?>"> Quote Link to comment https://forums.phpfreaks.com/topic/219641-variable-from-table/#findComment-1138780 Share on other sites More sharing options...
toobster Posted November 24, 2010 Author Share Posted November 24, 2010 kinda what i needed to know, thanks. I just need to know what the best way is, and i guess your answer and nobody else answering says it all. Quote Link to comment https://forums.phpfreaks.com/topic/219641-variable-from-table/#findComment-1138790 Share on other sites More sharing options...
trq Posted November 24, 2010 Share Posted November 24, 2010 Well, you don't want mysterious variables showing up in your scripts. It will make debugging very difficult. Its bad enough you have this $config array kicking around, but at least you'll be able to find where that comes from. A better method might be to create a registry object, but unless you know a bit of OOP, I'm not even going to go there. Quote Link to comment https://forums.phpfreaks.com/topic/219641-variable-from-table/#findComment-1138797 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.