PDA

View Full Version : Trying to wrap my brain around this.


Sara
10-11-2002, 07:12 AM
Hi,

I have been trying to figure out how to keep adding themes to this:




<?
// This is the first theme
if($theme == 'theme1')
{
$stylesheet = "theme1.css";
$headerimg = "<div id="logo">Image for theme one goes here</div>";
}

// This is the second theme
else if($theme == 'theme2')
{
$stylesheet = "theme2.css";
$headerimg = "<div id="logo">Image for theme 2 goes here</div>";
}

// This is the third theme -- also the default theme
else
{
$theme = 'theme3';
$stylesheet = "theme3.css";
$headerimg = "<div id="logo"><img src="theme3.gif" alt="logo" /></div>";
}

?>



I'm working on making themes for my blog that uses a certain tutorial. Unfortunately they never finished or took out the links to the information for adding more themes.

Help would be wonderful, thank you in advance!

:) Sara

macshack
10-11-2002, 11:32 AM
Hi Sara,

I saw your post over on the b2 forums and was going to respond,but, I got busy with other stuff....

All you need to do is add an additional 'ifelse' set to what you have. So if you were to add theme4 it would look like this:
// This is the forth theme
else if($theme == 'theme4')
{
$stylesheet = "theme4.css";
$headerimg = "<div id="logo">Image for theme 4 goes here</div>";
}
and inserted just before the line:
// This is the third theme -- also the default theme

If you are going to add many more themes than this, maybe a better approach would be to use the "switch" statement. It is a little easier to "read" and understand what is going on.

Here is an example using you post...

<?
switch ($theme) {
// This is the first theme
case 'theme1':
$stylesheet = "theme1.css";
$headerimg = '<div id="logo">Image for theme one goes here</div>';
break;

// This is the second theme
case 'theme2':
$stylesheet = "theme2.css";
$headerimg = '<div id="logo">Image for theme 2 goes here</div>';
break;

// This is the next one to add
case 'themeX':
$stylesheet = "themeX.css";
$headerimg = '<div id="logo">Image for theme X goes here</div>';
break;

// This is the third theme -- also the default theme
default:
$theme = 'theme3';
$stylesheet = "theme3.css";
$headerimg = '<div id="logo"><img src="theme3.gif" alt="logo" /></div>';
}
?>

You can just add more "blocks" as I did in the 'themeX' block above.
Also not I changed your use of double quotes to single quotes. Thus eliminating the need to escape the needed double quotes in the image tag.

Hope this helps,
Michael e

Sara
10-11-2002, 11:33 AM
Hi Michael, thanks for the response! Yes, I used the switch and it worked perfectly! :)

Sara

macshack
10-11-2002, 11:37 AM
your welcome,

Michael e

pete3005
10-11-2002, 01:56 PM
macshack has covered 99.9% of it, I would however, use single quotes for $stylesheet = "theme1.css";

so do

$stylesheet = 'theme1.css';

a minor picky issue but it's faster than double quotes.

Also Sara, where is $theme coming from? I assume a link or a form right?
If so its good to get into the proper habit of coding with the super globals, as by default PHP now comes with register globals Off (although most hosts will turn them on due to not wanting to break peoples scripts).

Anyway, switch ($theme) would be

switch ($_GET['theme']) or

switch ($_POST['theme'])

all very minor issues but its good to get into good coding habits :)

Pete:D

Sara
10-12-2002, 03:51 AM
Hi Pete,

I'm trying to learn to do this correctly, so your help would be great!

I have this in my index.php



<!-- The theme links --><h2>Skin me</h2><br />
<A href="<?= $_SERVER['PHP_SELF']; ?>?theme=theme1">Fall</a><br />
<A href="<?= $_SERVER['PHP_SELF']; ?>?theme=theme2">America</a><br />
<A href="<?= $_SERVER['PHP_SELF']; ?>?theme=theme3">Butterflies*</a><br />
<A href="<?= $_SERVER['PHP_SELF']; ?>?theme=theme4">Halloween</a><br /><br />



Is there a better way to do this? I tried what Michael said to do with the escapes, to use single quotes instead, but it doesn't work. I was wondering if this was the reason.

I love learning new things, y'all are wonderful to teach me!

:)

Sara

pete3005
10-12-2002, 05:05 AM
Sara,

Can you post the entire script. What you have should work fine. If you could post the whole page code that would help to find out where the problem is.

I assume you have the switch statement before you output $stylesheet right?

Also if you have a live example please post the URL.

Thanks

Pete

pete3005
10-12-2002, 05:20 AM
Sara,

I just ran a test and this works just fine,

<?php
session_start();
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">";
if(isset($_GET['theme'])){
switch ($_GET['theme']) {
// This is the first theme
case 'theme1':
$stylesheet = 'theme1.css';
$headerimg = '<div id="logo"><img src="theme1.gif" alt="logo1" /></div>';
$_SESSION['csschanger']=$stylesheet;
$_SESSION['headerimg']=$headerimg;
break;

// This is the second theme
case 'theme2':
$stylesheet = 'theme2.css';
$headerimg = '<div id="logo"><img src="theme2.gif" alt="logo2" /></div>';
$_SESSION['csschanger']=$stylesheet;
$_SESSION['headerimg']=$headerimg;
break;

// This is the next one to add
case 'theme3':
$stylesheet = 'theme3.css';
$headerimg = '<div id="logo"><img src="theme3.gif" alt="logo3" /></div>';
$_SESSION['csschanger']=$stylesheet;
$_SESSION['headerimg']=$headerimg;
break;
case 'theme4':
$stylesheet = 'theme4.css';
$headerimg = '<div id="logo"><img src="theme4.gif" alt="logo4" /></div>';
$_SESSION['csschanger']=$stylesheet;
$_SESSION['headerimg']=$headerimg;
break;
// This is the third theme -- also the default theme
default:
$theme = 'theme3';
$stylesheet = 'theme3.css';
$headerimg = '<div id="logo"><img src="theme3.gif" alt="logo3" /></div>';
$_SESSION['csschanger']=$stylesheet;
$_SESSION['headerimg']=$headerimg;
}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
<A href="<?= $_SERVER['PHP_SELF']; ?>?theme=theme1">Fall</a><br />
<A href="<?= $_SERVER['PHP_SELF']; ?>?theme=theme2">America</a><br />
<A href="<?= $_SERVER['PHP_SELF']; ?>?theme=theme3">Butterflies*</a><br />
<A href="<?= $_SERVER['PHP_SELF']; ?>?theme=theme4">Halloween</a><br /><br />

/*code below just for debugging and testing to ensure session vars are set and that the switch statement is correct*/
<?php echo $headerimg.'<br />'.$stylesheet;?>
<br />
<?php
while(list($key, $Value) = @each($_SESSION)){
echo 'Session: '.$key .' : '. $Value . '<br />';
}
?>
</body>
</html>

Then to maintain persistence, on all other pages use

$_SESSION['csschanger'] to call the stylesheet and $_SESSION['headerimg'] to call the img, such as <?php echo $_SESSION['headerimg'];?> will display the correct header img.

I assume you know how to work with sessions, if not just ensure you put session_start(); at the top of every page.

HTH

Pete

Sara
10-12-2002, 05:32 AM
Um, thanks Pete, but that's way too complicated for me, I want to keep it simple. Nope, I'm not familiar with session code.

Thanks though!

The tutorial I used was: http://tutorials.php-princess.net/index.php?p=9&more=1

That did get me started although as you know I changed the code a tad to switches.

Besides that, it's all there.

Sara

pete3005
10-12-2002, 05:45 AM
Looking at that tutorial, it looks like the blog you are using it on runs from one page, index.php, so that would mean you won't need the sessions.

However if you want to add extra pages and maintain the theme the user selects from page to page, then my session solution is perfect for that.

Anyway, apologises if that was too complex, have you actually got it working or is something broken?

From your other post, it sounded like something wasn't working, macshacks code was fine and should work without problems, as below:

switch ($_GET['theme']) {
// This is the first theme
case 'theme1':
$stylesheet = 'theme1.css';
$headerimg = '<div id="logo"><img src="theme1.gif" alt="logo1" /></div>';
break;

// This is the second theme
case 'theme2':
$stylesheet = 'theme2.css';
$headerimg = '<div id="logo"><img src="theme2.gif" alt="logo2" /></div>';
break;

// This is the next one to add
case 'theme3':
$stylesheet = 'theme3.css';
$headerimg = '<div id="logo"><img src="theme3.gif" alt="logo3" /></div>';
break;
case 'theme4':
$stylesheet = 'theme4.css';
$headerimg = '<div id="logo"><img src="theme4.gif" alt="logo4" /></div>';
break;
// This is the third theme -- also the default theme
default:
$theme = 'theme3';
$stylesheet = 'theme3.css';
$headerimg = '<div id="logo"><img src="theme3.gif" alt="logo3" /></div>';
}

Sara
10-12-2002, 05:47 AM
Nothing is broken per say, Pete. I was just told, by you, in the first few posts to take out the escapes and use single quotes, when I did this it stopped working and I wanted to know why and how to fix it! Do you know?

Sara

pete3005
10-12-2002, 06:10 AM
There is no reason for it to stop working, can you post the code you are using please.

This:

$headerimg = '<div id="logo"><img src="theme1.gif" alt="logo1" /></div>';

works as does this:

$headerimg = "<div id=\"logo\"><img src=\"theme1.gif\" alt=\"logo1\" /></div>";

They both work but the first method doesn't make PHP work so hard and is faster as no variables need to be interpreted.

I would imagine the problem you are having is when you echo the vars, such as echo $headerimg. Variables are interpolated when they are in double quotes but not single quotes. So you should be doing something like this:

<?php echo 'Here is my header image'.$headerimg;?>

The above will work, this one below won't work:

<?php echo 'Here is my header image $headerimg' ;?>

This next one will work

<?php echo "Here is my header image $headerimg" ;?>

because it is in double quotes, my suggestion is to use the first method which is

<?php echo 'Here is my header image'.$headerimg;?>

to go back to text you do

<?php echo 'Here is my header image'.$headerimg. 'isn\'t it nice' ;?>

If you are just using one var to be outputted in a long string of text then this is the best option.

Here is my header image <?php echo $headerimg ?>, isn't it nice.

Hope those examples make things a little clearer, if you are still haveing problems, post the code.

Pete :)

harmonic
10-12-2002, 09:06 AM
a little more in depth,

anything encapsuled in double quotes (") will be parsed out for variables and functions.

<?
echo "$variable"; // WILL PRINT THE VALUE OF VARIABLE
?>

anything encapsuled in single quotes (') will be printed AS IS

<?
echo '$variable'; // WILL PRINT $variable
?>

periods (.) can be used as concatenation or link two things together

<?
echo "Hello " . "World"; // WILL PRINT "Hello World"
?>

single quotes can be used inside double quotes, and information will still be parsed

<?
echo "Some '$thing' Just got printed"; // $thing WILL BE PARSED OUT
?>

now, to use single quotes for things like arrays, there are two good methods

1. concatenation

<?
echo "Some text " . $array['variable'] . "Some more text";
?>

2. {'s and }'s, these will tell php to use literal meanings of characters

<?
echo "Some text {$array['variable']} Some more text";
// THIS WILL WORK LIKE ABOVE
?>

To prevent PHP from parsing characters in double quotes, like dollar signs and more double quotes escape them with a backslash.

<?
echo "this is a quote and dollar sign \"\$";
?>

for more see:

http://www.php.net/manual/en/language.basic-syntax.php

Sara
10-20-2002, 07:22 AM
Thanks for all the help. I went and did a different tutorial and have it working, somewhat. :)