Include menu on every page - php

Let's say I have a simple CSS layout and my menu is a column on the side. This menu is going to be on every web page (About 10 web pages). If I have items on this menu in the form of links and such, how can I make it so that if I add a link on one page, it will add it to all pages? Would you just make it a PHP page and in that <div> element include a PHP file called menu or something, and just edit that PHP file (so I only have to edit that file and not every web page)?

If this is raw PHP (no frameworks) then you simply include it.
include('sidebar.php');
Make sure that you can access the file from where you are including it though. If you have files in a folder called foo for example, and accessed via example.com/foo/somefile.php you will have to change the include statement to include('../sidebar.php'); assuming sidebar.php is in the root.

As stated above, and include would do.
Drop this on every page you want your menu to appear:
<?php include("/yourMenu.php"); ?>
Just save the menu code as yourMenu.php and put it in the root and you are good to go.

i've used this trick before and it works well. you can use this in conjunction with an include php
Intelligent Navigation

If you are using the Apache web server it's even more simple and faster to use server side include (ssi). You can do the same for the footer and other common areas of your pages.
Here is the reference page: http://httpd.apache.org/docs/2.2/howto/ssi.html

Related

Dynamic websites using php without inclusion

I am developing a website where all 5 main pages(home, about me, contact, blog, my foundation) are structured different from each other so I dont want to use the include() function to inherit anything from the index. how do I go about going to these pages about me (home, about me, contact, blog, my foundation)
You will need to add a link from each page to every page. It would be best to create a header.php file which you would include on every page. This file would contain your 'navigation' or the links to all the other pages. Since you say you don't want to use includes you will have to add this same code at the top of all the pages. If you want to change them you will have to change them on every page. If you used the include() function instead you would only have to change it once.

Wordpress include php file

I am trying to create a php website on WordPress for the first time.
When I create a page, it creates a permalink which is of the form http://localhost/?p=123. I don't know if there is a corresponding file.
I've installed insert-php plugin to read php code. It works fine on a static page. But how do I include another php file? I want to include my_utilities which contains all the back-end functions, in login. It has a permalink 'http://localhost/?page_id=45'.
What to do?
include 'http://localhost/?page_id=45' doesn't work.
Pages you define in WordPress's backend are pages mostly setup for your users to view / read. They, by default, have this URL structure of http://localhost/?p=123 (though this can be changed, but that's a whole different lesson).
To include a script file, upload the file to your folder structure where you have your website then refer to it in your include statement as follows:
include('path/to/folder/my_script.php');
EDIT: You may also want to have a look into WordPress Page Templates:
Pages are one of WordPress's built-in Post Types. You'll probably want most of your website Pages to look about the same. Sometimes, though, you may need a specific Page, or a group of Pages, to display or behave differently. This is easily accomplished with Page Templates.
For exapmle your wordpress file is at
/var/www/html/wp-content/myphpfiles/test.php
To include the test.php file in your wordpress page you have to following.
[insert_php] include('wp-content/myphpfiles/test.php'); [/insert_php]
Thats it.

How to make links work, regardless of location in directory structure?

So in the included image, you can see a copy of the website I'm currently working on. The site is going to have separate pages for each of the company's machines. Obviously the navigation bar and menu bar are going to be the same throughout the whole website so I thought I'd make use of a single line of PHP script to just load the menu in each time.
http://i.imgur.com/spVS3a4.png
(Attached image as I do not have +10 rep yet)
Though, from what I understand, the pages within folders (Not within root) wont be able to link to the rest of the site without the use of ../ within the href.
So if I load the menu part of the site containing the navbar, etc, it won't work on all the pages because they'll all be within different folders, meaning every folder will need it's own version of the navbar PHP import.
Am I going about this the wrong way? I'm just trying to minimize the amount of code I'm using on each page. Not only that, but once I finish the site if I have to make a change to the menu-bar, I don't want to have to update 30-40+ pages. I figured the PHP import would save me that sort of trouble? Is there a better way of doing this that isn't terribly complicated?
It should also be noted that I'm doing this in HTML5 with Twitter-Bootstrap as my main CSS.
I would also be making a similar PHP call that would import a footer as well.
If I missed anything that could be of help, please do ask.
TL;DR - How do I make links work throughout the entirety of the site, regardless of where they are in the folder hierarchy. Thought about using a tiny PHP script that would contain the menubar, etc and would be loaded on every single page. Though I'm not sure if that will work due to folders?
Try to start your links from /. This means that url is started from site root.
For example:
Patch Kettles
Just define a constant with the root path and echo it within all the links inside the pages that are contained in a folder like this:
[Put this in a file that you include through out your whole website (e.g. like the file with your database connection if you have one)]
define('BASE_URL' , 'http://'.$_SERVER['HTTP_HOST'].'/');
Than just add an echo in the start of each link:
Home
And for images the same thing:
<img src="<?php echo BASE_URL; ?>images/img_01.jpg"/>
You can just use /path/to/image.jpg to get absolute paths, while still being fine if your domain name ever changes.

Include pages to index.php or create top and bottom part of page and include to all content pages?

I haven't't created a webpage in a long time but I decided to create on now, and probably like everyone else I wanna learn the best way of doing this. But I've stumbled upon a little dilemma on how to process my pages. My first idea was to create one main page with all the CSS and everything needed, then 1 part of the site dedicated to each page's content. Then by using a page variable showing all the content for each site, example.
I have index.php as homepage, then visiting index.php?page=aboutme would make index.php include the aboutme.php in the part dedicated to each page's content. And only having text and some pictures etc in the aboutme.php. However I believe this will be a pain when people google my site and finds interest in the aboutme.php so they get linked to example, mypage.com/pages/aboutme.php and only sees the text and pictures but no CSS and not "the front page". The pros of this is of course that editing pages will be easy, I can create links etc in php loops by just checking contents of maps on my page.
The second example is that I take everything in my index.php above the part dedicated to page content, create a separate file for this, calling it top.php. Take all thee parts under the page and call it bottom.php. Then for each new page I create I include the top and the bottom parts. Making the link mypage.com/aboutme.php include the CSS and "the frontpage". Pros being that you actually can google subpages. This seems like the best idea to me, but like I said, I haven't created a lot of webpages lately and I've seen plenty use of both methods.
I've seen both types of webpages so I kinda wondered which one is the best practice?
I recommend just using php includes for the header, nav, and footer elements and then placing a class (home, about me, contact, etc.) on the body tag (for highlighting nav elements and such). This way the content is on separate pages and gives you more freedom, but saves you from having to retype all of the navigation and stuff each time.
Example:
<!DOCTYPE html>
<head>
<title>
Hello World
</title>
<meta name="description" content="Use this area to provide a description of this page.">
<?php include '_php/_includes/head.php'; ?>
</head>
<body class="home">
<?php include '_php/_includes/header.php'; ?>
<!--
Content Goes Here
Remember: 'div' elements should only be used for non-semantic styling purposes. Content should
be placed in either a 'section' or an 'article' tag.
-->
<?php include '_php/_includes/footer.php'; ?>
</body>
</html>
I would prefer the second option with top and bottom part php files. However, this can become complicate when you need to process context information within them.
For example imagine a top.php containing a table of contents including navigation and highlighting of the currently shown page. I guess, in this situation it would be more appropriate to use your first proposed approach. To alter the table of contents depending on your current page variable (e.g. 'aboutme').
I personally like a 3rd approach which is templating. Here are two options for PHP listed in order of my preference:
http://templatepower.codocad.com/manual/index.php
http://www.smarty.net/
Using template, I suggest you create a layout template which contains all the style and "header" and "footer" of your page. Then dynamically generate the "content" using one template per page. This allows you to use your url scheme of "index.php?page=aboutme". Also, by doing this you actually don't expose the bare naked content page to google.
I've found this is the most simple and maintainable way to build dynamic PHP pages with a shared header/footer.

Include file.php + id. Is this possible with php?

I've built my site with php include and the index is as follows:
Menu.php (menu system)
main.php (Index site)
footer.php (footer obv.)
Anyway, when main.php (index) opens, I've added a news script that uses $_GET to fetch the news-data from our mysql database. This generates an ID for each news, and it shows only a few characters of the full news.
So, I've added a link in each news that says "Read more" to expand the news, it looks like this:
Read More</p>
So, is there a way for me to include this site (replace news.php?id=x with main.php)?
It gives me syntax error when I'm trying to use <?php include in the link since it's already using <?=$row['id']?>.
The only help I got so far are people telling me to change the menu system to Javascript (Ajax, Jquery) but I'm not too familiar with this. Is there something I can do more simple than changing the menu to javascript?
Thanks for your help and understanding,
Have a great day!
Yeah, include basically adds the code of another file to the execution of the code you are using. So if you already have the variable in you index file can just use it in your menu file.
If the menu file has to be loaded on it own sometimes you can do a conditional for the existence of the GET, then the existence of the variable in your index file, and finally if none of them exit, set a default value.
leave your link as is, it's fine.
do not include main into news.
include only menu and footer into news.
Change it to main.php, get the id value on main.php, pass it to your news function.

Categories