Xtremer360 Posted July 9, 2011 Share Posted July 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241504-template-storing-in-database/ Share on other sites More sharing options...
davidmyers Posted July 9, 2011 Share Posted July 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241504-template-storing-in-database/#findComment-1240562 Share on other sites More sharing options...
Xtremer360 Posted July 9, 2011 Author Share Posted July 9, 2011 Well I"m developing a form for a cms that would include an area for the header, footer for user input and have it store those inside the db but not sure what else I'm missing. Quote Link to comment https://forums.phpfreaks.com/topic/241504-template-storing-in-database/#findComment-1240563 Share on other sites More sharing options...
davidmyers Posted July 9, 2011 Share Posted July 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241504-template-storing-in-database/#findComment-1240568 Share on other sites More sharing options...
Xtremer360 Posted July 9, 2011 Author Share Posted July 9, 2011 That doesn't make sense to me because the header/footer template for the site the CMS is for why would I need a template footer for a form. Quote Link to comment https://forums.phpfreaks.com/topic/241504-template-storing-in-database/#findComment-1240572 Share on other sites More sharing options...
davidmyers Posted July 9, 2011 Share Posted July 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241504-template-storing-in-database/#findComment-1240579 Share on other sites More sharing options...
Xtremer360 Posted July 9, 2011 Author Share Posted July 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241504-template-storing-in-database/#findComment-1240584 Share on other sites More sharing options...
davidmyers Posted July 9, 2011 Share Posted July 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241504-template-storing-in-database/#findComment-1240593 Share on other sites More sharing options...
Xtremer360 Posted July 9, 2011 Author Share Posted July 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241504-template-storing-in-database/#findComment-1240596 Share on other sites More sharing options...
davidmyers Posted July 9, 2011 Share Posted July 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241504-template-storing-in-database/#findComment-1240598 Share on other sites More sharing options...
Xtremer360 Posted July 9, 2011 Author Share Posted July 9, 2011 Thank you for all your great input David. Very much appreciated. Do you know of any further website tutorials that I can look over to get an even better feel for what I'm trying to accomplish? Quote Link to comment https://forums.phpfreaks.com/topic/241504-template-storing-in-database/#findComment-1240601 Share on other sites More sharing options...
davidmyers Posted July 9, 2011 Share Posted July 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241504-template-storing-in-database/#findComment-1240612 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.