I'm trying to build a website that having different pages.
I'm a newbie to PHP, I have an index page with certain tables for holding header, navigation menu, main body, a sidebar and for a footer. The index page is attached to all the above-mentioned elements in it using include("filename.extension");. The problem is I tried to load the main body i.e content of the site, dynamically when the menu is changed.
Below is my code, any suggestion on this is very appreciatable. Thanks in advance.
<body>
<div> <?php Include('header.php'); </div>
<div id="menu" align="center">
<table width="790" height="35">
<tr>
<td>Home</td>
<td>Register</td> // this page need to display on the same window with other elements, after click.
</tr>
</tabel>
</div>
<div id="sidebar" align="right"> <?php include ("sidebar.php");?> </div>
<div id="Content ">
<?php include("FILE"); // here i need to display the hyper linked page.?>
</div>
<div id="footer"> <?php include("footer.php");?> </div>
</body> `
guess Diogo's answer will get you to what you want. create your index.php file like this.
//menu area
<div id="menuWrap">
link to page1
link to page2
</div>
//side bar
<div id="sidebarWrap">...</div>
//content area
<div id="contentWrap">
<?php
switch($_GET['page'])
{
case 'page1':
include '/pages/page1.php';
break;
case 'page2':
include '/pages/page2.php';
break;
default:
include '/pages/notfound.php';
}
?>
</div>
//footer
<div id="footerWrap">...</div>
This way you can display other pages in your site in the same window, only the content area will change
update:
I have got corrected your typos, here is the answer according to your code segment.
<body>
<div>
<?php Include('header.php'); ?>
</div>
<div id="menu" align="center">
<table width="790" height="35">
<tr>
<td>Home</td>
<td>Register</td>
</tr>
</table>
</div>
<div id="sidebar" align="right">
<?php include ("sidebar.php"); ?>
</div>
<div id="Content ">
<?php
switch($_GET['page'])
{
case 'page1':
include '/pages/home.php';//file path of your home page
break;
case 'page2':
include '/pages/students/reg.php';
break;
default:
include '/pages/notfound.php';
}
?>
</div>
<div id="footer">
<?php include("footer.php");?>
</div>
</body>
First, create a file with exactly the name below: (including the dot)
.htaccess
and put it in the same folder of your index.
Inside the .htaccess, write the following code:
# .htaccess mod_rewrite
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
The .htaccess on this case will rewrite the URLs to change the http://127.0.0.1/index.php?page=register to accept the "blogging pretty url" http://127.0.0.1/register
inside of your index.php folder, create a new folder "pages".
Inside of the folder pages, create 4 simple PHP files (home.php, register.php, anyother.php and notfound.php), with any content like
<?php
echo "I'm the Register Page.";
?>
This will be an example of index based on your code.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8"><title>¨title</title></head>
<body>
<div>
<?php include 'header.php'; ?>
</div>
<div id="menu" align="center">
<table width="790" height="35">
<tr>
<td>Home</td>
<td>Register</td>
<td>AnyOther</td>
<td>Broken Sample</td>
</tr>
</table>
</div>
<div id="sidebar" align="right">
<?php include 'sidebar.php'; ?>
</div>
<div id="Content ">
<?php
if(!isset($_GET['page']) || $_GET['page'] == ''){
$page = 'home'; //If no page specified
} else {
$page = $_GET['page'];
}
switch($page)
{
case 'home':
include '/pages/home.php';//file path of your home page
break;
case 'register':
include '/pages/register.php';
break;
case 'anyother':
include '/pages/anyother.php';
break;
default:
include '/pages/notfound.php'; //If any page that doesn't exists, then get back to home.
}
?>
</div>
<div id="footer">
<?php include("footer.php");?>
</div>
</body>
</html>
Related
i want to show total records in a database in dashboard-home.php .
i am getting error of undefined index
how to make proper links to pages to get contents in a same page i mean different pages in a same div.
<!DOCTYPE html>
<html>
<head>
<title>SIS Dashboard</title>
<link rel="stylesheet" type="text/css" href="../css/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../css/main.css">
</head>
<body id="dashbody">
<div class="row dashboard-head">
<div class="container">
<div class="col-md-2">
<h2>DASHBOARD</h2>
</div>
<div class="tpad navbar-right">
<div class="col-md-8">
<p><b><?php echo "Welcome " .$_SESSION['user']; ?></b></p>
</div>
<div class="col-md-4 logout-a">
Logout
</div>
</div>
<div class="row db-wrapper">
<div class="col-md-3 left-panel">
<ul>
<li>User Panel</li>
- View Users<hr>
<li>Gallery Panel</li>
- View Gallery<hr>
<li>News Panel</li>
- View All News<hr>
<li>Events Panel</li>
- View Events
</ul>
</div>
<div class="col-md-9 right-panel">
<?php
if($_GET['dash-home']){
include'dashboard-home.php';
}
if(isset($_GET['gallery'])){
include'pages/gallery_panel.php';
}
if(isset($_GET['news'])){
include'news_panel.php';
}
if(isset($_GET['view_news'])){
include'view_news.php';
}
if(isset($_GET['edit_news'])){
include'edit_news.php';
}
?>
</div>
</div>
</div>
</div>
</body>
</html>
Note that, your URL is dashboard.php, no query string with dashboard.php?dash-home=1, it means this condition can't be work and you will get the Undefined Index notice as you are getting:
if($_GET['dash-home']){
And if you check your code you are already using isset() for other input so you need to add isset() here also:
if(isset($_GET['dash-home'])){
As per your dashboard link:
<h2>DASHBOARD</h2>
You can't get the value from dash-home because you didn't use value of dash-home, you can use like:
<h2>DASHBOARD</h2>
One more important point, $_SESSION needs session_start() at top level and i didn't find this function in your file. This is just side note, you need to take care of this.
Update:
If you want to display an included file in default than you need to use else part and as per your comment you need to use one include file as per condition, try this:
<?php
if(isset($_GET['dash-home'])){
include 'dashboard-home.php';
}
elseif(isset($_GET['gallery'])){
include 'pages/gallery_panel.php';
}
elseif(isset($_GET['news'])){
include 'news_panel.php';
}
elseif(isset($_GET['view_news'])){
include 'view_news.php';
}
elseif(isset($_GET['edit_news'])){
include 'edit_news.php';
}
else{
include 'dashboard-home.php';
}
?>
I would recommend to use switch instead of if-else
You are checking for $_GET['dash-home'] but you need to check for $_GET['page']
Try:
if(isset($_GET['page']))
{
switch ($_GET['page'])
{
case 'dash-home':
include 'dashboard-home.php';
break;
case 'gallery':
include 'pages/gallery_panel.php';
break;
and so on.....
}
}
I am new to PHP. I have these 3 files :
index.php
functions.php (to organize functions)
header.php
I want to simplify(which has been done so far) the index.php page thus I do not need to write the and all stuff again and again. So I created header.php that can be loaded by index.php:
header.php
<!Doctype html>
<html lang="en">
<head>
<title>Learn PHP</title> <!--This is the problem, every page that loads header.php will have same title! -->
</head>
<body>
<div class="header">
<h1>Learn PHP</h1>
<p>Your library of PHP learning!</p>
<hr/>
</div>
<!-- footer is handled by footer.php that include </body> and </html>-->
I have even simplified things further by making a function in functions.php so that I can just type "get_header()" in the index.php without writing the whole code again.
functions.php
<?php
function get_header(){
if (file_exists('header.php')){
require 'header.php';
}
else{
echo "There is an error retrieving a file";
}
}
?>
Now, how do I allow this index.php to have custom page title instead of the default given by header.php?
Am I missing something important. I have tried creating a variable and try to pass it to the functions.php, but it didn't work. Or is there any cleaner way to do this?
I am inspired by how wordpress organize their files, I have checked the wordpress file. And then I decided to try something from scratch so I understand better and improve my PHP skills.
I know can use POST and GET, but no I dont want to refresh or load a new page just to change a page title especially index.php
EDIT :
Here I included my index.php
<?php
require 'functions.php';
?>
<?php
get_header();
?>
<table>
<h3>What we learned</h3>
<ul>
<li>Syntax </li>
<li>Variables </li>
<li>Code Flow </li>
<li>Arrays </li>
<li>Superglobals </li>
</ul>
</table>
<?php get_footer(); ?>
It seems like all you need you want is simple includes. You're actually making it harder by using a function, here, because an include has the same scope as where it was included from. E.g.
header.inc
…
<title><?php echo isset($title) ? $title : 'Untitled';?></title>
…
index.php
<?php
$title = 'Welcome';
require 'header.inc';
?>
welcome
another-page.php
<?php
$title = '2nd page';
require 'header.inc';
?>
2nd page content
If you want to use a function, give it parameters.
function get_header($title = 'Some default title') {
…
}
the included file will have access to the variables in the function's scope.
in the functions.php
function get_header(){
if (file_exists('header.php'))
require 'header.php';
else echo "There is an error retrieving a file";
}
in the header.php, and in the balise title you call the session parameter
<!Doctype html>
<html lang="en">
<head>
<title>
<?php if(!empty($_SESSION['title-page'])) echo $_SESSION['title-page']; else 'Learn PHP'; ?>
</title>
</head>
<body>
<div class="header">
<h1>Learn PHP</h1>
<p>Your library of PHP learning!</p>
<hr/>
</div>
<!-- footer is handled by footer.php that include </body> and </html>-->
and in the index.php
<?php
session_start();
$_SESSION['title-page'] = 'this is the welcome Page';
require 'functions.php';
get_header();
?>
<table>
<h3>What we learned</h3>
<ul>
<li>Syntax </li>
<li>Variables </li>
<li>Code Flow </li>
<li>Arrays </li>
<li>Superglobals </li>
</ul>
</table>
<?php get_footer(); ?>
and in another-page.php
<?php
session_start();
$_SESSION['title-page'] = 'this is an another Page';
require 'functions.php';
get_header();
?>
<table>
<h3>What we learned</h3>
<ul>
<li>Syntax </li>
<li>Variables </li>
<li>Code Flow </li>
<li>Arrays </li>
<li>Superglobals </li>
</ul>
</table>
<?php get_footer(); ?>
I am Developing a code using php . In which I am importing two files (header.php and footer.php) in another php file index.php.
Now I want that header.php and footer.php should be fixed while scrolling horizontally. can you suggest me the code. I am using the following code
Header.php
code here
footer.php
code here
index.php
<?php include("header.php"); ?>
welcome to index file
welcome to index file
welcome to index file
<?php include("footer.php"); ?>
In this file index.php, I want that header and footer should be fixed while scrolling horizontally and others css should also not be disturbed.
try this.,
<div id="header">
<?PHP include_once('header.php');?>
</div>
<div id="wrap">
welcome to index file
</div>
<div id="footer">
<?PHP include_once('foo.php');?>
</div>
css:
#header{position:fixed;top:0px;}
#footer{position:fixed;bottom:0px;}
or
header.php
<div id ="header"></div><div id="content">
footer.php
</div><div id="footer"></div>
index.php
<?PHP include_once('header.php');?>
content here.,
<?PHP include_once('footer.php');?>
Your solucion is ok.
only i recomend use something like this:
<div id="header">
<?PHP include_once('header.php');?>
</div>
<div id="wrap">
here is my html content
</div>
<div id="footer">
<?PHP include_once('foo.php');?>
</div>
the use of include_once is for not repeat the include of some file
and with a good css obtains a excelent layout
use HTML5 and use include_once
<header>
<?PHP include_once('header.php');?>
</header>
<div id="wrap">
here is my html content
</div>
<footer>
<?PHP include_once('foo.php');?>
</footer>
Here is just a sample FIDDLE DEMO
html,body{
margin:0;padding:0;
}
header{
background:Green;
height:50px;
}
footer{
position:absolute;
width:100%;
bottom:0;
background:Red;
height:50px;
}
In your css, just put the following attribute:
position:fixed; and that should do the trick.
I have simple slideshow on my home page as a php include. It shows up, but it seems to be floating up to the top of the page, and not falling in line with all of the other includes, ie, header, menu, etc. In addition, the image isn't lined up with the arrows. Here is where it lives:
http://www.starhilldesignstudio.com/adventure-suites/index-test.php
And here is the slideshow solo (arrows line up):
http://www.starhilldesignstudio.com/adventure-suites/slideshow-home.php
My html code:
<div id="container">
<div id="header">
<?php include 'menu.php'; ?>
</div>
<div id="mainContent" style="position:relative; z-index:1;">
<div class="center margin-top "><?php include 'slideshow-home.php'; ?></div>
<?php include 'home-blurb.php'; ?>
<?php include 'home-list.php'; ?>
<?php include 'bottom-stripe.php'; ?>
<div id="footer2">
<?php include 'footer.php'; ?>
</div>
</div>
</div>
THANKS!!!
Looks like a misplaced include, place your include function where you wanted to put your file to.
Example you wanted to put it before your content in between your header and footer:
include "header.php"
include "slider.php"
include "content.php"
include "footer.php"
Let us know if this is not the case.
<div id="container">
<div id="header">
<?php include 'menu.php'; ?>
</div>
<div id="mainContent" style="position:relative; z-index:1;">
<!-- Add this... -->
<div style="clear:both;padding-bottom:10px"></div>
<!--<div class="clear"></div>consider making the above style into a class.-->
<div class="center margin-top "><?php include 'slideshow-home.php'; ?></div>
<?php include 'home-blurb.php'; ?>
<?php include 'home-list.php'; ?>
<?php include 'bottom-stripe.php'; ?>
<div id="footer2">
<?php include 'footer.php'; ?>
</div>
</div>
</div>
The image is uploaded by the user and stored in public/users folder. The view script is as follows:
<?php echo $this->render("includes/header.phtml"); ?>
<div id="header">
<div class="w">
<div id="logo">SyncFan.com</div>
<div id="mainmenu">
<ul>
<li class="selected"><span>My Account</span></li>
<li onMouseMove="$('.submenu',this).show();$(this).addClass('hover');" onMouseOut="$('.submenu',this).hide();$(this).removeClass('hover');"><span>View Artists</span></li>
<li onMouseMove="$('.submenu',this).show();$(this).addClass('hover');" onMouseOut="$('.submenu',this).hide();$(this).removeClass('hover');"><span>Update Account</span></li>
<li onMouseMove="$('.submenu',this).show();$(this).addClass('hover');" onMouseOut="$('.submenu',this).hide();$(this).removeClass('hover');">
<span>Log Out</span>
</li>
</ul>
</div>
</div>
</div>
<div id="content" class="w">
<h2>SyncFan My Page - <?php $this->session = new Zend_Session_Namespace('login');echo $this->escape($this->session->username); ?></h2>
<!--- MAIN CONTAINER -->
<table class="table">
<tr><td valign="top">
<!--PROFILE DATA-->
<table class="table ">
<tr><td>Username: <?php echo $this->session->username; ?></td></tr>
<tr><td><img src="<?php echo '../public/users/'.$this->avatar; ?>"/></td></tr>
<tr><td><?php echo $this->aboutme; ?></td></tr>
<tr><td>Date Joined: <?php echo $this->session->dateJoined?></td></tr>
</table>
</td>
<td valign="top">
<!--FAVORITE ARTIST LIST-->
<table class="table table-hover">
<?php if($this->artists){?>
<tr align="center">
<td><b>Artist Name</b></td>
<td><b>Date Became A Fan</b></td>
</tr>
<?php foreach($this->artists as $artist){?>
<tr>
<td><a href='/artist/video-list?artist=<?php echo
urlencode($artist['artist_name'])?>'>
<?php echo $this->escape($artist['artist_name']); ?></a></td>
<td><?php echo $this->escape($artist['date_became_fan']); ?></td></tr>
<?php }
}else{
?>
<tr><td>You currently have no favorite artists.</td></tr>
<?php }?>
</table>
</td>
</tr>
</table>
</div>
<?php echo $this->render("includes/footer.phtml"); ?>
in the account view.
The output of the HTML statement
<img src="<?php echo '../public/users/'.$this->avatar; ?>"/>
is empty.
I'm able to get the details of the uploaded image but not able to present it back to the user account.
<?php var_dump($this->avatar); ?>
The ouput of the above statement is string 'filename.jpg' (length=length of file).
I have checked the .htaccess file also but could not find anything wrong in it.
Any help would be greatly appreciated.
<img src"<?php echo '/users/'.$this->avatar;?>"/>
The browser can't access anything outside your document root, which presumably is /public, so the path should be relative to that.
Edit: So the line from your template is:
<tr><td><img src="<?php echo '../public/users/'.$this->avatar; ?>"/></td></tr>
if you view the source of the resulting HTML page, you should get:
<tr><td><img src="../public/users/filename.jpg"/></td></tr>
(which won't work). With my suggestion above, the line from your template should actually be:
<tr><td><img src"<?php echo '/users/'.$this->avatar;?>"/></td></tr>
which should give you:
<tr><td><img src"/users/filename.jpg"/></td></tr>
If you are actually getting:
<tr><td><img src""/></td></tr>
then you've missed the 'echo'. If you're getting:
<tr><td><img src"/users/"/></td></tr>
then $this->avatar does not in fact contain filename.jpg like you said.
If the entire page is blank, there's actually a PHP error being generated but you have display_errors turned off (check your web server error log). Without knowing exactly what you are getting, all I can do is guess.
<img src="<?php echo '/users/'.$this->avatar; ?>"/>
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]