Jump to content

Two questions: PHP CSS Switch & Link code check


crazylegsmurphy

Recommended Posts

Hey Everyone,

 

I have two questions to start off with, so I hope you don't mind helping me out. This is the last bit of stuff I need done to finish off my clients site, so it would be great to get it working.

 

1. PHP CSS Switcher

 

I have tried pretty much every PHP css switcher I could find on the net, but none seemed to work...or more accurately they all failed various ways.

 

What I need is the client wants the CSS to change based on the season the viewer is looking at the page. Not only that, but they want the user to be able to manually change the season at the top of the page and have it stay like that until the next visit.

 

I have the following code...

 

$date = date("F");
if (!isset($_GET['css'])) {
if ($date >= April && $date <= May) { ?>
<link rel="stylesheet" href="/_stylesheets/spring.css" type="text/css" media="screen" />
<? } elseif ($date >= June && $date <= August) { ?> 
<link rel="stylesheet" href="/_stylesheets/summer.css" type="text/css" media="screen" />
<? } elseif ($date >= September && $date <= October) { ?> 
<link rel="stylesheet" href="/_stylesheets/autumn.css" type="text/css" media="screen" />
<? } elseif ($date >= November && $date <= March) { ?>
<link rel="stylesheet" href="/_stylesheets/winter.css" type="text/css" media="screen" />
<? } else { ?>
<link rel="stylesheet" href="/_stylesheets/screen.css" type="text/css" media="screen" />
<? }; 
} else { ?>
<link rel="stylesheet" href="</_stylesheets/<?php echo $_GET['css']; ?>.css" type="text/css" media="screen" />
<? }; ?>

 

Which seems to work for the date (except for some reason it's only showing the "Spring.css" even though it's July). If anyone has any idea why that is....

 

The second part which I can't figure out is how to get this code to work with a manual switcher.

 

 


 

My second question is, I have the following code.

 

<?php if ($section == 'gallery-spring') { ?><li class="current">Gallery</li><?php } else { ?><li><a href="/gallery/">Gallery</a></li><?php }?>

 

What I need is to also say, if ($section == 'gallery-spring' OR 'gallery-summer' or 'gallery-winter')

 

When I tried using the  || (or) then the link stayed active the entire time....but when I took all the extra's off it was fine...

 

Any idea how to make it look at multiple sections?

 

Thanks so much everyone!

 

Jeff

Link to comment
Share on other sites

this should be

$date = date("F");
if (!isset($_GET['css'])) {
if ($date >= April && $date <= May) { ?>
<link rel="stylesheet" href="/_stylesheets/spring.css" type="text/css" media="screen" />
<? } elseif ($date >= June && $date <= August) { ?> 
<link rel="stylesheet" href="/_stylesheets/summer.css" type="text/css" media="screen" />
<? } elseif ($date >= September && $date <= October) { ?> 
<link rel="stylesheet" href="/_stylesheets/autumn.css" type="text/css" media="screen" />
<? } elseif ($date >= November && $date <= March) { ?>
<link rel="stylesheet" href="/_stylesheets/winter.css" type="text/css" media="screen" />
<? } else { ?>
<link rel="stylesheet" href="/_stylesheets/screen.css" type="text/css" media="screen" />
<? }; 
} else { ?>
<link rel="stylesheet" href="</_stylesheets/<?php echo $_GET['css']; ?>.css" type="text/css" media="screen" />
<? }; ?>

 

this

$date = date("F");
if (!isset($_GET['css'])) {
if ($date >= "April" && $date <= "May") { ?>
<link rel="stylesheet" href="/_stylesheets/spring.css" type="text/css" media="screen" />
<? } elseif ($date >= "June" && $date <= "August") { ?> 
<link rel="stylesheet" href="/_stylesheets/summer.css" type="text/css" media="screen" />
<? } elseif ($date >= "September" && $date <= "October") { ?> 
<link rel="stylesheet" href="/_stylesheets/autumn.css" type="text/css" media="screen" />
<? } elseif ($date >= "November" && $date <= "March") { ?>
<link rel="stylesheet" href="/_stylesheets/winter.css" type="text/css" media="screen" />
<? } else { ?>
<link rel="stylesheet" href="/_stylesheets/screen.css" type="text/css" media="screen" />
<? }; 
} else { ?>
<link rel="stylesheet" href="</_stylesheets/<?php echo $_GET['css']; ?>.css" type="text/css" media="screen" />
<? }; ?>

Link to comment
Share on other sites

if(date..... || $user = "spring")

 

I thought of that, the problem is that if the user clicks the link "spring" then the second it changes pages will it not re-read the code and default back to the date?

 

I think I need to store that in a cookie/session or something until the user leaves so it won't be switching back.

 

 

Link to comment
Share on other sites

That's pretty much where I'm stuck....

 

The examples I found on the net don't seem to work all that well.

 

I can find stuff like this...

 

$Year =31536000 + time();
setcookie ('style', $choice, $year);
header("Location: $HTTP_REFERER");
?>

 

but it seems like really outdated and bad code...nor did it work when I tried it. The one I did get to work (it stored the cookie) couldn't actually read the cookie back into the page for some reason thus not changing anything.

 

Personally I would rather use a session than a cookie just in case cookies are turned off. It's also better for me because I think the users need to default back to the date chosen css file and not have a cookie keeping them stuck on the one they last picked.

 

I'm not sure however how to do that effectively with the code I have.

 

 

Link to comment
Share on other sites

try

 

session_start();

$_SESSION['css'] = "spring";

 

Blade! That looks like it might work...

 

How would I pass that to the pages I'm currently on again? Sorry, I am drawing a blank.

 

Here is the link (don't blame me for the design...it wasn't me) :)http://www.mmmyeah.com/index1.php

 

<ul class="seasons"> 
              <li><a href="/_scripts/switch.php?style=spring" title="Spring">Spring</a></li>
              <li><a href="/_scripts/switch.php?style=summer" title="Summer">Summer</a></li>
              <li><a href="/_scripts/switch.php?style=autumn" title="Autumn">Autumn</a></li>
              <li><a href="/_scripts/switch.php?style=winter" title="Winter">Winter</a></li>
            </ul>

 

This is the links I have at the top of every page (via template)

Link to comment
Share on other sites

Well, what I mean is that the user has links along the top (as you can see on the link I posted)

 

They need to be able to click say, "winter" and it'll pass a variable somewhere to allow the session to be "winter". Then the date code that we worked on above needs to be able to see that there is a session with the variable "winter" and keep displaying the "winter.css" file until they leave.

 

 

Link to comment
Share on other sites

So...(just trying to wrap my head around things)

 

If I have this as my links...that is sending the variable to the session correct?

 

  <ul class="seasons"> 
              <li><a href="?css=spring" title="Spring">Spring</a></li>
              <li><a href="?css=summer" title="Summer">Summer</a></li>
              <li><a href="?css=autumn" title="Autumn">Autumn</a></li>
              <li><a href="?css=winter" title="Winter">Winter</a></li>
            </ul>

 

 

From there...

 

I'm confused as to how to capture the session...

 

 

I have my PHP date code...

 

<?php 
$date = date("n");
if (!isset($_GET['css'])) {
if ($date >= 4 && $date <= 5){ ?>
<link rel="stylesheet" href="/_stylesheets/spring.css" type="text/css" media="screen" />
<? } ?>

 

Do I need to somehow say, "if date is... or session is "spring" { ?>spring.css

 

I think I'm confused by this

 

session_start();

$_SESSION['css'] = "spring";

 

where does that go, and do I not have to have that blank or something so the session captures the variable passed by the link?

 

i'm a tad confused how to get it all working together.

Link to comment
Share on other sites

I did this...

 

<? } elseif ($date >= 6 && $date <= 8 || $css = "summer") { ?> 
<link rel="stylesheet" href="/_stylesheets/summer.css" type="text/css" media="screen" />
<? } elseif ($date >= 9 && $date <= 10 || $css = "autumn") { ?> 

 

And it seems to switch it to autumn...but then if I click "summer" it goes to autumn....they all go to autumn actually for some reason. :D

 

crap...and it also defaults the css file back to spring...something it's right there...

Link to comment
Share on other sites

This is the full code...

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
  <title>carraigeridge.com</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Content-Style-Type" content="text/css" />
  <meta name="keywords" lang="en" content="" />
  <meta name="description" lang="en" content="" />
  <meta name="copyright" content="" />
  <meta name="robots" content="all" />
  
  	<script type="text/javascript" src="/_scripts/lightbox/prototype.js"></script>
<script type="text/javascript" src="/_scripts/lightbox/scriptaculous.js?load=effects,builder"></script>
<script type="text/javascript" src="/_scripts/lightbox/lightbox.js"></script>
    <script type="text/javascript" src="/_scripts/external.js"></script> 
  
  	<link rel="stylesheet" type="text/css" media="screen" href="/_stylesheets/lightbox.css"  />
    <link rel="stylesheet" type="text/css" media="screen" href="/_stylesheets/screen.css" />
    <link rel="stylesheet" type="text/css" media="print" href="/_stylesheets/print.css" />
    <!--[if IE 6]>
    <link rel="stylesheet" type="text/css" media="screen" href="/_stylesheets/screen.css" />
    <link rel="stylesheet" type="text/css" media="print" href="/_stylesheets/print-ie.css" />
    <![endif]-->

<?php

?>

<?php 
$date = date("n");
if (!isset($_GET['css'])) {
if ($date >= 4 && $date <= 5){ ?>
<link rel="stylesheet" href="/_stylesheets/spring.css" type="text/css" media="screen" />
<? } elseif ($date >= 6 && $date <=  { ?> 
<link rel="stylesheet" href="/_stylesheets/summer.css" type="text/css" media="screen" />
<? } elseif ($date >= 9 && $date <= 10) { ?> 
<link rel="stylesheet" href="/_stylesheets/autumn.css" type="text/css" media="screen" />
<? } elseif ($date >= 11 && $date <= 3) { ?>
<link rel="stylesheet" href="/_stylesheets/winter.css" type="text/css" media="screen" />
<? } else { ?>
<link rel="stylesheet" href="/_stylesheets/screen.css" type="text/css" media="screen" />
<? }; 
} else { ?>
<link rel="stylesheet" href="</_stylesheets/<?php echo $_GET['css']; ?>.css" type="text/css" media="screen" />
<? }; ?>

    
    
</head>

 

If I put in the session code between the PHP tags as you had on the other page... I get

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/mmmyeahc/public_html/_includes/top.php:11) in /home/mmmyeahc/public_html/_includes/top.php on line 29

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/mmmyeahc/public_html/_includes/top.php:11) in /home/mmmyeahc/public_html/_includes/top.php on line 29

 

 

Link to comment
Share on other sites

I think I solved my second question...

 

<?php if ($section == 'gallery-spring' || $section == 'gallery-summer') { ?><li class="current">Gallery</li><?php } else { ?><li><a href="/gallery/">Gallery</a></li><?php }?>

 

This works... I wonder if there is a better way to write it however....

 

 

Link to comment
Share on other sites

Oops...

 

I also have this as well...

 

The site builds two files around the middle content....

 

<?PHP
$section='community';
include('../_includes/top.php');
?>

<div id="content-preview">
            
            <p class="italic">Engage, Inspire...</p>
            <p><a class="more" href="community.php" title="community">More...</a></p>
            
            </div>

<?PHP
include('../_includes/bottom.php');
?>
            

Link to comment
Share on other sites

If I put this:

 

<?PHP
session_start();
echo "Current Session is:  ".$_SESSION['css'];
$section='community';
include('../_includes/top.php');
?>

<div id="content-preview">
            
            <p class="italic">Engage, Inspire...</p>
            <p><a class="more" href="community.php" title="community">More...</a></p>
            
            </div>

<?PHP
include('../_includes/bottom.php');
?>
            

 

And then click the links sending ?css="winter" it doesn't give me a session.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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