How can I make php pages appear inside a div? - php

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>

Related

How to hide header and footer.php

Hi guys i m building a website in php i created my first 3 page
-header
-index
-footer
in index.php i use:
<?php include("header.php"); ?>
<?php include("footer.php"); ?>
My question is:
if i go to my url http://localhost:8888/project/index.php
every its'ok
but if i write http://localhost:8888/project/header.php or footer
How i can not display the content it s in this two pages?
Because if i m using a redirect i have a blank page in index.php because the index.php include header and footer
Thanks a lot
Define a constant in your scripts before including header or footer:
index.php:
define('MY_CONSTANT', 1);
include 'header.php';
//
include 'footer.php';
Then, in header.php and footer.php check if constant is not defined:
if(!defined('MY_CONSTANT')) {
// You can show a message
die('Access not allowed!');
// Or send user to index.php, first delete above line (die)
header('Location:index.php');
exit; // This line is needed to stop script execution
}
// rest of header and footer code
go to your footer and header scripts, and make then a variable, for example:
$header = "<!doctype html><html><head>#head Content</head>";
and replace every qoutes with \" (look at the div class in $footer)
$footer = "<footer><div class=\"class\">#footer Content</div></footer></html>";
then in the index.php make it echo the variables
<?php
require_once("header.php");
require_once("footer.php");
echo $header;
echo $body;
echo $footer;
?>
then you can add this code to make it look like the file doesn't exist
<?php
header('HTTP/1.0 404 Not Found');
readfile('www.link.to/error-page/404');
exit();
?>
Note: it's all up to you how you make your website, but this is the method I use, and it works fine.
extra:
you can make your $body like this
let's say you have a folder call scripts, you create a file called pager.php
and you add the code
<?php
$dir = $_SERVER['PATH_INFO'];
if(empty($dir)) {
require_once("templates/mainpage.php");
} elseif ($dir == "page2") {
require_once("templates/page2.php");
} else {
require_once("templates/pages-doesnt-exist.php");
}
?>
remember to make a folder called "templates" and add all your pages in there,
then you'll be able to access all page from http://example.com/index.php/{pagename}
you can also add this code to your .htaccess
RewriteEngine On
RewriteRule ^page2$ /index.php/page2 [L] --page wise condition
so http://example.com/page2 will access http://example.com/index.php/page2
Compare $_SERVER['REQUEST_URI'] to make sure the requested page/file is neither header.php nor footer.php

Including CSS file depending on php include variable

I am trying to include a specific css file depending on the URL provided by the browser.
My website has the following format:
Header information
php include for content
Footer information
I am doing this so when I change my header menu links, I don't have to change every page on the website.
The php include code is as follows:
<?php
$page = $_GET['page'];
$pages = array('main', 'contact');
if (!empty($page)) {
if(in_array($page,$pages)) {
$page .= '.php';
include($page);
}
else {
include('404.php');
}
}
else {
include('main.php');
}
?>
The URL is made up as follows
index.php?page= for example index.php?page=contact
As part of the php include code, when the page=???? element isn't within the array it loads page 404.php which is an error page.
As can be seen, the array currently contains two pages, main (the home page content) and contact (the contact form). If a user tries to load a page called test (index.php?page=test) my webpage will display the 404 page (however doesn't load index.php?page=404 - the URL in browser stays as index.php?page=test.
What I'm trying to achieve is to load 404.css file when page=test(or any other page name not included in the array) is loaded.
Does anyone know how i'd go about achieving this? I've tried writing an if statement in the header to load 404.css however I was using strstr that fetches the browser URL (so test, not 404), therefore the correct css file doesn't get loaded.
Any help is much appreciated (or a better way to achieve what i'm trying to do).
Thanks in advance
Iain
An easy way of doing this is to make use of the ob_* php functions like bellow:
PHP get all the information from within the ob_start and the ob_get_clean functions and concatenate them all together as a single variable $_page_content, that variable can be used anywhere you need.
<?php
$page = $_GET['page'];
$pages = array('main', 'contact');
if (!empty($page))
{
if(in_array($page, $pages))
{
ob_start('ob_gzhandler');
?>
<link rel="stylesheet" href="css/<?=$page?>.css" type="text/css">
<?
include($page . '.php');
$_page_content = ob_get_clean();
echo $_page_content;
}
else
{
include('404.php');
}
}
else
{
include('main.php');
}
?>

Change the content of webpage when clicking different links

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
}
}
?>

hide div in a certain html page

apology for this newbie question.
I have created an html page (dash.html) that uses the same header as the other pages.
it calls this PHP function <?php include 'header.php'; ?>
the dash.html contains a special <div> made specially for that page; and it must be placed inside the header.php
im trying to figure out how to enable/disable a certain div on a certain html page.
will it require a PHP conditional statement?
Yes. The easiest way is to include a conditional line in the header, and pass the checked variable from each page that calls header. So, in your dash.php (it can't be a .html if it calls php, can it?):
<?php
$includediv = true; // set to false, or leave out, if you don't want the div
include('header.php');
?>
and in the header.php:
<?php
...some other code...
if($includediv){
...code to include div...
}
?>
This will continue to work as before for all other pages that call header.php.
You are trying to show the div only if header.php is present, right?
So, just set a variable inside header.php and use a conditional inside the HTML page.
Try this code
<?php
$path=explode('/',$_SERVER['REQUEST_URI']);
$page=end($path);
if($page=='dash.php')
{
?>
<div>your div for dash.php page</div>
<?php } ?>

PHP how to open page1, redirect to page2 and show page1 in a div dom?

I have many sub pages, there urls like www.domain.com/sub/page1.php www.domain.com/sub/page2.php... Now when I type there url in browser. they all redirect to www.domain.com/sub/index.php, and the current sub page will show in a div dom in the index.php.
My knowledge is very limited. I only know jqeury.load and php header Location. but it is difficult redirect to index.php then tell index.php, which is the current sub-pages then do a jqeury.load.
One more note: Also should think SEO. maybe jqeury.load is bad for a search spider. maybe should use hash url.
So I ask for a help. is there anybody could give me some good suggestion? or simple worked examples?
Thanks.
In every PHP file (e.g page1.php) use this code:
<?php
if(!defined('INCLUDE')) {
header('Location: index.php?site=' . $_SERVER['PHP_SELF']); //Send the user to the index.php page
die();
}
?>
Insert the content you want here...
And in the index.php file:
<?php
define('INCLUDE', true);
if(isset($_GET['site'])) {
$site = $_GET['site'];
} else {
$site = 'page100.php'; //Put the name of the default page if the user visits index.php here
}
?>
<html>
<head><!-- HEAD CONTENT HERE --></head>
<body>
<div><?php
include($site); //Put the page requested into the div
?></div>
</body>
</html>

Categories