Jump to content

Template Storing in database


Xtremer360

Recommended Posts

I didn't see a tutorial like this on the phpfreaks.com website so I'll ask it in here. I'm looking for a tutorial that teaches good use of how to store templates systems inside of a database. Anyone know of a good tutorial for this. All I can find is just using files but I'm looking to store these via a database.

Link to comment
Share on other sites

What kind of template are you referring to?

 

I'm going to assume you're talking about a template for a page, ie storing standard paoge layout/markup to be re-used by calling a function ( or in your case a database query).

If that's what you're talking about then it's much much easier to just create a PHP class with functions that echo the different parts of the page/template that you want when you call them.

Link to comment
Share on other sites

The easiest way to do this is (in my experience so far) to create a function such as this:

 

function NewForm()
{
    echo '
<form id="NewForm" >
    <div id="Header">Header</div>
    <div id="FormContent">
        Name: <input type="text" id="UserName">
    </div>
    <div id="Footer">
        <input type="submit" value="Submit">
    </div>
</form>';
}

 

That way you can make specific types of forms one time and then re-use them whenever necessary just by calling the function. Of course you'd need code to handle processing the form data and adding it to a database, but this shows you what I think you're looking for.

Link to comment
Share on other sites

I don't think I understand what it is you are trying to do. If you could explain in more detail or include code of what you've got that would help.

 

If you're trying to make a form that is completely dynamic then it's pretty much the same principle of what I've shown  you but will include accepting and handling lots of arguments, and will need lots of conditional statements.

Link to comment
Share on other sites

Okay let me try this again. Lets say I have a list of templates for 1 website.  I'm developing a form to add another template to the website.  So basically the form would be composed of the area for the header and footer. This is not referencing the header of the form but the header of the website.

Link to comment
Share on other sites

Ok, this is way different than what I initially thought you were wanting.

 

If I understand this correctly, you want to have different site templates/layouts/themes that users can select to alter what the site looks like for them. First off you'll want to have every template/layout/theme work with the same base HTML, meaning that you have a bunch of div tags making up the page and what the site looks like is completely dependent upon the css applied to it. Then once you have all of your different template/layouts/themes created and working, have a place where the user can select the one they want. You can store their selection in a database either as a number or a name that references the particular template.

 

So for instance a table column named "template" which holds the name of the template they chose. Then at the beginning of every page, just check the current users template variable and call the correct stylesheet accordingly.

 

Hopefully this helps.

Link to comment
Share on other sites

Yes that is good information but does not actually answer my original question. On the add new template form what other stuff do I need other than just a textarea for each the header and the text area for the footer? Possibly the name of the template and what else if anything.

Link to comment
Share on other sites

I just realized that you said you were creating a form to add a new template to the site. To do this you would want to have them choose their options (which can be anything definable in css) and then use those to create a new stylesheet for that template using PHP.

 

Then you would use the same kind of system that I already described to fetch the correct template for them whenever they visit a page.

 

If you're setting it up where a site admin would do this but it would affect the entire site for every user, you would just check the "template" column value for the admin and apply that stylesheet every time the page is loaded.

 

Honstly, creating a new template for the site in this way is valuable but will take a lot of work to write, especially if you're trying to make it really customizable.

 

Also just saw your new post and I'm not sure what you would be using the textareas for. If you're including customization for page titles and such then you'll want to store that information in the database and call it like the stylesheet. In fact, if you have a lot of things you want to store about the templates, I'd make a table for template info and then have an entry for every user that specifies all of their options, like page header, css template name, etc.

Link to comment
Share on other sites

I feel like I use to have a couple links to sites regarding this, but I can't find them. I do know of this one link though:

http://www.developphp.com/view.php?tid=1037&t=Website_Design_Theme_Application_Tutorial_Using_PHP_Cookies_to_Change_CSS

This guy uses cookies to store users theme choices and you can't customize them, but it's a good way to see step by step some of the things you'll need to get your own version working.

 

Really though that's gonna be the easy part. Personally I'm a fan of jQuery and it would make the customization form a lot easier to process imo. I've been planning on building a feature similar to this, just with much customization. Mostly only letting users customize colors. But I'm thinking of possibly writing some tutorials in the future. If you'd be interested I could do this one first, just let me know.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.