John Davidson

html - Linking CSS file in php document nested in pages folder

0 comments
Message:


Web development newbie here. My site is becoming large enough that recently took a few steps to help manage it including:



  1. Using a separate header.php; footer.php; and navigation.php file with an includes tag.

  2. Filed additional pages under root/pages. Below is a snapshot of my current folders:



  • root

    • pages

    • assets

    • images

    • js

    • php




My issue now that I have nested additional pages (other than index.html) in the pages folder. When coding these files, the link/include files are causing issues, because of the relative/absolute file reference. Through some research I was able to use this php code which seems to work for the header, footer, and navigation menu:


<?php 
set_include_path( implode( PATH_SEPARATOR, array(
$_SERVER['DOCUMENT_ROOT'],
get_include_path()
) ) );

include 'root/assets/header.php';

?>

The issue I have now is the css link no longer works either:


 <link rel="stylesheet" href="css/style-services.css">

I have tried doing this with no luck:


<?php 
set_include_path( implode( PATH_SEPARATOR, array(
$_SERVER['DOCUMENT_ROOT'],
get_include_path()
) ) );

include 'root/css/style-services.css';

?>

How do I insure the referenced css also gets included when using a file structure and php include functions?



My Answer:
php



This code allows you to include the header.php file from the root directory in any file within the pages folder.

To link a CSS file in a PHP document nested in the pages folder, you can use a similar approach. Assuming your CSS file is located in the assets folder, you can use the following code:

html



This code will link the style.css file from the assets folder in the root directory to your PHP document in the pages folder. Just make sure to adjust the file path accordingly if your CSS file is located in a different directory.

Rate this post

5 of 5 based on 2633 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us