Variable from table??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

thejoker
Forum Newbie
Posts: 8
Joined: Tue Nov 23, 2010 3:17 pm

Variable from table??

Post by thejoker »

Hey,

I have a mysql table for my setups, it contains basically three fields: 'id','variable','value'.

The table is already filled with the variables i want to use for my website, 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:

Code: Select all

$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.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Variable from table??

Post by Pazuzu156 »

Did you connect to the host of your sql tables and dud u connect to the database? And the code you posted also looks incorrect.

If you are going to connect it, you would do it like so:

Code: Select all

<?php

$connect = mysql_connect("localhost","root","") or die ("Cannot connect to host");
mysql_select_db("database_name") or die ("Cannot select database");
$queryget = mysql_query("SELECT * FROM table_name") or die ("Invalid Query");

?>
See if that helps. There is also a way to connect them through an array.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
thejoker
Forum Newbie
Posts: 8
Joined: Tue Nov 23, 2010 3:17 pm

Re: Variable from table??

Post by thejoker »

yeah thats not the isssue at alll
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Variable from table??

Post by Pazuzu156 »

I see, if you post the html inside the php form, all you will need is the variable. Say $gbcolor = "#EEEEEE"; then when making the div, just do it like so: <div style='background-color:$bgcolor;'>

see if that helps
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
thejoker
Forum Newbie
Posts: 8
Joined: Tue Nov 23, 2010 3:17 pm

Re: Variable from table??

Post by thejoker »

i got that to, the problem is as i described, 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
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Variable from table??

Post by Pazuzu156 »

As being fairly new to sql, your question is beyond my knowledge. With the id, do you want the values to show by the id? I will toy around with this one and see if I can come up with any possibilities as I study your question more.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
thejoker
Forum Newbie
Posts: 8
Joined: Tue Nov 23, 2010 3:17 pm

Re: Variable from table??

Post by thejoker »

yeah okay, thanks for thinking with me thoug, you need to think a little more extended thoug, this is not a basic connection assigning a variable manually, it's that i wamt the variables to be created automatically and there values signed automatically
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Variable from table??

Post by Pazuzu156 »

if you want them done automatically, do you mean by a user inputing the values into a form and it doing the change?
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
thejoker
Forum Newbie
Posts: 8
Joined: Tue Nov 23, 2010 3:17 pm

Re: Variable from table??

Post by thejoker »

no, automatically from an already existing list from my database
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Variable from table??

Post by Pazuzu156 »

i get what you're saying. Could you export the database for your table thing, and ill work on a php code for it.

E-Mail: kklein_webmaster@jae-entertainment.we.bs
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
thejoker
Forum Newbie
Posts: 8
Joined: Tue Nov 23, 2010 3:17 pm

Re: Variable from table??

Post by thejoker »

Sure here it is:
CREATE TABLE IF NOT EXISTS `setup` (
`id` tinyint(3) unsigned NOT NULL auto_increment,
`setup` varchar(255) collate latin1_general_ci NOT NULL default '000000',
`variable` varchar(255) collate latin1_general_ci NOT NULL default '000000',
`value` varchar(255) collate latin1_general_ci NOT NULL default '000000',
`type` varchar(255) collate latin1_general_ci NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=78 ;

--
-- Dumping data for table `setup`
--

INSERT INTO `setup` (`id`, `setup`, `variable`, `value`, `type`) VALUES
(63, 'Website Header Background Color', 'header_bg', '4A92FF', 'color'),
(64, 'Website Header Font Color', 'header_font', '41FF3B', 'color'),
(65, 'Website Menu Background Color', 'menu_bg', 'FFF945', 'color'),
(66, 'Website Menu Font Color', 'menu_font', '44FF00', 'color'),
(67, 'Website Main Page Background Color', 'main_bg', '4AB1FF', 'color'),
(68, 'Website Main Page Font Color', 'main_font', 'A3FF2B', 'color'),
(69, 'Website Footer Background Color', 'footer_bg', '54AAFF', 'color'),
(70, 'Website Footer Font Color', 'footer_font', '3636FF', 'color'),
(71, 'Header Height', 'header_height', '015', 'height'),
(72, 'Menu Height', 'menu_height', '150', 'height'),
(73, 'Footer Height', 'footer_height', '150', 'height'),
(74, 'Header Image Size', 'img_size', '80', 'size'),
(75, 'Header Image Left', 'img_1', '_header1.png', 'img'),
(77, 'Header Image Right', 'img_2', '_header2.png', 'img');
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Variable from table??

Post by Pazuzu156 »

Ok, i'll get on it and show you what i get from it
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
thejoker
Forum Newbie
Posts: 8
Joined: Tue Nov 23, 2010 3:17 pm

Re: Variable from table??

Post by thejoker »

cool thanks it would be awesome if i can make this work, im so curious if it even can be done.

I have been looking for quite a while for a solution, but haven't found anything anywhere, not even anyone attempting basically.

Anyway if you have any further questions let me know
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Variable from table??

Post by Pazuzu156 »

Yes, is there a way i can get those images u were using as well, that way when i make it nothing messes up in the design.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
thejoker
Forum Newbie
Posts: 8
Joined: Tue Nov 23, 2010 3:17 pm

Re: Variable from table??

Post by thejoker »

those arent actual images yet, just 5 by 5 pnq of any color
Post Reply