Jump to content

Multy styles with php


irn3rd

Recommended Posts

Hello again,

 

I have a few php files with my websites design and colouring in, like my top banner my links and my colour scheme, but is it possible to use php to switch between two design adn use a cookie so it can be remembered on the next page etc and when the user re-visits the site?

 

Thanks again Ben.

Link to comment
https://forums.phpfreaks.com/topic/114866-multy-styles-with-php/
Share on other sites

sure is ben..

 

on the file that you pull in your stylesheet.css file

 

replace

 

yourstyle.css

 

with

 

$yourstyle

 

variable.. and then setup a function to place the designated stylesheet name into the "$yourstyle" variable.

 

at the same time create a session variable or cookie that contains the style selected..

Right i know what your saying its just getting to do it, I've only dealt with variables in URL strings in taking data from one page to another.

 

My CSS for background and Text/links:

<style type="text/css">
<!--
body {
background-color: #0024FF;
}
a:link {
text-decoration: none;
color: #000000;
}
a:visited {
text-decoration: none;
color: #000000;
}
a:hover {
text-decoration: underline;
color: #FFFFFF;
}
a:active {
text-decoration: none;
}
body,td,th {
color: #FFFFFF;
}
-->
</style>

 

I use includes to bring it to the file i want for example:

<?php
include('includes/style.php')
?>

 

I would like to do the multi change code in a drop down list, i know how to set the list up its just getting it to work i don't understand.

 

I would be grateful for some help,

I'm still learning and searching for tutorials etc

 

I'm very greatful and if you have a website ill put it on my i know it aint much but its a little something for you for helping me.

Thank you.

ok well it has alot to do with your website structure believe it or not..

 

if your site is setup like myne... I use an index.php file that ALL other site pages are included too.

 

based on which page you select one of the includes is brought in that reflects that page..

 

example:

(index.php)

<?php

require('header.php')//Just like include only the script will error if the file is missing..

//HERE WE WILL CHECK FOR WHICH PAGE THE USER SELECTED
if(isset($_GET['page']))//THIS MEAN THE CODE BELOW WILL ONLY RUN IF $_GET['page'] is set..
{
if($_GET['page']=='home')//IF USER SELECTS HOME PAGE
{
	include('home.php');//INCLUDE HOME PAGE
}
}

 

if you do your site this way.. you can do this for you stylesheet

 

<?php
session_start();

//NOW WHEN YOU LOGIN THE USER OR THE USER SELECTS THEIR STYLE, YOU"LL NEED TO SET A SESSION
//VARIABLE HOLDING THE STYLE NAME ie..'Yourstyle.css'


///SELECT STYLE
if(isset($_SESSION['style']))
{
$style=$_SESSION['style']; //MAKE $STYLE EQUAL TO STYLE SELECTED

echo("<link rel='stylesheet' type='text/css' src=' " . $style . " ' />"); //ECHO OUT THE HTML FOR THE STYLE SHEET TO USE
}





///SELECT PAGE

if(isset($_GET['page']))//THIS MEAN THE CODE BELOW WILL ONLY RUN IF $_GET['page'] is set..
{
if($_GET['page']=='home')//IF USER SELECTS HOME PAGE
{
	include('home.php');//INCLUDE HOME PAGE
}
}
?>

Hello ag3nt42, :)

 

So i put the first box of code in my index page, and the second bow if i stick that in what?

 

and if its more useful ill upload my indexpage with my style php file? if that saves you time instead of explaining?

 

Sorry if i seem slow im still getting used to this.

 

thanks again

yea post your index code..

 

really tho all you have to do is set a SESSION variable somewhere and then each page plug it into the stylesheet src..

if(USER SELECTS ThisStyle.css)
$_SESSION['style']='ThisStyle.css';



$STYLE=$_SESSION['style'];


echo("<link rel='stylesheet' type='text/css' src='".$STYLE."'");

 

 

if you do your index page like myne tho you won't have to put the above code on every page.

ok so your going to set up your sessions  and stylesheets in your header.php

 

since that should be carried across all the pages..

 

create a selection box containg a list of all the different style sheets you have..

 

your html form for users selection:

 

<?php

//INTIALIZE VARIABLES
$S_checked1='';
$S_checked2='';
$S_checked3='';
$S_checked4='';

echo("
<select name='style'>
<option ".$S_checked1." value='style1.css'>Style 1</option>
<option ".$S_checked2." value='style2.css'>Style 2</option>
<option ".$S_checked3." value='style3.css'>Style 3</option>
<option ".$S_checked4." value='style4.css'>Style 4</option>
</select>
");

?>

 

the php to process it:

 

<?php

//First COLLECT THE POST INFO
if(!(isset($_POST['style'])))
{
    $STYLE='';// IS NOT SET SO STYLE EQUALS NOTHING (or default style)
}
else
{
    $STYLE=$_POST['style']; // IS SET SO STYLE EQUALS USERS CHOICE
}

echo("<link rel='stylesheet' type='text/css' src=' " . $STYLE . " ' ");

//NOW WE RUN ANOTHER CHECK TO SEE WHICH ONE USER PICKED AND CHECK IT ON THE SELECTION DROP DOWN   
switch ($STYLE){
case "style1.css":
	$S_checked1="CHECKED";
	break;
case "style2.css":
	$S_checked2="CHECKED";
	break;	
case "style3.css":
	$S_checked3="CHECKED";
	break;	
case "style4.css":
	$S_checked4="CHECKED";
	break;
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.