How to make conditional PHP links? - php

I've searched for a way to do this, but since i'm not fluent in PHP, nothing i've tried seems to work. I have a general idea of how it should work, and it seems simple enough, but I can't get it to work the way I want it to.
Basically, I have two links on my site - each one of them links to a different style sheet, which is part of a JS style switcher. The style switcher works fine, except for one thing - I want the links to be conditional. For example, right now, my links look like this:
Make it Dark (alternate stylesheet)
Light it up (main stylesheet)
They both appear at once.
I'd like it to work this way: when the page loads, only the "Make it Dark" link shows. Then when it's on "Make it Dark", only a link to the Main style sheet shows.
This can be seen on the last.fm page at the very top right: http://www.last.fm/
There's two themes there: "Paint it Black" and "Simply Red". Only one link shows at a time so that you can switch between them. How would I be able to do that?
I'd appreciate any help with this. Thanks.

Would it work if in each of your stylesheets you gave one of your links display:none?

You can specify a class on both off them and than add a display: none in the opposite stylesheet to hide it for the user. So you have:
Make it dark
Light it up
And in the two stylesheets you have in the dark one:
.lighten { display: none; }
And in the light one:
.darken { display: none; }

Personally I'd use a session variable and not a cookie.
Something like this should work:
<?php
session_start(); // add to the top of your page
if (!isset($_SESSION['dark_theme'])) {
$_SESSION['dark_theme']==false;
}
if ($_GET['changetheme']!='') {
if ($_GET['changetheme']=='dark') {
$_SESSION['dark_theme']=true;
} else {
$_SESSION['dark_theme']=false;
}
}
?>
This bit, place where you want the link:
<?php
if(!$_SESSION['dark_theme']){
?>
Make it Dark
<?
}else{
?>
Make it Light
<?
}
?>
Or do it using CSS+JavaScript:
The HTML:
Make it Dark
Make it Light
The CSS:
In your "dark" stylesheet:
#darken {
display:none;
}
In your "light" stylesheet:
#lighten {
display:none;
}
JavaScript (note, I'm using JQuery as it's much easier to write than normal JS - just you'll need to include JQuery if it's not already on the site):
(function($, undefined)
{
$('#darken').click(function() {
$(this).preventDefault(); // stops the link from functioning as a link
chooseStyle('none', 60); //call your change stylesheet function
$('#darken').hide(); //hide the dark link
$('#lighten').show(); // show the light link
})
$('#lighten').click(function() {
$(this).preventDefault(); // stops the link from functioning as a link
chooseStyle('none', 60); //call your change stylesheet function
$('#lighten').hide(); //hide the light link
$('#darken').show(); // show the dark link
})
})(jQuery);

if($_COOKIES['dark_theme']){
$show_dark = true;
}else{
$show_dark = false;
}
if($show_dark){
echo "<link rel="stylesheet" href="/css/dark.css"...";
}else{
echo "<link rel="stylesheet" href="/css/light.css"...";
}
When you click "make it dark", just reload the page and modify the cookie.

Or you can tell php to do this in one line, you just need to set cookie when user is switching to another stylesheet change below cookie name according to your need.
echo (isset($_COOKIE['dark_theme'])) ? '<link rel="stylesheet" href="/css/dark.css">' : '<link rel="stylesheet" href="/css/light.css">';

Related

Conditionally print css style in Wordpress

I am working on a Wordpress theme and want to give the option to choose between a boxed layout and a full width layout.
For this purpose I created a variable in my header.php:
<head>
<?php
$isBoxedLayout = true;
?>
...
</head>
Down in the body I am asking if the variable is set:
<?php if($isBoxedLayout) { echo '<div id="boxed">'; } ?>
...
<?php if($isBoxedLayout) { echo '</div>'; } ?>
This works fine so far. But now I also want to change some css styles if this variable is set. My problem is that I am not so good in PHP yet so my solution would be something like this:
<head>
<?php
$isBoxedLayout = true;
?>
...
if ($isBoxedLayout) {
echo '<style type="text/css">';
echo '#container {width:999px;}';
echo '</style>';
}
</head>
But I think this is not good programming because my header.php file would soon be full of code and confusing if I would add some other options. So logically I should create a variable or an array maybe in the functions.php file and outsource my code like this:
$isBoxedLayout = true;
if ($isBoxedLayout) {
function boxed_css_styles() {
echo '<style type="text/css">';
echo '#container {width:999px;}';
echo '</style>';
}
}
Is my thinking right? And if so how would I access the functions I create in my index.php or header.php or whatever. Or would it work to print the styles in the functions.php?
best regards
Don't over-complicate this by trying to add styles via PHP. Add the ID via PHP...but not the styles themselves.
Since you only apply the boxed ID when the boxed layout is in effect, you can simply define some #boxed CSS styles. These styles will ONLY be applied if the ID exists in your markup...which means they won't come into effect when the ID isn't applied by your PHP.
In other words, put this in your CSS stylesheet, and forget about it:
#boxed {
width: 999px;
}
A flexible option would be to use the body_class() function to add an additional class to your body tag. This would allow you to scope your styles accordingly:
.boxed section {
/* Styles here /
}
.full-width section {
/ Styles here */
}
By adding the class on the body you can effectively target everything. This way you're keeping your styles separate from your markup.
Another option would be to keep your universal styles in one stylesheet and then load an additional stylesheet for your full-width/boxed specific styles. This can be handy for organisational purposes, by does add an extra http request, so you'd need to consider bear that in mind.

Iframe Question

If I control the content of two pages (PageA and PageB), one of which I want to load into an iframe (PageB), is there some javascript I can enter on PageB that will pull a style from the parent (PageA)? I need two style options, one for when PageB is loaded in an iframe, and one for when PageB loads on its own.
A solution I could see is loading the styles for both files from an external file, and set the src attribute of the IFrame to include a GET variable as such (rest of this example assumes a file with the name index2.php):
<iframe src="index2.php?iframe=Y" ... />
From there, towards the top of index2.php should look something like this:
<?php
if ($_GET["iframe"] == "Y") {
echo "<link href='global.css' type='stylesheet' />";
} else {
echo "<link href='specific.css' type='stylesheet' />";
}
....
?>
While Kris' solution is also valid, I wouldn't and never do rely on Javascript to work the same in all browsers, regardless of whether it's been tested or not. At least this way, you know that the file is being loaded into an IFrame.
I would do it this way: check to see if your page is loaded in a frame or not and choose your styles based on this rather than attempting to pull styles from a parent page.
You can detect if you're in a frame via this type of code:
if ( top.location.href !== window.location.href ) { /* In frame */} else { /* Not in frame */ }

Displaying item only on an osCommerce home page

I have a box ad in a <div> that I have put in index.php but it's showing up in almost all the other pages.
Which file do I need to edit or put this box ad in so that it is only visible on the home page and not on other pages?
Well i found the answer
if ( basename($PHP_SELF) == FILENAME_DEFAULT && $cPath==null) {
Give the body of your index.php an extra class. For example contentindex. Also, provide your ad-div with an extra style element:
<div class="contentindex" style="display:none;"/>
And in your css it should be something like:
.contentindex ad {
display: inline;
}
Work with parents and children.
But why do you want this? If it's not allowed to be visible, why is it the div there in the first place?
Even thought its not proper you can edit the language template since it allows HTML.
It's in catalog\includes\languages\english\index.php You could use:
define('TABLE_HEADING_NEW_PRODUCTS', 'Your content here.<br />New Products For %s');
Or something similar.
Add it to the bottom of the index.php file in the catalog folder. Below
<?php
} else { // default page
?>

Implementing multi languages with CSS and PHP

I'm building a small site, that needs to support 2 languages for the same page.
Each language has different buttons on the page, the buttons are basically some images with text inside.
The positioning of the buttons is done using a CSS file, one for every language.
Question is how do I implement the changing of button for every image. I could put a php if statement, and use some other src if lang == English or something else if lang == Russian, Or could implement this with the css file (I could do this with a div and set it's background in the CSS)? What would you recommend?
Cheers.
You could create three CSS files:
One for english buttons
One for russian buttons
One for the rest
This would basically work this way: You create your buttons with a fallback name, for example
<button id="button_ok">OK</button>
and define different background images in your english/russian CSS files:
(english.css)
#button_ok {
background-image: url(images/buttons/eng/ok.gif);
}
and (russian.css)
#button_ok {
background-image: url(images/buttons/rus/ok.gif);
}
All other elements that do not change when the language is chosen come in the third file:
p {
font-size: 1em; /* whatever*/
}
The last step is to choose at the top of every page which file you want to load:
if ($_GET['lang'] == 'eng')
$cssFile = 'english.css';
elseif ($_GET['lang'] == 'rus')
$cssFile = 'russian.css';
and include the special and the general css file in your head:
<link rel="stylesheet" type="text/css" href="general.css" />
<link rel="stylesheet" type="text/css" href="<?php echo $cssFile; ?>" />
I would recommend using CSS by setting the background and size of the button.
use < button> tag, its better because you can use css like "button{}" without having to put a class on it. Just a remind that < button> has a type like the input and it is "submit" as default if I remember correctly.
Btw, always try to depend on CSS instead of PHP, because on a next manutenance you will be glad that you did it.
or, if you going to use PHP, try using a function instead of hardcoding, even if it is just for one button, we never know if we will use it later.
hope it helped,
Joe

How to handle this CSS issue with Wordpress?

I have a (probably not) unique issue with a css background div I am seeking advice on. I am using Wordpress, which creates my pages dynamically. The front page and some other pages are using one type of background (Gradient) while internal pages are using a solid white. Right now I am forced to have two style sheets - main.css for the gradient background, then internal.css for the internal - just for this background div.
Is there a way to use one css file and handle these two background divs easily? I will probably need to use a bit of php...
Essentially I am only trying to pass two different background divs, on either home or some internal pages.
Just use different template files (which you should be doing anyway because of the different looks), and use something like an ID on the body tag to check like this:
<body id="grad">
...
</body>
or
<body id="white">
...
</body>
And use this in your stylesheet:
#grad {
background-image:url(something.png);
}
#white {
background-color:#FFF;
}
Make sure to check out the template hierarchy page in the WordPress codex to see how you can easily create the template files you need. Use #grad in home.php and/or a custom template file that you apply to your front page (if it's static), and then use #while in everything else (category.php, tag.php, single.php, and page.php are probably the basics).
You could use your normal stylsheet on all the pages, with the solid white background set. Then on your front page and other 'special' pages, you could have a tag with the background image that will override the white:
<head>
<link rel="stylesheet" type="text/css" href="main.css" /><!-- This has background-color:white; -->
<?php if(!empty($special)){
echo <<<HTML
<style>
body{
background-color:transparent;
background-image:url('image_url');
}
</style>
HTML;
?>
</head>
Then you'd just set $special to true or something when you're on a 'special' page.
I didn't think of this but here is the code:
<body<?php if ( !is_home() ) echo ' style="background-image: url(images/about_bg.png);"'; ?>>
Put it in the header.
<?php
if(is_home) {
echo '<div class="bg for main page">';
} else {
echo '<div class="bg for internal page">';
}
?>

Categories