several subpages with same topbar - php

I have a webpage with several subdirectories for example /search or /friends. Each of this subpages has its own javascript and css files. Now I want all this pages to have the same topbar so if I wanted to change the topbar I would only have to do this in one single place.
What's the common way of doing this? Simple php drops out because of the several scripts and css files. My idea was to call a php script via ajax on each subpage and append the returning string to the body element with jquery's append method but this doesn't seem very clean to me.
How does facebook handle this? Facebook's topbar doesn't even blink when clicking an internal link.
Thanks.

What about using an header.php in all the pages where you want to show your top bar?
To do this just create a file with top bar and save it as header.php and then in your index.php just place include('header.php'); repeat second step for each page where you want to have your top bar.

header.php
// top bar stuff
echo '<ul><li>Link</li><li>Link</li></ul>'; //etc
Other Pages
<?php
include 'header.php';
?>

Related

Dynamic change of content in tabs

<?php --this is my index page--
header('Content-Type: text/html; charset=iso-8859-1');
include ('Header.php');
include ('Navigation.php');
include ('Content.php');
include ('Footer.php');
?>
<section class="tabs"> --this is my navigation tab--
<ul class="links1lvl">
<li class="active"><a>About</a>
<ul class="links2lvl">
<li class="active">O nás</li>
I'm creating a new web and I need help with some coding. The idea is, that I have 2 sections. First for tabs to choose desired content and second as place to display actual content.
The thing is, I've found a ton of guides but those inlude the content of all tabs on that index page, but I have 20+ pages so that's unreal. What I desire is a page, where only the actual content would change as I click different tabs, without refreshing the whole page AND wich is loaded from ,,external" html/php files. Now I don't need code, I will gladly learn as much as I can on my own, thing I need however is a direction. Where should I look for solution.
You need to use AJAX. This will allow you to trigger a call to an external file via Javascript, and display the result onto the current page without the need to refresh.

jQuery not working in a PHP include

I am building a website that utilizes a template and brings each "content" page in through an include statement. On one page, I am using wt-rotator which is a jQuery script based slide show so to speak. My problem is that the page that I am wanting to include runs the script perfectly, but when I try to view that page through the main template using the include, there is no slideshow. This is the code that makes the "include" work:
<?
include "/home/content/82/7960182/html/alliantwellness/contentfiles.php";
$pid = $_GET["pid"];
if ($pid == "") {
$pid = 0;
}
?>
That part is at the very top of the index.php page. Then this:
<?
//Main Content including Navigation fills in here
include "/home/content/82/7960182/html/alliantwellness/content/" . $content[$pid];
?>
is what I use to call the page. The contentfiles.php page is just an array of "content" pages that are stored in a folder called "content".
Any ideas on why the slideshow works outside the template, but not inside?
Here are the URL's so you can see what I am talking about:
http://www.alliantwellness.com
http://www.alliantwellness.com/content/home.php
I think you need to remove this line:
<script type="text/javascript" src="http://www.alliantwellness.com/scripts/jquery.wt-rotator.js"></script>
from index.html. That file is not a Javascript file, it looks like it's a duplicate of the index.html file. You're already loading jquery.wt-rotator.min.js on a different line of the file, and that's the correct JS.
I also wonder why you're loading both http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js and http://www.alliantwellness.com/scripts/jquery-1.7.1.min.js, why do you need to load two different versions of jQuery from different locations? But home.php loads both of them and it seems to work.

How to link navigation/buttons to the whole site using one file?

Is there a quicker way to make the navigation bar/buttons go onto all my pages of my site instead of going into each .php file and copying and pasting it. How would I link up the class="button" to another file so that it changes the whole navigation on my site?
Just use an include statement.
include 'nav.php';
on each page.
Nav.php could then contain nothing more than just the navigation.
First Separate your php file as header.php for header menu and footer.php for footer menu.
And then include the file in pages .
See this example for reference.

Minimize code size to avoid duplication of the same code

I need one advice from you. I am working on a website, which uses PHP and HTML. As the biggest part of the header and footer code will be same for many pages, I am thinking of using PHP's include to avoid code duplication. But, each of those pages requires different stylesheets and JS files included. What do you think how could I let the other file know what scripts and stylesheet to import?
Our company does this:
The header reads the filename of the page calling it when it's included.
Then, it changes the extension to '.js' and outputs that if it exists. Same for CSS.
So if I have a page "register.php", it will auto-include "register.js" and "register.css" if they exist.
Here's what I do:
<?php include("includes/headContent.php"); ?>
<title>Page title goes here!</title>
<script src="script_only_used_on_this_page"></script>
<?php
require_once("includes/siteHeader.php");
?>
Site Content Goes Here!!
<?php
require_once("includes/siteFooter.php");
?>
Head Content includes any PHP I want included in every page, as well as the opening html and head tag, and any Javascript libraries and css stylesheets I want on every page. Site header closes the /head tag, and opens the body as well as printing out my site header and some other markup that goes on every page. Finally Site Footer closes out my template. Everything in between is my content area!
There are lots of different ways you can do templating, if you wanted to create a simple include and an echoHeader() and an echoFooter() function... just have the echoHeader function accept a parameter which you would pass your javascript and CSS lines to.
you can use MVC coding pattern

use common template for all pages of website

ok, the title did not make much sense but this is what i am planning to do. I have designed a template for my website, with head body and div for specific stuff and everything. The website consists of header file, footer file, right-side column, header dropdown menu and a main body which would be present beneath the header dropdown menu, to the left of the right-side column, and above the footer. Right now there is some content is this main body area. What i am trying to achieve is that whenever any link is clicked on any of the other parts of the webpage, i want that content to be displayed in this main body. Right now i am copying this template to each and every page, but I want to keep this standard template as index.php and then replace main body content based on the link clicked. This is a php based website. Are there any examples where i can see how this can be achieved? or is there any standard procedure to do this. Please guide me, Thanks.
Here's a very simple way to do this:
index.php
<?php
function putPage($page) {
// put a list of allowed pages here
$allowed = array('page1', 'page2');
$page = trim($page);
$page = (in_array($page, $allowed)) ? $page : 'home';
echo #file_get_contents('.\html\\' . $page . '.html');
}
?>
<html>
<head>
<title>Title</title>
<!-- put stylesheets, js files, etc. here -->
</head>
<body>
<!-- you can have a nav bar or something here -->
<div class="navbar">
Page 1 Page 2
</div>
<?php putPage($_GET['page']); ?>
<!-- put a footer here -->
</body>
</html>
Then just put .html pages with the contents in an html subfolder.
The script will fetch them and insert them in the body.
There are a few ways you can achieve this. Off hand the two obvious ones I would say are:
Ajax to obtain content with event handlers attached to links/buttons/menus that produce maincontent specific to the request.
This requires server and client side scripting to achieve.
w3 ajax
Or alternatively use mod_rewrite with apache to determine what content to load in index.php page. For example with mod rewrite you may have a link http://www.site.com/subject/content/item# as a link structure. This could translate to www.site.com/index.php?subject=&content=&id= And these GET values would allow you to determine what to display in main content area.
This requires server side scripting and configuration of apache or (any web server with similar functionality to mod_rewrite).
mod_rewrite - apache
I use this:
<?php
$pag = array(1 => 'Home.php', 3 => '2.php');
echo require $pag[(int)#$_GET['p'] | 1];
?>
This is called either a Template View as far as you build your link specific HTML completely in PHP. You create a page layout template containing some wildcards. You load the template into a string and use string replacements or XML functions (more fancy but only suggestive if transformation is more complex).
Otherwise it is called Two Step View where you create the page layout template (as above) and a specific template for the links. Now first load the link specific template, put your dynamic content into (same techniques as above), load the page layout template and put the previous transformed specific template into.

Categories