php virtual (''); works but php include''; does not - php

I'm trying to understand the difference between the following. All of the reading I've done says that 'include' should work. I'm wondering 'why' I have to use the 'virtual()' and/or WHAT I'm doing wrong with the 'include'.
<?php virtual('/path'); ?>
and
<?php include'/path'; ?>
I use both in the code below. The 'virtual()' works and is used for header.php. The 'include' (does not work) and is used for footer.php. The code for the entire page is below.
Link to live page
<head>
<title>Untitled Document</title>
<link href="/student_sites/2013/web_40/pages/css_includes/css_includes.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<div id="header"><?php virtual('/student_sites/2013/web_40/pages/css_includes/assets/includes/header.php'); ?>
</div>
<div id="content_wrapper">Content for New CONTENT WRAPPER Div Tag Goes Here
<div id="side_bar">Content for New SIDE BAR Div Tag Goes Here</div>
<div id="content">Content for New CONTENT Div Tag Goes Here</div>
</div>
<div id="footer"><?php include '/student_sites/2013/web_40/pages/css_includes/assets/includes/footer.php'; ?></div>
</div>
</body>
</html>

<?php include'/path'; ?>
Doesn't work because of the type of file it's in. This statement must use the 'virtual()' which only works in php files. The 'include' only works in html or shtml files.
<!--#include virtual="/path" -->
for html files and
<?php virtual('/path'); ?>
for php files.
The html code still uses virtual but include as well, so I'm still not sure why it can't use include by itself.
-your 2ed period stutent

Related

PHP and includes

I am creating my first website from scratch and had seen something where you can reduce code by using PHP includes for sections of the site that are to be repeated. So far, I have a head.php (which I added due to my stylesheet.css being linked there and needing access to it on every page), header.php, footer.php, index.php, and other pages with the php extension (about, contact, that bunch).
Everything is appearing where I'd like it to except for one issue: when setting the body background color, everything (all includes: header.php, footer.php) seems to be in the body. I tested this by setting a border around the body, and it confirmed what I thought.
Does anyone have an idea what is going wrong? I am using flexbox in my header, footer, and other bits within the index file, but I don't think that should be affecting anything.
<!DOCTYPE html>
<?php include 'head.php'; ?>
<?php include 'header.php'; ?>
<body id="main-block">
<!-- Button links to Portfolio and Other stuff -->
<div class="flex-container">
<a class="main-button" href="#">Web Work</a>
<a class="main-button" href="#">Other Work</a>
</div>
</body>
<?php include 'footer.php'; ?>
</html>
your body tag is suppose to wrap around your header and footer elements. see below markup
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<?php include (header);?>
<?php include (footer);?>
</body>
</html>
if u wanna change the background color of your elements just add css
element { background-color:pink }
I admit that I do not really understand your issue. But I'll give you a little snippet to start coding php:
<html>
<head>
<title>Welcome</title>
<style>
<?php include 'stile.php'; ?>
</style>
</head>
<body>
<?php include 'header.php'; ?>
<?php include 'footer.php'; ?>
</body>
</html>

How to create a footer once and then use it in all my pages

The question is simple, but maybe the answer not.
I am wondering how can copy and paste my <footer>CONTENT HERE</footer> created by Bootstrap in all my pages automatically.
Is there some easy way to do that? Because on the Internet I only can read very complex ways.
Thanks
You seem to be confusing how the page gets rendered. CSS is used for styling content which is already there provided by the HTML, so Bootstrap cannot help you here. Bootstarp did not "create" the content of the footer, it jsut gives it some styling.
Depending on the back-end technology you use for generating pages (maybe Jade, EJS, PHP?), you need to do some sort of inclusion of the footer template.
Here is a sample structure of how to include the layout structure in HTML page.
<!DOCTYPE html>
<html>
<head>
<?php include('includes/meta.php'); ?> <!--[if IE]>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<![endif]-->
<?php include('includes/page_title.php') ?>
<!-- BOOTSTRAP CORE STYLE -->
<?php include('includes/header_scripts.php'); ?>
</head>
<body>
<?php include('includes/header.php'); ?>
<!-- LOGO HEADER END-->
<?php include('includes/header_menu.php'); ?>
<!-- MENU SECTION END-->
<div class="content-wrapper">
<div class="container">
<div class="row pad-botm">
<div class="col-md-12">
<h4 class="header-line">Blank Page</h4>
</div>
</div>
</div>
</div>
<!-- CONTENT-WRAPPER SECTION END-->
<?php include('includes/footer.php'); ?>
<!-- FOOTER SECTION END-->
<!-- JAVASCRIPT FILES PLACED AT THE BOTTOM TO REDUCE THE LOADING TIME -->
<!-- CORE JQUERY -->
<?php include('includes/footer_scripts.php'); ?>
</body>
</html>
footer.php
<footer>CONTENT HERE</footer>
Like this if you put a separate file and call it in all the other pages and it will be created dynamically in all the pages if you edit or change in this file alone.
Like the above way you need to create all the pages and include it into your project file.
Folder Structure
Project Folder
includes(folder)
header.php
footer.php
index.php(file)
You can accomplish this with jquery.
Place this code in index.html
<html>
<head>
<title></title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(function(){
$("#footer").load("footer.html");
});
</script>
</head>
<body>
<div id="footer"></div>
</body>
</html
Here is a stack overflow page that can help you with this
Also if you are using php this will be more simple
<html>
<body>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body>
</html>
Just create your footer in footer.php
Here is w3schools post if you want to know more about php and how to include this.

Load php files into layout template?

I'm working on my first php site, I'm running into an issue I can't see to figure out. I'm trying to have one php page that contains my structure, and others that inject their html inside, while retaining url changes so I can still direct link pages.
So far this is what I'm doing, but it doesn't seem efficient:
index.php
<html xmlns="http://www.w3.org/1999/xhtml">
<?php include("head.php"); ?>
<body>
<div class="container">
<!-- Navigation header -->
<?php include("navigation.php"); ?>
<!-- Main container -->
<div id="MainContainer">
<?php include("home.php"); ?>
</div>
<!-- Footer -->
<?php include("footer.php"); ?>
</div>
</body>
</html>
about.php
<html xmlns="http://www.w3.org/1999/xhtml">
<?php include("head.php"); ?>
<body>
<div class="container">
<!-- Navigation header -->
<?php include("navigation.php"); ?>
<!-- Main container -->
<div id="MainContainer">
About me!
</div>
<!-- Footer -->
<?php include("footer.php"); ?>
</div>
</body>
</html>
This feels totally wrong, if I ever want to change my container class, or change the structure, I now have to do it in two places instead of one.
In ASP.net MVC I would have a Layout_Head.cshtml file that would contain my HTML structure and inside I can render views from different pages, the url changes but the layout is always rendered first and then the controller/actions take care of injecting the html of the needed views.
How do I replicate that in PHP?
Usually people use php includes for templating more like this:
header.php
<html>
<head>
<title></title>
</head>
<body>
<div class="container">
footer.php
</div> <!-- .container -->
</body>
</html>
about.php
<?php include('header.php'); ?>
... content goes here ...
<?php include('footer.php'); ?>
This is so you don't need to continuously repeat the start/end tags on every template you make.

Why "inspect element" shows different code?

I have a php page where I have the code posted below. My main problem was that in the header there was a line added at the top. This line was not added at the other .html files of my page that used exactly the same code. The only difference was the extension (html and php).
I tried "inspect element" feature to see what is going on. And I can see a different code in the <head> and the <body>.
My page code :
<!DOCTYPE html>
<?php
session_start();
include("conf.php");
$current="gallery.php"
?>
<html lang="en">
<head>
<title>.......</title>
<meta charset="utf-8">
<link rel="stylesheet" media="screen" href="......css">
<!-- JS -->
<script src=".................."></script>
</head>
<body>
<!--==============================header=================================-->
<header>
<div class="container_12">
<div class="grid_12">
<h1><img src="images/logo.png"> </h1>
<div class="menu_block">
<nav>
<ul class="sf-menu">
<li>ΑΡΧΙΚΗ </li>
...
...
<li>ΕΠΙΚΟΙΝΩΝΙΑ </li>
</ul>
</nav>
<div class="clear"></div>
</div>
</div>
</div>
</header>
...
...
</body>
What causes that ? I have the same html code (and css) in the other pages (different extension though) like this one : HTML version
Your webserver is set up to only interpret files with .php extension as PHP scripts. Whenever you give the page the .html extension, it will just send the file to the browser to interpret, without parsing it beforehand. So everywhere you use php code you should give the file the .php extension (or change the webserver's configuration, but I wouldn't do that now).

web page slightly altered from index page

The question is fairly simple, yet I've been looking around for an hour and found nothing:
make a page that is exactly the same as the home page, but a specific div has altered content
example index.html:
<html>
<head>
<title>title</title>
<style type="text/css">
/* css goes here */
</style>
</head>
<body>
<div id="stay">I wont change</div>
<div id="change">I will change</div>
</body>
</html>
so I want to be able to code a page so that it inherits the entire html from the index page (WITHOUT COPYING THE CODE), but a specific div (here with the id #change) to have different content. How would I go about doing this?
You don't really "inherit" code snippets, but I understand that you're trying to reuse the page content. From your posted code, it's hard to tell exactly how the change differs from the index. Is it just a content change or does the index page not have that div?
You have a couple of options. If just the content of the div is changing, you could use the same php page and then use jquery to change the content of the div, so something like
index.php
<? php include("page.php"); ?>
other page
<? php include("page.php"); ?>
// javascript to modify div
You could break the page into chunks and just include them as needed, so you could have a top.php and a bottom.php, and the index page could do
<? php include("top.php"); ?>
<? php include("bottom.php"); ?>
And then your similar page could do something like
<? php include("top.php"); ?>
// custom stuff here
<? php include("bottom.php"); ?>
If neither of these solutions work you could always use a templating engine to create a page template, though that may be a little much for your situation.
I see you have tagged this question in php So, I will give you answer inclusive of php implementation.
Create 3 pages. index.php about.php and foo.php
The objective is to show some content in index.php but all content in about.php
Call this page foo.php
<html>
<head></head>
<body>
<p> Show this in index.php </p>
<?php if($_SERVER['PHP_SELF'] === 'about.php'): ?>
<p> Show this in about.php </p>
<?php endif; ?>
</body>
</html>
Now, all you have to do is ... include foo.php in both pages.
Make the page you want and you can go about doing this:
<html>
<head>
<title>title</title>
<style type="text/css">
/* css goes here */
</style>
</head>
<body>
<div id="stay">I wont change</div>
<?php
if(basename($_SERVER['PHP_SELF'] == "other-page.php")){ ?>
<div id="change">I will change</div>
<?php }else{ ?>
<div id="change">Original div</div>
<?php } ?>
</body>
</html>
That takes the file name and based on that you can change content (if is only for one page, otherwise write a function/class based on that).
There are many ways to do this. Here are two, each with their own advantages and disadvantages.
Firstly, if you don't want to modify the page at all, you can add a small PHP code segment which will include a page passed in through the GET variable. For example
<html>
<head>
<title>title</title>
<style type="text/css">
/* css goes here */
</style>
</head>
<body>
<div id="stay">I wont change</div>
<div id="change"><?php require($_GET['page']); ?></div>
</body>
</html>
would mean that using the URL mypage.php?page=home.php would automatically include the contents of a file called home.php into that div.
Another way to do it is to divide up that page into 2 sections, and including both of them in any other page you use. For example, splitting the code into 2 seperate files, such as
top.php:
<html>
<head>
<title>title</title>
<style type="text/css">
/* css goes here */
</style>
</head>
<body>
<div id="stay">I wont change</div>
<div id="change">
bottom.php:
</div>
</body>
</html>
then in your PHP file you can use the following
require("top.php);
MY CONTENT HERE
require("bottom.php);
remember that you will need to use echo to output html code on this method if it is within <?php and ?> tags
hope this helps.
You can't do this will plain HTML.
To do it in php, first create template file like so: (template.php)
<html>
<head>
<title>title</title>
<style type="text/css">
* css goes here */
</style>
</head>
<body>
<div id="stay">I wont change</div>
<div id="change"><?=$main_content?></div>
</body>
</html>
Now, let's say you want to make a "contact me" page.
<?php
// in contact.php
$main_content = "Contact me at my#email.com
include "template.php";
?>
This will write the contents of template.php to the page and echo out the value of $main_content inside div#change
Now, this is generally frowned upon because managing your variables becomes difficult as the size of the template increases. To keep things sane, use a templating engine as all of the other answers are suggesting.

Categories