I have a header file that contains my nav items. Views are loaded in dynamically depending on the link clicked using PHP.
The views currently are about 4. something like
<?php
require_once 'header.php';
//Pull in view or page depending on link clicked
$pages = ['dash'=>VIEW_ROOT.'dashboard.php', 'registration'=>VIEW_ROOT.'register.php', 'payment'=>'transaction.php'];
$page = (isset($_GET['page']) ? $_GET['page'] : 'dash');
include_once $pages[$page];
require_once 'footer.php';
The link that should open the modal is in the header file and the header file appears in every page. At the moment I have to write the modal div(the div containing contents of the div) in all the views so I can access it from every page. Can there be a way of writing it once and it will be available in all the views without duplicating same code in all views.
Will it be recommended to stick the div containing the modal in the header file? Any good recommendation will be appreciated.
Yes you can put it in your header.php, but i would recommend to create a extra file and include it into the footer.php
Hidden stuff is mostly at the bottom.
Why not just add it to your header.php or footer.php since they are static.
Related
I'm looking for an object-oriented solution or possibly a framework that already exists to do what I'm hoping to do for a somewhat small back-end/admin project.
For instance, the system is to manage Projects. So there will be a header/footer/menu on every page. Two pages, for instance, will be projects.php and notes.php.
To include the header and footer on these pages I would normally do:
//header.php
<!-- Header Content Here -->
include menu.php
//projects.php
include header.php
<!-- My Projects Content Here -->
include footer.php
//notes.php
include header.php
<!-- My Projects Content Here -->
include footer.php
and have my links to each page just be '/projects/' or '/notes/'
I've also tried:
//index.php
include header.php
include projects.php
include notes.php
include footer.php
//projects.php
if(isset($_GET['projects']) {
<!-- My Projects Content Here -->
//notes.php
if(isset($_GET['notes']) {
<!-- My Notes Content Here -->
and have the links in my menu be '/index.php?projects' and '/index.php?notes'.
Both are frustrating and don't seem very fluid to me. I would like to upgrade my tactics here but am unsure the best way to go about it.
What's the best way? Is there a Framework that is lightweight and can manage these for me? I would like to keep the links at '/projects/' and '/notes/' but just be able to create an object that calls the content from projects.php and places it in automatically on that link click.
TLDR; - I don't want to have to place header/footer/etc.php as an 'include X' into every PHP page template file manually. I would like for it to know that every page needs it unless otherwise assigned.
Create a file called something like page.php:
if(!empty($_GET['page'])) {
// Could add check to see if file exists
$page = $_GET['page']; // Being "notes" or "projects"
include 'header.php'; // Assuming this includes <head>, header, menu, functions etc.
include $page.'.php'; // Makes notes.php or projects.php
include 'footer.php';
} else {
echo 'Someting went wrong';
}
For links in menu it should be page.php?page=notes or page.php?page=projects
Every file? Are you really sure you want to do that? It might cause data crash.
But if you insist, look for your php.ini file and find auto_prepend_file
http://php.net/manual/en/ini.core.php#ini.auto-prepend-file
auto_prepend_file header.php // For Example
Now that you did that if you don't remove header.php from every program (or make sure include_once is used) then you might have php errors flying around.
I have searched all over the web trying to figure this out and am now trying to get a direct answer from some experienced users. I hope I can explain myself completely.
I know HTML and CSS and some PHP and Javascript, but no mean an expert. This is my questions:
When creating a website by hand (no Drupal, or Wordpress or predesigned templates), The first thing I do is create an index.php file that shows my HTML page layout. The second thing I do is create my links.inc.php file that will show all the links to my pages, ex: Home, About Us, Contact Us. Now on the index.php page I create php include files for the header, footer, and link pages. (these would read header.inc.php, footer.inc.php, links.inc.php) Now here is where I am trying to figure if there is an easier way to do the next step.
My normal steps would next to be to create a home.inc.php, aboutus.inc.php, contactus.inc.php files which will have all the "content" I want shown for each page.
I would then create a duplicate of the index.php and create aboutus.php where I would use the php include function to add the aboutus.inc.php into the "main content" area I would want this information displayed at. Then I would create anther duplicate of the index.php and name it contactus.php and "include" the contactus.inc.php file.
Is there any way to use the index.php file and have all the inc.php files on that page? For instance,
<div id="main">
<?php
include ("home.inc.php");
include ("aboutus.inc.php");
include ("contactus.inc.php")
?>
</div>
Obviously this does not work they way I have it laid out above, it shows all the pages at the same time instead of only showing the one page that is clicked on from the menu. Any suggestions? Is there a different way or am I doing it correctly with creating multiple pages?
Thank you for any help and I hope I was clear, if not I can try to explain a different way.
My suggestion is to include files conditionally, based on a variable that defines the current page.
For example, given the following navigation:
Home
About Us
Contact Us
Configure your index.php file to include external files, something like this:
// determine the requested page, default to the home page
$page = isset($_GET['page']) ? $_GET['page'] : 'home';
// check if the requested include file exists
$include_file = is_file($page.'.inc.php') ? $page.'.inc.php' : false;
// if the requested include file exists, include it
if ($include_file) {
include $include_file;
}
Feel free to adjust the logic. For example, if a $page value is not recognized as a valid page on your site, you may want to show a 404 page, default to the "home" page, etc.
Edit
If your include files are in a different directory, you'll need to provide the correct path:
// define the path to includes
$path = 'inc/';
// check if the requested include file exists
$include_file = is_file($path.$page.'.inc.php') ? $path.$page.'.inc.php' : false;
You could send a variable to PHP index.php?action=home; then, inside index make some verifications
if($action=="home") {include index.inc.php; }
else if ($action=="contact") {include contact.inc.php }
and so on.
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.
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';
?>
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.