I have a index.php file which loads (require) 2 different files based on a condition.
Visiting this link will cause;
mysite.com/index.php will load stats.php (require("stats.php");)
Visiting this link will cause;
mysite.com/index.php?auth="jgbsbsbasm" will load encry.php (require("stats_encry.php");)
Done with this code:
<?php
if(isset($_GET['auth'])) require("stats.php");
else require("stats_encry.php");
?>
This works fine. Now my question is; I have a CSS file in the header as static;
<link rel="stylesheet" href="cv.css">
What I want is now to load cv.css file for stats.php and cv1.css for stats_encry.php respectively.
How do I do this?
In your header replace
<link rel="stylesheet" href="cv.css">
with
<?php if(isset($_GET['auth'])){ ?>
<link rel="stylesheet" href="cv.css">
<?php } else { ?>
<link rel="stylesheet" href="cv1.css">
<?php } ?>
You can use this like below syntax
<?php
if(isset($_GET['auth'])){
echo '<link rel="stylesheet" href="cv.css">';
} else {
echo '<link rel="stylesheet" href="cv1.css">';
}
?>
Related
I have a site in php with account. I want to write a CSS for "profile.php/user" (for example: profile.php/benjamin). I tried to put this line in profile.php :
<link rel="stylesheet" type="text/css" href="profile.css">
But obviously doesn't work.
profile.php
<?php
session_start();
if($_SESSION['logged'] == true){
$username = $_SESSION['login_username'] ;
$username = strtoupper($username); //il facem uppercase
echo "Welcome ".$username;
echo "</br>Press here to go to the home page";
echo "</br>Click <a href='/logout.php'> here </a> to log out.";
}
else {
echo " </br></br></br>You must login first to see this section ";
header("refresh:4;url=/index.php");
}
?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <!--scalare pagina in functie de dispozitiv -->
<link rel="stylesheet" type="text/css" href="profile.css">
<html>
<body>
<div class = "top_band"></div>
</body>
</html>
Assuming the CSS file is in the same directory as the script, it should be:
<link rel="stylesheet" type="text/css" href="../profile.css">
This is because as far as the browser is concerned, profile.php/ is a directory in the URL. To get back to that same level, you have to go up with ../ in the URL.
The browser doesn't know anything about the actual file locations, and which parts of the URL are directories, filenames, and parameters, it just parses it textually. / simply separates directory levels, and ../ removes a trailing directory level when parsing a relative URL.
This question already has answers here:
Relative URL issue
(3 answers)
Closed 7 years ago.
This is how my header.php looks like;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="headerStyle.css">
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.2.min.js"></script>
<script src="headerScript.js"></script>
<?php
if( strpos($_SERVER['PHP_SELF'], 'index') ){
echo '<title>My website title</title>';
echo '<link rel="stylesheet" type="text/css" href="index.css">';
echo '<script src="index.js"></script>';
}
if( strpos($_SERVER['PHP_SELF'], 'index') ){
echo '<title>Contact Us</title>';
}//And goes on like this..
?>
</head>
header.php,headerStyle.css,headerScript.js are in same folder : mysite/header/
And this how my index.php looks like;
<?php
include 'header/header.php';
//And some other irrelevant stuff (<body> etc.)
?>
My index file's path is mysite/
The problem is when I view the index page it cannot load headerStyle.css and headerScript.js.
What is the best way to manage that?I'm trying to make a solid,dynamic header.php so i can include it for every page in my site.
You can define a constant and make use of __DIR__ magic constant.
define('HEADER',__DIR__.'/');
<link rel="stylesheet" type="text/css" href="<?php echo HEADER; ?>headerStyle.css">
<script src="<?php echo HEADER; ?>headerScript.js"></script>
How can I have certain CSS load only if a specific PHP variable is true and have other CSS load of the variable is false?
Have it in 2 separate files and have the correct file load depending on the boolean state or use if statements to echo the code you want within the document depending on the boolean state.
<style type='text/css'>
<?php
if(boolean)
{
echo "elementName { attribute: value; } ";
echo "elementName2 { attribute: value;} ";
}
else
{
//echo css here
}
?>
</style>
or, in the head tag:
<?php
if (boolean)
{
echo "<link rel='stylesheet' type='text/css' href='1.css'>";
}
else
{
echo "<link rel='stylesheet' type='text/css' href='2.css'>";
}
?>
Also, form vs content; it would be more practical to keep your CSS in a separate file and have a particular CSS script load depending on a variable rather than mix up the CSS with the HTML.
<link rel="stylesheet" type="text/css" href="<?php echo ($var) ? "css1" : "css2"; ?>.css">
In your HTML page, on the <head> section, you can write some simple conditions in PHP to include or not a CSS:
<?php
if ($yourCondition)
{
?>
<link rel="stylesheet" type="text/css" href="http://path/to/css1.css" />
<?php
}
else
{
?>
<link rel="stylesheet" type="text/css" href="http://path/to/css2.css" />
<?php
}
Note: Avoid using inner style, because all style should be in CSS (presentation layer separated from the HTML which is a structure and data layer)
My idea is to switch multiple links to CSS files, if special URL was detected. But I have a trouble: my code includes only css in the first if..else statement. And it doesn't depend on the URL.
Here is my code.
http://pastebin.com/Jm3QFDmH
ported from pastebin
<?php
// get first folder in URL
$f_folder = substr(substr($_SERVER["REQUEST_URI"],1), 0, strpos(substr($_SERVER["REQUEST_URI"],1), "/"));
//get full directory structure from URL for current page
$full_path = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
//css for account
if ($f_folder='account') {
?>
<link rel="stylesheet" href="/mainstyle/common.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="/mainstyle/account.css" type="text/css" charset="utf-8" />
<?php
} elseif ($f_folder='signin'||$f_folder='signup'||$full_path='/account/resetPassword'||$full_path='/account/logout') {
?>
<link rel="stylesheet" href="/mainstyle/login-signup.css" type="text/css" charset="utf-8" />
<?php
} else {
?>
<link rel="stylesheet" href="/mainstyle/common.css" type="text/css" charset="utf-8" />
<?php
}
?>
Where is mistake?
if and elseif, should have == comparison operator , not = assignment
Line 7 should be
if ($f_folder=='account') {
I am using multiple stylesheets and need the pages to differ based on category.
I added the following to my header.php, but shows base theme`s single entry template. Any ideas?
<?php if (is_category('20')) { ?>
<link rel="stylesheet" type="text/css" href="wp-content/themes/tanzaku/style.css" />
<?php } else {?>
<link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/style.css" />
<?php } ?>
I am using the same theme as these sites, but I use two themes on my site compared to these.
http://marioortega.net/
http://atelier6.co.uk/
When you click one of the thumbnails, the single ends up on top and the thumbnails at the bottom.
All:
Thank you for looking into this. In order for me to get this to work, the problem was actually multiple single.php files. This can be solved by having your single.php look like this,
<?php
$post = $wp_query->post;
if ( in_category('20') ) {
include(TEMPLATEPATH . '/single1.php');
} else {
include(TEMPLATEPATH . '/single2.php');
}
?>
I have also edited the question for other people looking for this answer.
Shouldn't you be using echo statement to put the link into your page.
<?php
if (is_category('20')) {
echo '<link rel="stylesheet" type="text/css" href="wp-content/themes/tanzaku/style.css" />';
}
else {
echo '<link rel="stylesheet" type="text/css" href="'.bloginfo('template_url').'/style.css" />';
}
?>