Dynamics menu php - php

I am quite new in php
I would like to make a website where all pages has a default menu with 4 choices.
Then, depending of the choice a specific sub-menu will be showed in a new page (including the main menu).
I could obviously do something like:
index.php - main-menu - sub-menu1
index.php - main-menu - sub-menu2
index.php - main-menu - sub-menu3
But, is there any other way I can make a single page "sub-menu" and show different "sub-menu 'div' " so that I dont need to create a new page for each sub-menu?
Maybe I should use smarty php template engine?
Could anybody put me in the right way or give any suggestions? either advice`?
Exemple: if my index is something like:
<html>
<head></head>
<body>
<?php include ("main-menu.php"); ?>
</body>
</html>
Do I have to create different page for each submenu? Like...
<html>
<head></head>
<body>
<?php include ("main-menu.php"); ?>
<?php include ("submenu1.php"); ?>
</body>
</html>
or I can create page "submenu" and show that specific submenu I want base on a page title for exemple?

Related

More php sites and one template

I write my template in index.php using bootstrap.
In this template there is a place <div id="content"></div> which is dynamic content of this site.
It means that, there I want to change the content.
Other for login.php , register.php, news.php page etc...
Okey I can copy this template(from index.php) to all of these pages but it is not good idea. If I want to change something static (like menu) I should do it in all pages.
I can use mysql to generate menu of course, but I want to include other pages (which is need) to index.php content div
Best Regards.
Okey I have one template (in html + js - bootstrap) which have static elements for example navbar, logo etc...
But this template have one dynamic element I mean - div = content it change depending on which site is user watching (login.php,register.php etc...)
How to include this template to all subpages for example login.php,register.php etc... without copy and paste it everywhere ?
A basic solution could be to create a layout.php page containing your whole template, and just put a where your content is supposed to be.
Then, on each page, set your $content variable with the html of your page and just include layout.php then. A better solution is to implement the MVC design pattern, but it's more "complicated".
From personal experience, I find that it's best to break down every component of the page, for example I'll have
<?php
$pageTitle = "MyAwesomeWebpage";
require_once('header.php'); //Contains Doctype, HTML header tags
require_once('navbar.php'); //Contains navigation
?>
// Unique content here
<?php require_once('footer.php'); ?>
header.php example:
<!DOCTYPE html>
<html lang="en"><!-- ended in footer.php -->
<meta content= .... />
<title><?php echo $pageTitle; ?></title>
<link rel="stylesheet" href="mystyle.css" />
<!-- .... Other links and styles here -->
<body> <!-- ended in footer.php -->

Making a php template for blog page

I'm quite new to the whole php scene but I have managed to make a php header and footer and include them on my pages so I dont have to edit the header and footer on each page now. My next challenge is making a whole template for something like a blog page where if I change the template then all the blog pages will change accordingly but the content will of course have to remain the same much like the php header and footers I have. I have read a bit about theme engines etc but they all seem to be quite confusing, and I don't wish to convert it to wordpress. So what are my options as to making a template? thank you in advance.
You can simply use smarty, powerfull template engine for PHP.
First - create template with html base, header, footer. Save it to templates/_Frame.html
<!DOCTYPE html>
<html>
<head>
<title>{block name='Title'}{/block} - My website</title>
</head>
<body>
<div>Header and some other stuff</div>
{block name='Content'}{/block}
<div>Footer and some other stuff</div>
</body>
</html>
Then, crate a template file for each page. If its a blog with same look on each page and the only variable thing is the post content - you need only 1 template. Lets call it 'Post.html'
{extends '_Frame.html'}
{block name='Title'}{$Post.Title|escape}{/block}
{block name='Content'}{$Post.Content}{/block}
In php - do such thing:
<?php
//Lets say at this poin you've got $BlogPost = array('Title' => 'Blog post title', 'Content' => 'Body of blog post')
$S = new Smarty();
$S->assign('Post', $BlogPost); //This creates new variable $Post which is availible inside templates.
$S->display('Post.html'); //this displays your template
?>
|escape - escapes all html in variable < goes ^lt; etc.

How to configure the page to search for external layout code?

I'm looking for ways to have my pages search for the page layout from an external template page. Please see the below example.
<head>
<title></title>
</head>
<body>
<search for header, css, layout, etc from external page>
Page contents
<search for footer>
</body>
Is there any way to do this using PHP or HTML? I want to be able to edit the layout for all the pages without having to do it page by page. I welcome any other means to achieve the same effect as long as it works on all the browsers.
Thank you very much!
This is exactly the sort of thing that PHP is for. A PHP script can include the contents of another script using the include statement.
So each page in your application could have an associated PHP script that generates the contents, and includes footer.php for the footer layout. In this way, when you change footer.php all the pages that use it will automatically get the changes.
You can't do this with pure HTML, though you could with some javascript and Ajax.
Like Andrew said, use includes. I'll set up 2 basic examples.
The simplest, have multiple layout files that are called by your main file(s):
header.php:
<div id="header">
Menu can go here.
<?php echo 'I make all my files .php, so they can use PHP functions if needed.'; ?>
</div>
footer.php
<div id="footer">
Footer Link
</div>
index.php
<html>
<head></head>
<body>
<?php include('/path/to/header.php'); ?>
Specific index.php content here.
<?php include('/path/to/footer.php'); ?>
</body>
</html>
The other option is to have one PHP file which includes all your different layout elements in functions. The reason I like this, is because you can include one file and then call specific functions for different parts. This can also be used to pass variables like a title of a page.
layout.php
<?php
function makeHeader($title) {
return 'My title is: '.$title;
}
function makeFooter() {
$html = '
<div id="footer">
Footer Link
</div>
';
return $html;
}
?>
index.php
<?php include('/path/to/include.php'); ?>
<html>
<head></head>
<body>
<?php echo makeHeader('Page Title'); ?>
Specific index.php content here.
<?php echo makeFooter(); ?>
</body>
</html>
Just make sure you use relative paths (no http://www.) when including files. This will allow variables and functions to transfer over smoothly. The easiest way to do this is using the PHP variable $_SERVER['DOCUMENT_ROOT'] so if you have a file http://mysite.com/includes/layout.php, you could include it with include($_SERVER['DOCUMENT_ROOT'].'/includes/layout.php') no matter where your file you are including from is located.

php website one page for design

I'm done creating a php website that has 6 pages, and the structure of the pages is the same for each one of them, the only thing that changes is the content, so is the same header, same design and same footer, the only thing that changes like I said before is the content itself.
so i was thinking instead of having many pages, I could have only one design page, and change only the content, what do you recommend?,and how do I do that?, also im not planning installing anything like Typo3, wordpress, joomla or whatever in my server, so I want something i could do using php idk. thank you!
Simplest solution is to create separate files.
header.php
footer.php
menu.php
In header.php put your code from header
<?php ?>
<HTML>
<HEAD>
</HEAD>
<BODY>
...
<? ?>
Same goes for footer and menu files.
Then you can use it by including them.
Your index.php could look like following.
<?php
include("header.php");
include("menu.php");
?>
<h1> This is my content </h1>
<?php
include("footer.php");
?>
This is the easiest option I think for someone who doesn't want to spend using templates, CMS etc. Also you can create function called header that takes $title and changes title of your window. Up to you.
Sounds like you want AJAX. Use prototype. You can make one page, and then use prototype to swap out the content (which could include a PHP page) based on user clicks.
Simple and easy solution:
create footer.php and header.php
and in the header.php you can have something like this:
<?php function top_header($title) { ?>
<html>
<head>
<title> <?php echo $title ?> </title>
</head>
<body>
<?php } ?>
footer.php
<?php function footer() { ?>
</body>
</html>
<?php } ?>
Your index.php could look this:
<?php
include("header.php");
include("footer.php");
top_header("Title of the page");
?>
Hello World!
<?php footer(); ?>

HTML tags question

Something basic that i don't understand:
I have header.php with navigation bar for my site. Inside it, there's a <head>...</head> section.
Now, in each other page of my site, I'm using require_once 'header.php' so that each page will show the navigation bar. But, I need also specific <head>...</head> sections to the different page.
For example, in page customers.php, I'm using <script>...</script> to include the jQuery library. I don't need to include it in other pages.
Now, searching the web I see that multiple head tags is wrong syntax.
So, how can anyone:
avoid multiple "head" tags
WHILE
separating his work to different PHP files and including them ?
You have to change your page structure and employ templates.
Instead of loading header at the top of the code, you have to do it at the bottom!
And page code should output not a word, but collect all data in variables.
And only after that output can be started by calling template.
A example layout is going to be like this:
First. page itself.
it outputs nothing but only gather required data and calls a template:
<?
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
$pagetitle = "Links to friend sites";
//etc
//and then call a template:
$tpl = "links.php";
include "template.php";
?>
Next, template.php which is your main site template, consists of your header and footer:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<? include $tpl ?>
</div>
</body>
</html>
And, finally, links.php is the actual page template:
<h2><?=$pagetitle?></h2>
<ul>
<? foreach($DATA as $row): ?>
<li><?=$row['name']?></li>
<? endforeach ?>
<ul>
easy, clean and maintainable.
there are many advantages in such approach:
as requested, you can populate header with actual page-relevant data.
HTTP headers can be sent as well, before any output. It includes cookies, sessions, cache-control and many more.
it's 2011 today. AJAX era. You may wish change your code to return JSONed data instead of whole HTML page. It's going to be easy using such layout.
Imagine you're going to create very similar site with just different design. You will have to change only templates and don't touch engine files. That's really great advantage of using templates.
Here are some simple ways you can look at.
You can have jQuery on the pages
that don't need it; once it's
downloaded it will be cached so it
still wont use more bandwidth.
You can move out the closing </head>
tag from header.php and close the
<head> tag in the page that's including
header.php.
You can include javascript anywhere
on a page, not only in the header.
You can also do something like this.
Before you do require_once 'header.php'; you put a variable called $jquery = true;
In your header.php file you check if $jquery is set to true, if it is, you include jQuery.
in header.php
you can type like this
<head>
<?php echo $script; ?>
</head>
then in your customers.php
you can first assign the variable
$script = '<script>...</script>'
then
require_once 'header.php'
One possible solution.
You create a global variable before including header.php.
You test this variable in header.php.
If it is true, You print script or something. Something like this:
<!-- Fragment of header.php -->
<?php if ($i_want_jquery): ?>
<script ...>
...
</script>
<?php endif; ?>
On the other hand, a template may be a better solution.

Categories