Jump to content

PHP include function


simi_id

Recommended Posts

Hi,

 

I have a.php .. with head and body, in head I have a.css which applies only to a.php

In b.php I will have also head and body but b.css which I want to apply only to b.php, and to include a.php, but I have to add as well a.css into b.php, else it's not working.

 

How can I have to separate files a.php with a.css and b.php with b.css and just to include them in c.php using just include and nothing more ?

 

Thanks

Link to comment
Share on other sites

Hi,

 

I have a.php .. with head and body, in head I have a.css which applies only to a.php

In b.php I will have also head and body but b.css which I want to apply only to b.php, and to include a.php, but I have to add as well a.css into b.php, else it's not working.

 

How can I have to separate files a.php with a.css and b.php with b.css and just to include them in c.php using just include and nothing more ?

 

Thanks

 

 

a.php contains:

<head>

  <link href="a.css" rel="stylesheet" type="text/css" />

</head>

<body></body>

 

b.php contains:

<head>

  <link href="a.css" rel="stylesheet" type="text/css" />

  <link href="b.css" rel="stylesheet" type="text/css" />

</head>

<body></body>

 

 

But you also want to include a.php in b.php?

Link to comment
Share on other sites

a.php contains:

<html>

<head>

  <link href="a.css" rel="stylesheet" type="text/css" />

</head>

<body>content a</body>

</html>

 

b.php contains:

<html>

<head>

  <link href="b.css" rel="stylesheet" type="text/css" />

</head>

<body>content b</body>

</html>

 

c.php to contain only:

<html>

<head>

<!-- no more css included in head each included file to use it's own css style included in their head section -->

</head>

<body>

  <?php require("a.php"); ?>

  <?php require("b.php"); ?>

</body>

</html>

 

the ideea is that in the final document just to include files worked alone, not to be necessary to include css styles, and to mess one with another.

Link to comment
Share on other sites

No. It's just for a simple web page.

I want to develop all content in independent pages, like menu.php, content.php, footer.php, and so on ... and then just to place them in one single file.

What I want is that single file where all are included to not require to add css in head. Each file included to use it's own css already defined in it own head.

Link to comment
Share on other sites

//text files in valid HTML markup.
$head = file_get_contents('head.txt');
$menu = file_get_contents('menu.txt');
$content = file_get_contents('content.txt');
$footer = file_get_contents('footer.txt');

//php files with valid syntax, and variables already set.
include 'head.php';
include 'menu.php';
include 'content.php';
include 'footer.php';

//using a heredoc to build the page.
echo <<<HEREDOC
<html>
<head>
{$head}
</head>
<body>
   <div id="menu">{$menu}</div>
   <div id="content">{$content}</div>
   <hr />
   <div id="footer">{$footer}</div>
</body>
</html>
HEREDOC;

Link to comment
Share on other sites

EDIT beaten to it :)

 

You do not need to add the basic html structure for each page that is included. Only the html,head,body tags should be present within the parent file (c.php) that is including the files

 

Here is an example of how to setup your files

Index.php - contains the basic page structure

<html>
<head>
   <link href="main.css" rel="stylesheet" type="text/css" />
   <link href="menu.css" rel="stylesheet" type="text/css" />
   <link href="content.css" rel="stylesheet" type="text/css" />
   <link href="footer.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <h1>Your title</h1>
   <?php require("menu.php"); ?>
   <?php require("content.php"); ?>
   <?php require("footer.php"); ?>
</body>
</html>

 

menu.php contains only html for the menu

<div id="menu">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
</div>

 

content.php only the html to display the contents

<div id="content">
    <h2>Content Title</h2>
    <p>Your content here</p>
</div>

 

footer.php only the html for the footer

<div id="footer">
    <h4>Your footer here</h4>
</div>

 

 

Link to comment
Share on other sites

Thanks guys, nice community in here.

 

I finaly choosed the last solution to implement.

Initialy I wanted that that menu.css to be in menu.php and so on, but I understood that there can be only 1 header even I include 1 file there can't be 2 headers only if I do frames if this is correct.

 

Thanks again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.