Silveredge9 Posted February 5, 2009 Share Posted February 5, 2009 I'm relatively new to PHP, and I'm wondering if at all it would be possible to allow the user to select an include file via a link or drop down menu, and then have that include file automatically applied to the website and then saved so that choice doesn't reset for them when they leave the page. I'm asking because my website's template is defined by an include in the websites header. For example: <?php include("layout_default.php")?> And I want the user to be able to choose from a list of available templates (aka: include files) and then have that template applied to their particular version of the website and then saved until they select another one. Thanks for any help in advance, and if what I've written doesn't make any sense, feel free to point that out. Quote Link to comment https://forums.phpfreaks.com/topic/143947-template-switching/ Share on other sites More sharing options...
JonnoTheDev Posted February 5, 2009 Share Posted February 5, 2009 Yes, when the user selects a view you can then set a cookie. Use the cookie value to display the appropriate template. If no cookie is set then use a default template. Look at cookies. Quote Link to comment https://forums.phpfreaks.com/topic/143947-template-switching/#findComment-755350 Share on other sites More sharing options...
Silveredge9 Posted February 5, 2009 Author Share Posted February 5, 2009 I've checked out cookies, and have come up with the following code: <?php $expire=time()+60*60*24*9990; $set = $_GET['set']; if(isset($_COOKIE['style'])) { $set = $_COOKIE['style']; } else { switch ($set) { case 1: $set = "layout_darkness.php"; break; case 2: $set = "layout_default.php"; break; default: $set = "layout_default.php"; break; setcookie('style', $set, $expire); } } ?> And I've added the links to my site: <a href="indextest.php?set=1">Darkness</a> <a href="indextest.php?set=2">Default</a> When the links are clicked, the chosen template is successfully applied to the website, but it isn't saved. Could someone take a look and my code and correct it? Quote Link to comment https://forums.phpfreaks.com/topic/143947-template-switching/#findComment-755431 Share on other sites More sharing options...
JonnoTheDev Posted February 6, 2009 Share Posted February 6, 2009 You should not be passing the value of the cookie through the URL - you dont need to. You are better organising your templates in a better directory structure. Lets say I select a view on your website. The option values are 1,2, and 3. If I select 2 then a cookie is set with the value of 2. So lets organise your templates in the following way: templates/ 1/layout_default.php 2/layout_default.php 3/layout_default.php Now in your include function I use this value. include('templates/'.$_COOKIE['value'].'/layout_default.php'); I would also recommed using a template engine such as smarty as it will make things a lot easier. http://smarty.net Quote Link to comment https://forums.phpfreaks.com/topic/143947-template-switching/#findComment-755883 Share on other sites More sharing options...
Silveredge9 Posted February 6, 2009 Author Share Posted February 6, 2009 So how - using what you suggested as a means of applying a template - would the cookie responsible for assigning templates be set from a link or drop down menu? I'm sure it's something relatively simple, but like I said, I've only recently began learning PHP. Quote Link to comment https://forums.phpfreaks.com/topic/143947-template-switching/#findComment-756151 Share on other sites More sharing options...
JonnoTheDev Posted February 8, 2009 Share Posted February 8, 2009 No, the cookie is set by the user selecting the template they require (that is how you wanted it, correct?) Cookies are read from the users computer. There is no requirement to pass any vaues from page to page. Quote Link to comment https://forums.phpfreaks.com/topic/143947-template-switching/#findComment-757495 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.