3

I have a index PHP page where I include all PHP files like index.php?page=example. All pages are in another folder, here is the structure:

public_html/index.php
public_html/css/style.php
public_html/pages/

Index calls the CSS file from css/style.php.
Pages are called from index.php like (include pages/example.php) using GET function.

If I run index.php I get no problems with CSS, if I run only the included page like example.php I get CSS problems because the CSS is in index.php and obviously will not show the CSS correct.

But when I run the index.php and include the index.php?page=example then the index CSS show correct but the classes from the included pages does not work...

I suppose the include will only import the code but it seems like something is wrong with the server or I am doing something wrong?

Here is a example code of what I am using. This is index.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
      <link rel="stylesheet" href="css/style.css"> 
  </head>
  <body>
        <?php 
        include('pages/example.php'); 
        ?>
  </body>
</html>

Index.php all css classes works fine but the style class from the included pages does not work they are just not styled

6
  • How you path to css look in style.php? /css/style.css or css/style.css? Commented Jul 12, 2015 at 17:55
  • @Happy Knight Add your index.php code... Commented Jul 12, 2015 at 17:57
  • You cannot include css files. css files are referenced from the client side inside the html head. The reference you put in there has nothing to do with how you include or call you php files. Commented Jul 12, 2015 at 17:57
  • <link rel="stylesheet" type="text/css" href="css/style.css"> Commented Jul 12, 2015 at 18:14
  • Edited my code question with a example Commented Jul 12, 2015 at 18:17

2 Answers 2

5

You shouldn't write your css code in a php file. Better create a css file and put your style directives in there. You can include css styles best by following conventions, create a basic html template like the following and link to your css file and include the php in there.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
      <link rel="stylesheet" href="part/to/file.css"> <!-- link your stylesheet here -->
  </head>
  <body>
        <?php 
        include('path/to/file.php'); // include your php code here
        ?>
  </body>
</html>
Sign up to request clarification or add additional context in comments.

3 Comments

I think this answer will help full try that
Edited my question, added your code as example because is the same thing. And is not working. Maybe because css and pages are in separated folders?
Completely agree with you and gave a thumbs up, but there are many reasons for which he could want a dynamic php stylesheet, so perhaps that should also be addressed. @michael
1

Make sure you have header("Content-type: text/css"); as your first line in php file so it renders correctly as css. Then do not include the file. Instead refrence to it like a normal css file only change the .css to .php. <link rel="stylesheet" href="part/to/file.php">. That should get you working. I am assuming your pulling data from a database to fill in your css, so make sure it is format correctly. Do not use something like .headertext{ color:<?=$row['headercolor'];?>; . Instead declare it in php tags. $color= $row['headercolor']; . Then in css part of php file call that variable. .headertext{ color:<?=$headercolor?>;. Hope that helps

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.