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.
Related
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
How can I make PHP pages appear inside a div called contenct, whenever a menu button is pressed?
For example I should bring product menu product.php page to the document body and head, footer will continue well without causing alteration.
I've tried include, but how could it put the link from appearing on the container div?
I hope I have understood!
leave a sample image:
You can use PHP's include() for that purpose.
An example:
<?php
include('head.php');
include('menu.php');
//your normal body code goes here
include('footer.php');
?>
include('file.php'); will read the contents of the php file and it will add / paste the contents in the main file. In this particular example, it will load the contents of head.php, menu.php, and footer.php.
Hope this helps!
Based on your question, im assuming you would like to page to NOT refresh when the navigation button is pressed, and to simply load the contents of a php file into the content area.
The smart way to do this would be to look into using jquery with AJAX, or LOAD to load external content as needed.
PHP has no direct way to manipulate the clients browser without refreshing or loading another page.
if you must do this in php, a very simple approach would be to have each menu item link to a query string. I.E.
HTML: menu.php
Home
Products
Clients
Contact
and to have the index.php file itself run a switch and case I.E.
PHP: index.php
<?php
if(isset($_GET['page']) && $_GET['page'] != '' ){
$page = $_GET['page']; // page being requested
}else{
$page = 'home'; // default page
}
include('head.php');
include('menu.php');
// Dynamic page based on query string
include($page.'.php');
include('footer.php');
?>
This is a very basic example, but might get you pointed in the right direction.
hope this helps
In the php file the user is viewing, put:
include('home.php');
this will execute the contents of the home.php file in the current php file
You could use AJAX and on the event's finish use result $('#content').empty().append(result)
I'd like to add AMAL's answer...
<?php
require('function.php');
include('head.php');
include('menu.php');
//Create a function here to fetch pages
echo '<div id="content">';
echo get_page();
echo '</div>';
include('footer.php');
?>
Then create a function page and add a function that parse the url and gets the php page. (quickly made function, but should do the trick, or help)
function get_page()
{
//http://example.com?page=home
$page = $_GET("page");
$directory = "pages";
if($page){
//security check (anti-hack)
if (!strpos($page,".")&&!strpos($page,"/")&&!strpos($page,"\\")&&!strpos($page,";")){
$path= $directory."/".$page.".php";
if (file_exists($path)){
require ($path);
}else{
echo "<p>Sorry, but that page does not exist.</p>";
}
}else{
echo "<p>Sorry, but those characters aren't allowed !</p>";
}
}else{
$path = $directory."/home.php";
require ($path);
}
}
Then your php page should look like this:
<p>This is the home page</p>
Add this piece of code in the content area of your content area in main file...
<li>BLOG</li>
<div class="container content">
<?php
if(isset($_GET['page']))
{
$page_name = $_GET['page'];
include("/".$page_name);
}
?>
</div>
I'm not sure how describe my question perfectly or how to begin. But what i can start with is that i'm making a website in PHP for my school and i've never used php before. And what i need is to only change one part of the page i.e the sidebar. or the content, depending if the user clicks i.e school, contact etc.
on my index.php the sidebar shows the news. But in my navbar if you click i.e useful links then the sidebar should change from showing news to showing a couple of links.
And if it is possible, if you then click on one of the links it should only change the content under the navbar and the sidebar should still show the useful links.
<?php
include('include/header.php');
include('include/nav.php');
?>
<div class=content>
<h1>Heading 1</h1>
<p>Text goes here</p>
</div>
<?php include('include/nyheter.php') ?> <--not part of code! this is the sidebar
<?php include('include/footer.php')
For now, i've only got the news to be shown in the index.php using <iframe>code. but i want to get rid of the <iframe> and use <div class=sidebar">or use ID instead of class.
I've search around to find useful information, but most of them says something about AJAX and JQuery, which i don't know what is or even how to use it =(
If you need more information, just ask as spesific as possible, so that i can give the right information =)
EDIT
I've uploaded the entire website to my google drive so that i could share it with you.
as you may see, i've created a bunch of new .php files (most of them was from some other versions that was created in html, and are now converted to PHP.
The link is: https://docs.google.com/folder/d/0BxsqMLXovlRHdmFlOFYzMjlzRHc/edit?usp=sharing
by the way, the website is in Norwegian.
I hope this information can help you to tell me how to solve my question.
remember, i am totally new to PHP and other programming languages other than HTML & CSS.
You could use if sentence to change the page content according the url
for example
if($_GET['page'] == "about") {
echo "about page content";
}
elseif($_GET['page'] == "page1") {
echo "page1 content";
}
and so on
then you just put links yourpage.com/index.php?page=about into your menu and so on
<?php
// get current page url
$page = basename($_SERVER["REQUEST_URI"]);
if ($page === "index.php"){
// display news info
}else if($page === "usefulllink.php"){
// display userful links
}
?>
Use the php $_GET variable to set which page and navigation you want to use. This is accessed via variables in the url.
For example: www.yoursite.com?page=home&nav=first
You can then include in the php
<?php
$page = $_GET['page'];
$nav = $_GET['nav'];
//set some defaults
if(strlen($page) == 0){ $page = "home"; }
if(strlen($nav) == 0){ $nav = "about"; }
//Then using if statements you can select content and navigation
if($page == "home"){
//print navigation bar with selected link
if($nav == "about"){
//print content for the selected navigation
}
}
?>
I am building a website that uses includes php scripts.
I've got a header.php file, where it contains the logo, and the navigation, and a footer.php file that contains information. These are loaded from the main content files, for example, from the home page, I have a <?php include 'header.php'; ?> script.
The main problem I am having is the navigation. Because it is included in the header, none of the formatting is working as it should, so when I click on a page, the CSS styling doesn't change so the user knows what page they are on. From what I am gathering, the header.php file is refreshing the information.
Including the header.php file won't affect this as the CSS is processed on the client side.
What you need to do is have make your header.php file aware of which page it is on so it can assign a class to the navigation element appropriate to that page. Then make sure you have a rule in your CSS that specifically states that any navigation element that has that class will be highlighted appropriately.
A rough example:
page.php
$current_page = 'home';
include('header.php');
header.php
<ul>
<li<?php if ($current_page === 'home') echo ' class="highlight"'?>>Home</li>
<li<?php if ($current_page === 'about') echo ' class="highlight"'?>>About</li>
</ul>
I'm building a website which uses a lot of repeated styles and HTML tags, such as the header, the navigation bar at the top, and the footer at the very bottom. Instead of typing all of that repeated code in again and again, I'd like to use PHP include statements to include everything that I need.
While that's all well and good, this poses a problem with my navigation bar. I use a CSS class attached to the navigation link to let the user know what page they're on (using a different background color). On each page, I manually specify which link gets that special class. If I were to simply include that content via a PHP include statement, I couldn't manually specify the class.
My question is: how would I be able to do that? Either by using a separate PHP script to find out which page the user is on, and then specify the class to style the link appropriately, or by using JavaScript to do the same thing? Or maybe there's something else that I'm missing? Who knows! And as such, I ask!
An easy way to do it would be to add a PHP variable like $page before the include and then use this variable in your included menu file to determine which menu item needs highlighting.
So in the file with the includes:
$page = "home";
include("menu.php");
And in the menu file:
<ul>
<li <?php if( $page == "home") echo 'class="active"' ?> >home</li>
<li <?php if( $page == "about") echo 'class="active"' ?> >about</li>
<li <?php if( $page == "contact") echo 'class="active"' ?> >contact</li>
</ul>
The example above isn't the tidiest or most optimal solution, but it gives you an idea of how you could achieve what you are trying to do by using PHP variables.