PHP includes file.php with CSS issues

I have a PHP index page where I include all PHP files such as index.php?page=example

. All pages are in a different folder, here is the structure:

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

      

The index calls the CSS file from css/style.php

.
Pages are called from index.php

like ( include pages/example.php

) using the GET function.

If I run index.php

, I have no problem with CSS, if I only run the included page, for example example.php

I get problems with CSS because the CSS is in index.php

and obviously won't display the CSS correctly.

But when I run index.php

and include index.php?page=example

, then the CSS index is displayed correctly, but the classes from the included pages don't work ...

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

Here is some sample 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 work fine, but the style class from the included pages doesn't work, they just aren't designed

+3


source to share


2 answers


You shouldn't write your css code in php file. Better create a css file and put your style directives in it. You can include CSS styles best by following conventions, create a basic html template like below and link to your css file and include php 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>

      

+5


source


Make sure you have a header ("Content-type: text / css"); as your first line in php file so that it displays correctly as css. Then don't include the file. Instead, discard it like a normal css file, only changing .css to .php. <link rel="stylesheet" href="part/to/file.php">

... This should get you working. I am assuming that you are pulling data from the database to populate your css, so make sure it is formatted correctly. Don't use something like .headertext{ color:<?=$row['headercolor'];?>;

. Instead, declare it in php tags. $color= $row['headercolor'];

... Then in the css part of the php file will call this variable. .headertext{ color:<?=$headercolor?>;

... Hope it helps



+1


source







All Articles