database driven stylesheet

They have: 426 posts

Joined: Feb 2005

I am trying to create a database driven stylesheet for easy online editing through forms, but im not sure how to design the database.

I had the idea that each part of the html has either an id or class, this id or class () can become the primary key, then having various attributes ina few rows.

link this;

create table styles {
primary key class varchar(30),
background text
border text
width int(20)
height int(20)
}
'

think that is the right sql, but you get the idea?

If anyone has done this before would like to here about it.

Shirthead's picture

He has: 58 posts

Joined: Jun 2006

sounds quite server intensive. compared with a cached style sheet. Why not physically write over the file instead?

They have: 426 posts

Joined: Feb 2005

the idea is that anyone can edit it through straight forward forms, where some parts have tag where they can only choose certain words...etc does it make sense.

Shirthead's picture

He has: 58 posts

Joined: Jun 2006

Think I understand. Is the idea that every user gets their own style set-up, or that any user can change the overall style?

They have: 426 posts

Joined: Feb 2005

yes each user can create a style. But they have their own url as well so this will be the key to which styles to load up.

example.

user1.mysite.com user1 will be the PK for the styles to load up

user2.mysite.com will have a different stylesheet to load up.

Somehow i need the users table to link in with the styles table.

Shirthead's picture

He has: 58 posts

Joined: Jun 2006

OK - understand now. didn't quite grasp it before. It could still be done with writing to files, but the advantages are greatly diminished if each user has their of style, so I'll assume the database approach.

I'd probably still have an basic style-sheet with anything in it that applies to all users - for instance if you are using CSS for common layout.

In addition to that I'd probably grab the style information and save it to a cookie/session - only if that cookie/session is not already set (and therefor clearning it when they edit the style information). This would reduce the number of hits on the database.

For the style information itself, what you have is not far off. Add a user_id to that table and you will have the basic functionality.

If you are offering options - rather than allowing users to actually type in the values you might want another couple of tables: 1 to list the classes and 1 to list the options within each of those classes.

Does that make any sense?

They have: 426 posts

Joined: Feb 2005

Yes i get the idea, infact it just came to me tonight before reading your post. The username and class will be a joint PK (cant remember what it is called when you have two attributes as a PK) I like the idea of having the styles loaded into a session, that way it doesnt need to load up each time.
But i dont know how i can grab the information out of the database. I mean, would i have two tables, one for the user, which would include the username password and email address..etc and one for the styles which includes the username, class, attributes..etc. Would i have to use a join when i query the database?

"In addition to that I'd probably grab the style information and save it to a cookie/session - only if that cookie/session is not already set (and therefor clearning it when they edit the style information). This would reduce the number of hits on the database."

Not sure i quite understand what you are saying here?

I am reasonably new to all this database stuff and this is the biggest task i have had to do, remember i need to somehow load up the content of all the pages, i figured there would be another table and in that would be a username, page_id(like about_us, contact..etc) and the actual content itself.

Would it be possible to explain all this in simpler terms, perhaps in more depth?

Thanks for the help.

Shirthead's picture

He has: 58 posts

Joined: Jun 2006

Bit hungover today, so apologies in advance if this is not as clear as it should be.

GETTING THE INFO

If you are storing the user_id in the style definition table then you wouldn't need to do a join. Something along the lines of the following would get all you need (assuming that your user_id is already stored in a sesson):

'SELECT * FROM style_definitions WHERE user_id = '.$_SESSION["uer_id"]

Once you have got that from the database just create a look to put each value in the session.

Quote: "In addition to that I'd probably grab the style information and save it to a cookie/session - only if that cookie/session is not already set (and therefor clearning it when they edit the style information). This would reduce the number of hits on the database."

Not sure i quite understand what you are saying here?

OK - breaking that down a bit:

1. Somewhere at the top of every page (before any output) I'd have an include file that..
a. checks whether the style session has already been saved as a session
b. if it hasn't then it gets the information from the database and saves it all to the session
c. if nothing is in the database (for instance, user has not changed the style) then set some default values
2. Have a page somewhere for users to edit the style that
a. edits the relevant database records
b. clears the session containing the style information (so that on the next page load this will all be collected from the database again).

Does that make any more sense?

They have: 426 posts

Joined: Feb 2005

Yes that makes good sense, thanks for a reply with a hangover by the way. But, does it mean that each attribute from the database, like border, color etc have to be set in a session of their own and then declared in the stylesheet like

html {
border:

<?php
$_SESSION
[border];
?>
;
color:
<?php
$_SESSION
[color];
?>
;
}

also how can i grab the username from the URL like mysite.com/website_temp?user=user1

to load up the appropriate styles, would i use $_GET[id] and put it in a session as well?

Think i am getting it.

But if i managed to assign a sub domain to all users how can i grab the sub domain part as the id like user1.mysite.com

Also if i want to class each part of the template, i may need a table for the classes and values and one table for the usernames and other info.

example;

create table users {
username varchar(30)
password varchar(30)
email_address text
address
phone_number
}

create table styles {
class varchar(50)
border
color
background
font
}

get the idea, i need to somehow join the two to get the username and styles for each.

unless each user has his own table on registration and perhaps the name is the name of the table is the same as the username the could just ask the database to load up the values of the table which has the name $_SESSION[user_id] or perhaps the $_GET[id]. ????

Shirthead's picture

He has: 58 posts

Joined: Jun 2006

Yes to all the first bits.

With regards to setting the user, you could always put the subdomain in the user table of the database as well. That way you could just check the subdomain and add that to your select statement. So the previous select would end up as something along the lines of...

'SELECT * FROM style_definitions, user_info WHERE user_info.user_id = '.$_SESSION["uer_id"].' AND user_info.user_subdomain = '.$thisSubdomain.' and user_info.user_id = style_definitions.user_id'

Somthing like that anyway.

They have: 426 posts

Joined: Feb 2005

ok thanks for the help am now going to mess around and see what i can do!!! Will up date this post if i manage to do something incredible.

Thanks again, lots of help.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.