I am building a personal portfolio webpage as a school assignment for my study Information science at the university of Amsterdam. Right now I copy and paste my header to each new page I make, so I can make little changes which show people at which page they are. Copying and pasting everytime you make a new page is far from efficient. I want to include the header code with a php file. But how can I still make little changes per page then?
My page: http://annatol.nl/
I hope anybody can help me.
Thanks in advance,
Anna
In your main php script :
<?php
include_once('inc/header.php');
?>
In your inc/header.php script :
<?php
$page = $_SERVER['REQUEST_URI'];
switch($page) {
case "/projects.html":
//Do specific stuff for Projects page
break;
case "/otherpage.html":
//Do specific stuff for another page
break;
default:
//In other cases...
break;
}
?>
Or if you need inline changes, you can do something like this in your header.php script :
<?php
$page = $_SERVER['REQUEST_URI'];
?>
<a href="/projects.html" <?php if($page == "/projects.html") { echo "class=\"active\""; } ?>>My projects</a>
Something like this?
include 'header.php';
You can include all the files by using the include function
Example:
include "Path_To_File";
you can also use the require or require_once functions aswell
Related
I've been searching around for a good hour or so and haven't really found anything to answer my question.
The thing is, I understand how to make a page and do:
index.php
<?php
include 'head.php';
include 'nav.php';
include 'footer.php';
?>
But in my nav.php, I'm not sure how the works, and how to include different content pages within my index.php like: contact.php, registration.php, etc.
I don't know how make it so that if I were to click the "contact" navigation link for example, only the content would change, and I wouldn't have to make a whole separate HTML file like so: Contact
Hopefully I explained it good enough. If anyone can give me a reference or explain how to do this, I would greatly appreciate it. Thank you guys.
I quoted your answer, this is the line
I don't know how make it so that if I were to click the "contact"
navigation link for example, only the content would change, and I
wouldn't have to make a whole separate HTML file like so:
<a> href="bla.html">Contact</a>
it's not the way it should be, what i get from your question is, you wanted a full page is all the same
<?php include 'head.php'; include 'nav.php';
and only this content should be change, not the head, not the nav, nor footer.
include 'footer.php'; ?>
am I correct?
if it is what you wanted, you should place a php file whatever the name is, index.php would be better. and the link it self should be called with something that php can get it.
Contact US
and in your index.php, you also create a condition if else would be easier.
if($_GET['contact']){
place your html /php here
}
else if($_GET['about']){
place your html /php here
}
else{
include 'index.php';
exit();
}
I suggest you not to do this, everyone is now can do the code, but not the code architecture .
wish it help you to understand.
You need to use different views. I believe you are looking for a way to change the view using php. One way is to add the page id in the url like 'www.myweb.com/index.php?page=contact' and receive it as a $_GET['page']. I think this is the simplest way to change the view using php. And this is the index.php, put all the view file in the views folder.
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php
include 'head.php';
include 'nav.php';
switch($_GET["page"]){
case 'contact':
include 'views/contact.php';
break;
case 'registration':
include 'views/registration.php';
break;
case 'chat':
include 'views/chat.php';
break;
}
include 'footer.php';
?>
</body>
</html>
this would be your nav.php;
<ul>
<li> Contact </li>
<li> registration </li>
</ul>
I am new to PHP and programming in general and hope someone can help me with this.
I am working on building a website where the code for each page is saved in a separate file.
Now I have a couple of code snippets that I want to use on different pages but not on all of them.
My current approach is that I paste them on every file that uses them which duplicates code and therefore supposedly is not good practice.
On the other hand when I use snippets on all pages I have removed them from the single files and store them as separate includes files using PHP's include or require in order to include them on a page, e.g. for the header, the footer and the menu etc. - example:
require_once("includes/header.php");
This works well and I was wondering if there is a similar way I can include the other code snippets as well BUT without having to save each of them as a separate file.
Is there a way for example that I could use functions for this or is there some other common practice ?
Example (just to show what I mean):
<?php
// example of what I would like to include on different pages
echo "<button type="button" class="class1" id="btn1">Button 1</button><br />
<button type="button" class="class2" id="btn1">Button 2</button><br />
<button type="button" class="class3" id="btn1">Button 3</button><br />";
?>
The pieces to insert could be anything but usually they are some small PHP / HTML snippets, like a set of buttons or a div or a dropdown etc.
Make the code snippets inside a function so you can call it on any other page by including the same page everytime.
Let's say we have an index.php page, and we have a test.php that have this code (that we want to include on the index page):
<?php
function hello(){
echo 'Hello World!';
}
hello(); //to print "hello world!" in this particular page
?>
Now, in the index.php page, we put:
<?php
include('test.php');
?>
<h1><?php hello(); ?></h1>
use a single index.php file to handle the rest
index.php
<?php
require_once 'header.php';
if(isset($_GET['page']){
switch($_GET['page']){
case "123":
require_once 'snippet1.php';
break;
case "1234":
require_once 'snippet2.php';
break;
default:
require_once 'notfound.php';
}
}
require_once 'footer.php';
Forgive me as I am still kinda new at web development. :)
Normally when I create my websites I create a file called header.html and footer.html and just include this files into my webpages to be consistent also so that I only change data once then add whatever code is necessary.
(Eg: index.php)
<?php
include ('includes/header.html');
include ('html/index.html');
include ('includes/footer.html');
//other php codes
?>
Now my problem is that on one of my pages (food-menu.php), I have a sidebar that has got links that I have setup to retrieve the pages according to the links that are clicked - and the code that I am currently works in terms of retrieving dynamic content. My only problem is how can i setup a default content include on that page before it goes dynamic?
The sidebar goes like this:
//Sidebar
<section class="widget">
<h3 class="title">Main Menu</h3>
<ul>
<li>Cold Starters</li>
<li>Hot Starters</li>
<li>Charcoal Grilled</li>
<li>Chef's Special</li>
</ul>
</section>
then the main file is (food-menu.php :)
<?php
include ('includes/header.html');
$link = $_GET['link'];
if ($link == '1'){
include 'html/cold-starters.html';
}
if ($link == '2'){
include 'html/hot-starters.html';
}
if ($link == '3'){
include 'charcoal-grilled.html';
}
if ($link == '4'){
include 'chef-special.html';
}
include ('includes/menufooter.html');
?>
the retrieving of file works, but how do i setup a default include page? like setup the ('html/cold-starters') to be the first page when they go to the food-menu.php url? because so far ive only included the header and footer but not a default content page.
Thanks Experts! :)
You can amend your code this way:
$link = isset($_GET['link']) ? $_GET['link'] : 1;
What happens here is, if you have given the parameter of link, it takes it, else, it will take the default value of 1.
Id like to know what the best way of laying out a simple website,
I used switches so that my website would contain the same layout and then just have a little content area that changes as you go to
myserver/index.php?page=home
or
myserver/index.php?page=settings
Here is some code of my switch:
index.php:
<?php
session_start();
if (!isset($_SESSION['Username'])) {
header("Location: login.php");
}
include("config.php");
include("userinfo.php");
if (isset($_GET['page'])) {
$Page = $_GET['page'];
} else {
} switch ($Page) {
case "logout": {
require("logout.php");
include("layout.php");
break;
}
case "home": {
$PageTitle = "Home";
$PageFileName = "home.php";
include("layout.php");
break;
}
case "music": {
$PageTitle = "Music";
$PageFileName = "music.php";
include("layout.php");
break;
}
I basicly want to know if the above is a good system for haveing different pages with the same layout but a different content section?
I could also use php includes and include bits and pieces as I need, like a header and side bar, but Id like to know what the best system is? or just any advice or anything.
Thanks,
Jason Russell
I personally use the following structure:
main directory: has all the files
includes folder: has two files
Then I create a page, let's call it index.php
<?php
include("includes/header.php");
echo "<h1>Page title</h1>";
echo "<p>Page text...</p>";
include("footer.php");
?>
I then write a script for each page and keep it all separate. Your method above seems extremely complicated and you will get very very confused after not very long!
Also, the best way to split the header/footer is to make the layout as you want it in one file. Then locate the beginning of the main DIV and the end of the main DIV and put the top part in the header, bottom part in the footer.
I tried to keep that as simple as possible. I hope that helps :)
Well, I can assure you that a huge switch/case is not the solution. Look into MVC.
I have a small situaton here. I'm building a custom CMS for one of my websites.
Below is the code for the main index page:
<?php
require("includes/config.php");
include("includes/header.php");
if(empty($_GET['page'])) {
include('pages/home.php');
} else {
if(!empty($_GET['page'])){
$app = mysqli_real_escape_string($db,$_GET['page']);
$content = mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM pages_content WHERE htmltitle = '$app'")) or die(mysqli_error($db));
$title = $content['title'];
$metakeywords = $content['htmlkeywords'];
$metadesc = $content['htmldesc'];
?>
<h1><?php echo $content['title']; ?></h1><hr /><br />
<div id="content"><?php echo $content['content']; ?></div>
<? } else { include('includes/error/404.php');} }
include('includes/footer.php'); ?>
The file, includes/header.php contains code to echo variables, such as, page title and meta stuff.
The issue is that when the include("includes/header.php"); is where it is, outside of the if conditions, it will not echo the varables, obviously, however, I can't put the include in the if condition otherwise, the home page, which does not require any url variables will show without these conditions.
What do I do?
You can't really write code like this for too long. It's ok to for start, but you will soon realize it's hard to maintain. The usual way is to split it into a few steps.
First check input and determine on which page are you
If you know you are on the homepage, include something like includes/templates/homepage.php
Otherwise try to load the page from the database
If it worked, include includes/templates/page.php
Otherwise include includes/templates/404.php
Each of the files in includes/templates will output the whole page, i.e. they all include the header, do something and include the footer. You can use for example Smarty templates instead of PHP files, which will make the approach obvious.
Once you have this, you can split the code even more. Instead of loading the page directly from index.php, include another file which defines a function like load_page($name) and returns the page details.
Then a few more changes and you realize you are using the MVC approach. :) The functions that load data from the database are your Models, the Smary templates are Views and the PHP files that put them together are Controllers.