template selector

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

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

template selector

Post by nite4000 »

hey All

I have a task i am trying to complete.

I want to have like 3 layouts each in a folder called layout1,layout2,and layout3 now in the db i want to have a tabled called layout and a field called template and in the field i want to have like layout2/index.html

now when I would go to like http://www.mydomain.com it will chk and load the template that is in that field. I dont have a drop box or anything so I will have to change it manual til i make one but this was the major part of my task


Any help would be nice.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: template selector

Post by Jade »

Do you have a database setup all ready? Have you tried to code any of this on your own yet? The basic idea is that whenever your website loads a page it will need to check which template it's using, and then implement that template. I would suggest writing everything in CSS and then just loading different stylesheets as this is the easiest way and gives you the most flexibility, for example CSS Zen Garden can show you the awesome power of CSS.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: template selector

Post by nite4000 »

yeah i have a db setup and i know i would have to have the site chk for the template its using just not sure how to begin. i know i have to define the template to use in teh db then use the site to load that template from the correct folder.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: template selector

Post by AbraCadaver »

nite4000 wrote:hey All

I have a task i am trying to complete.

I want to have like 3 layouts each in a folder called layout1,layout2,and layout3 now in the db i want to have a tabled called layout and a field called template and in the field i want to have like layout2/index.html

now when I would go to like http://www.mydomain.com it will chk and load the template that is in that field. I dont have a drop box or anything so I will have to change it manual til i make one but this was the major part of my task


Any help would be nice.
Are you a teenage girl? :wink:
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: template selector

Post by nite4000 »

how about you dont reply if you are going to be a smartass
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: template selector

Post by Jade »

Okay so basically you're going to create a file called template_config.php:

Code: Select all

session_start(); //we use sessions so we don't query all the time
include('dbconnect.php'); //this is an include file with the connection to your database

if (!$_SESSION['template']) //there is no template loaded
{
       //select all the information from your table for whatever template you have marked as active. 
       //if you don't have an active field (bool) then I would add one, this way you can just change whatever the active template is
       //and your site will reflect that change as soon as the template is re-loaded into the session
       $result = mysql_query("SELECT * FROM database_name_here WHERE active='1'")
                     or die (mysql_error()); 

       $row = mysql_fetch_array($result);
       $_SESSION['template'] = $row['template_file_name']; //or whatever the field is with the CSS file name in it
}
?>
And on any page you want to use the templates, just include your template page and echo the session's template information:

Code: Select all

<?php
include('template_config.php');
?>
<html>
<head>
<title>My Website</title>
<link rel="stylesheet" type="text/css" href="<?php echo $_SESSION['template']; ?>" />
</head>
<body>
This is where the rest of your page goes
</body>
</html>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: template selector

Post by AbraCadaver »

nite4000 wrote:how about you dont reply if you are going to be a smartass
Just good natured fun, hence the :wink: And you didn't answer the question. :D
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply