Reputation: 834
A few of my CSS pages will negatively interact with each other if they were all listed on every page of my site. The goal is to remove entire head section into a header include file to clean up the pages. I've tried to use a php script to look at SERVER's Request URI variable to detect current page and then use that in an if statement to pick the css link tags needed for that page. Here's what I tried:
<?
$currentLocation = $_SERVER['REQUEST_URI'];
if ($currentLocation == '/index.php' || $currentLocation == '/' || $currentLocation == '')
{
echo '<link href="css/signin.css" rel="stylesheet">';
} elseif ($currentLocation == '/signup.php') {
echo '<link href="css/signin.css" rel="stylesheet">';
} elseif ($currentLocation == '/anotherPage.php') {
echo '<link href="css/anotherPageCSS.css" rel="stylesheet">';
}
?>
Is this a decent approach? Do you see any errors in it? Please advise.
I've also considered breaking the CSS pages down into one stylesheet, and use id attributes to target instead of having tag selectors. What do you guys recommend?
Upvotes: 0
Views: 104
Reputation: 4104
Here is a basic example of a way to utilize page keying with a small class. Whenever you want to setup output for html, you choose which pagekey it should build from (that way, your urls and php filenames can change without worry):
<?php
// include_handler.php
class IncludeHandler {
private $pagekey = 'basic';
private $cssfiles = array();
private $jsfiles = array();
function __construct($key) {
$this->pagekey = $key;
$this->cssfiles = [ // define all css files to pagekeys
'home' => array('home.css'),
'basic' => array(),// if none needed
'accounts' => array('accounts.css'),
'checkout' => array('accounts.css','checkout.css'),
];
$this->jsfiles = [ // define all js files to pagekeys
'home' => array('home.js'),
'basic' => array(),
'accounts' => array('accounts.js'),
'checkout' => array('accounts.js','checkout.js'),
];
}
public function headlinks() { // call this in your html output <head> area
$html = '';
foreach ($this->cssfiles[$this->pagekey] as $cssfile) {
$html .= '<link type="text/css" rel="stylesheet" href="/css/'. $cssfile .'" />';
}
foreach ($this->jsfiles[$this->pagekey] as $jsfile) {
$html .= '<script type="text/javascript" src="/js/'. $jsfile .'"></script>';
}
return $html;
}
}
?>
Example usage from like a checkout page:
<?php
// checkout.php
require_once(__DIR__.'/include_handler.php');
$htmlincludes = new IncludeHandler('checkout');
// processing code
// html output area
?><html>
<head>
<link type="text/css" rel="stylesheet" href="/css/global.css" />
<?PHP echo $htmlincludes->headlinks();?>
</head>
<body></body>
</html><?PHP
?>
Upvotes: 2
Reputation: 377
That is one way how to do it. I would suggest using some kind of dictionaries for it (associative arrays in PHP)
Something like
<?php
$currentLocation = $_SERVER['REQUEST_URI'];
$styles = [
"/index.php" => "first.css",
"/" => "first.css",
"/another.php" => "second.css",
//...
];
if (in_array($currentLocation, $styles))
{
$link = "css/" . $styles[$currentLocation];
echo "<link rel=\"stylesheet\" href=\"$link\"/>
}
?>
Just my opinion :)
Upvotes: 1