0

Is it possible to call a different css file depending on which dynamic page you're on?

For example if you start at index.php -> You then click a link that refers to index.php?p=apple

If you have a css file called index.css that is specific to index.php, can you have a css file that overides index.css in the event that index.php?p=apple is opened?

If there is no solution, i may just rewrite my css file to accommodate all the dynamic pages.

UPDATE: The following is some extract of my code.

Note: in the something.inc.php page is a link to index.php?p=apple

<head>  
    <link href="css/index.css" rel="stylesheet" type="text/css" media="screen" />
</head> 

<?php
$pages_dir = 'pages';

if (!empty($_GET['p'])) {
    $pages = scandir($pages_dir, 0); 
    unset($pages[0], $pages[1]);

    $p = $_GET['p']; 

    if (in_array($p.'.inc.php', $pages)) {
        include ($pages_dir.'/'.$p.'.inc.php'); 
    } else {
        echo 'Sorry, page not found'; 
    }

} else {
    include($pages_dir.'/something.inc.php');
}

?>

UPDATE 2: Used the solution marked as answer, and it's all working well. Thanks everyone for the help!

2
  • 2
    Of course it’s possible – but for an average site, I think it might be rather counter-productive, because with a separate CSS resource for each individual page you waste the opportunity of caching your CSS after the first page load … Commented May 22, 2013 at 11:00
  • Hmmm. By the looks of it, i think i need the link in the head to change depending on what the value p equals. Can i use something like this? if($_GET['p'] == 'apple') { echo '<link.....>'; } else {.....} Commented May 22, 2013 at 11:12

4 Answers 4

1
$link = (isset($_GET['p']))? $_GET['p'] : 'index';
if(file_exists(PATH_TO_CSS_FILES.$link."css")){
  echo "<link src='$link.css' media='all' rel='stylesheet' type='text/css'>";
} else {
  echo "<link src='index.css' media='all' rel='stylesheet' type='text/css'>";
}
Sign up to request clarification or add additional context in comments.

5 Comments

-1: page.php?p=' will cause error on this page. never trust user input!
and using $_GET['p'] without 'isset' will cause Notice: Undefined index: p.
just turn off notices XD fixed answer
@StefanDunn - If I could downvote a comment I would. Bad advice.
I've ended up using the solution i marked as answer. Don't know if it's the best solution, but it's definitely working for me. Thanks everyone for your help!
0

If your url are index.php?p=apple or index.php?p=orange

Then

if($_GET['p'] == 'apple')
{
   echo '<link href="css/apple.css" type="text/css" rel="stylesheet">';
} 
elseif($_GET['p'] == 'orange')
{
   echo '<link href="css/orange.css" type="text/css" rel="stylesheet">';
}

If your url are index.php or product.php

Then

$file = basename($_SERVER['REQUEST_URI']);
if($file == 'index.php')
{
   echo '<link href="css/apple.css" type="text/css" rel="stylesheet">';
} 
elseif($file == 'product.php')
{
   echo '<link href="css/orange.css" type="text/css" rel="stylesheet">';
}

1 Comment

Thanks very much! Tested this out and it worked. Not sure if it's the best way to go, but it works, and that's all i need for now!
0

One way i think is that you can generate the CSS link dynamically based on the querystring . You can have separate php file which will act as helper file and you can include in all the pages.

         if($_GET['p'] == 'a')
         {
                 echo LINK OF NEW CSS File
         } 
         else
         {
             echo Link of another CSS
         }

Comments

0

maybe...

$page = (isset($_GET['p'])) ? $_GET['p'] : false;
if ($page){
    $css = '';
    switch ($page) {
        case 'apple':
        $css = 'apple.css';
        break;
        //other options here
    }
//link to $css
}

And remember to use !important in all styles aimed to override original.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.