I split up my website using code refactoring with templates.
When I go to construct a basic link in my nav.php file or I suppose anywhere on the site for that mater, I need to grab multiple files as the site is templated -- let's assume for this example the user is going to services.php.
I believe I would need to attach all of these files in the link:
header.php
nav.php
pages/services.php
footer.php
How do I go about constructing a link to accomplish this? Could I make some sort of array and attach all of the files in the link and then loop through the array with a foreach statement and echo the results?
You don't grab the other files through the link. You link to one URL, which processes a single PHP file and that PHP file includes all of the other necessary files. For example, your link might be:
Services
Which will run services.php, which will then include the header, nav, footer, etc. For example services.php could look like this:
require( "header.php" );
// Some PHP code specific to servicer.php goes here.
require( "nav.php" );
// Some more PHP code specific to servicer.php goes here.
require( "footer.php" );
Using something like Smarty would be a nice and clean way of implemtning views in a MVC setup.
Related
I'm new at the scene and I'm using Bootstrap 3 and this template for the first time. I need help with the navbar. In the template each content.html has their own navbar.
I need the navbar in a separate file, because I do not want to make changes in every single content.html file.
In my old site without Bootstrap I worked with:
<?php
include("includes/navigation.html");
Can someone help me?
You need configure to put your repeated html data's in a single file and include that one into another.
Lets simple,
if your Project structure like this
project(folder)
db(folder)
Dbconnection.php(file)
js(folder)
jquery.js(file)
css(folder)
bootstrap.css(file)
index.php(file)
page1.php(file)
page2.php(file)
Create a new file with contents of your navbar html tags alone.
For ex. in your nav-menu.php contains
<navbar>
......
......
</navbar>
and save them into new includes folder in your project. So now your project structure will be like this one,
project(folder)
db(folder)
Dbconnection.php(file)
js(folder)
jquery.js(file)
css(folder)
bootstrap.css(file)
includes(folder)
nav-menu.php(file)
index.php(file)
page1.php(file)
page2.php(file)
Now you should include this file(nav-menu.php) in all the template files using php include method, like
include("includes/nav-menu.php");
Add this above code in all of your common files.
You can use
<?php include("includes/navigation.php") ?>
in this project too.
Fastest way that comes to my mind is to just include the code you need to change for every page in a separate switch/case and call the different cases at the top of the page that is including the navbar.
Just note your navigation file needs to be .php, not .html like your example.
Thanks for reading!
I am managing a header with links using a PHP include. It is within a folder /includes/header.php.
Here's an example of what header.php looks like:
<nav>
<ul>
<li>Home</li>
<li>Page</li>
</ul>
</nav>
When I add the include to a file within the root directory, like /index.php, I add it like so: <?php include_once("header.php"); ?>. This all works fine, and the links point where they need to.
When I do the same thing but with a file in a subdirectory, for instance a file called /foo/page.php I will add the include like this: <?php include_once("../includes/header.php"); ?> - this way it grabs the file correctly.
My problem is that all of the links in the header.php file aren't going where I want them to. I found some information about using a set environment function in .htaccess, but I don't know what to make of it.
If you have an answer to this problem I'd love to hear it! Thanks!
Start all the links in the header from the root web directory.
Just do;
"/index.html"
"/subdirectory/link.html"
So basically just start all the links with a forward slash, as without it, it will look for the page within its current directory.
You can set the base url in your HTML head.
Store the base url of your application in a config file or database and then use it to build absolute links not relative ones. For example you have a file like config.php:
<?php
$baseUrl = "http://yourdomain/yourapp/";
And in header.php:
<?php include_once("config.php"); ?>
Page
It may seem inconvenient having to edit a file in case you move your application, but this way your links will work in any directory any time, and as your application grows there will be some other things like DB access that also have to be changed if you move your application, and can be stored in the same config file.
I'm certain I can use PHP to accomplish this task, but I'm not sure how.
What I currently have is a faux news (ha ha ho ho) site for practise here.
http://puu.sh/402Rl.png
For Browse News, I would like all html documents within a specified folder to be shown in the format I have
SAMPLE
<p class="content centeralign">
8.12.13 <!-- ARTICLE NAME -->
</p>
<hr noshade></hr>
Although it's not much, setting up a way to do this automatically would save some time.
Here is how I would imagine the logistics behind this would function ----
All HTML files will be listed in a folder
They will all have a consecutive order based on the date they were created (e.g. 1.html, 2.html, 3.html etc.
PHP would find each document and add it in the right order
A bit inside the file would define the title (meta tags?)
That seems a really bad way of doing it, but anyway.. You want to be you want to be using the directory functions. Specifically http://www.php.net/manual/en/function.readdir.php
A good idea would be to set up a MySQL system for this, but it is achieveable with PHP only.
You could get all .html files in the folder with glob, and then include them with this code:
foreach (glob("/htmlfiles/*.{htm, html}") as $filename) {
include "$filename";
}
The nice thing about this, is that it's sorted alphabetically and numerically too.
Edit: You would then use this system twice, with two folders, one for the meta tags/title, and one for the page itself. Again, not the best way to do it. You should really check out a CMS.
If you used MYSQL, you can still get all html files from the folders as you are now, but just use MYSQL to make simple basic references, such as the filename, date, category, etc. Then you don't need to use complex (and likely failing) file and directory code to determine when a file was created and which order to serve them in. The DATE in your MYSQL would be when to serve them.
To answer your meta questions, I would have a header.php file with all the meta data in for the site (doc declaration, titles, css links, etc) and then each individual file could have a variable to pass to the header when it's included.
eg
File: about.php
$PageTitle = 'About';
include_once('header.php');
<some more code>
<h1>$PageTitle</h1>
File: 1.php
$PageTitle = 'News about something';
include_once('header.php');
<some more code>
<h1>$PageTitle</h1>
File: header.php
usual code, doc declaration, head, etc
<title>$PageTitle</title>
So at the start of each file you declare what the title will be then include header.php. The title is used on the meta title (so your browser and tab etc) and the var in the file can also be used on headers (ie h1, h2) and links if needed.
you could do all this without MYSQL still, but using a database to even just reference things like date_of_creation - last_update_date - author, etc, could save you headaches, but is up to you how you want to do it and what skills you have etc.
Hello I'm having my first serious go with PHP to create a sample script for my self. It has a basic structure, in my root folder I have:
index.php
core folder (holds most of my php function files)
includes (holders my header.php and footer.php)
sites - (sites has 3 further folders site A, B, C)
CSS
js
All pages are made up by taking a header.php and footer.php from the includes folder and then each page has its own content in the middle. The header.php contains (as well as basic html and links to javascripts stylesheets ect) includes from core folder like so:
include_once '/core/connect.php';
Now these all work great using the index.php which provides links to the 3 different sections of the site, sitea, siteb and sitec.
But when you navigate out of the document root to say /sites/sitea/index.php all those links are now broken.
What is the best way to go about building the links in the header.php section so they are relative site wide no matter which folder you are in?
The idea behind this is that you do only have ONE file for each process.
So process all pages through index.php
index.php would contain, for example,
require('header.php');
include('content.php');
require('footer.php');
That way, it won't break the site if your content doesn't show.
Your index is always loaded from the same path, so header/footer wouldn't change. Just content.
When you're including you want to use a real path, not a relative path...
require_once ($_SERVER['DOCUMENT_ROOT'].'/includes/header.php');
/* something happens here */
require_once ($_SERVER['DOCUMENT_ROOT'].'/includes/footer.php');
The best way is to always use physical path from wherever you are - this way every page that include other page with includes won't get break:
PHP 5.2 and down:
require(dirname(__FILE__) . '/core/connect.php');
PHP 5.3 and above
require(__DIR__ . '/core/connect.php');
I want to use php to easily maintain my website, but I simply can't figure out the language - I've found some tuts online, and some other questions here, but none help me.
I've divided my site into some .php files, header/footer and such - And using
works fine..
Now I want the content of my site, to update according to which menu I click on at my site.
http://dawtano.com/pp/
If I click on "about" I want the "Hello World" to open inside my content div, but I can't get the right php code to do it.
I think you should do this---
Note: This will only work if the CSS styling are on the current directory! ()
<div>
<?php
$html_page = implode('', file('http://dawtano.com/pp/'));
echo $html;
?>
</div>
Hope this helps!
well currently your links are taking you to a separate page entirely. So why not just code it so that your include file is specific to the page. i.e, on about.php, use something like
include 'about_content.php
in your contetnt div.
If you're looking for your content to load dynamically into the content div you'll need to look into using ajax to fetch the content pages.
One popular way to construct the site is to have a single php script which displays content based upon a $_GET variable like 'page' or 'content', and then make the link as:
'http://dawtano.com/pp/index.php?page=helloworldcontent'
Using this method, you would need to check if the variable ($_GET['page']) is set using isset(), and then make sure the string is safe... as anybody with a browser could just type in some mumbo-magic script and hijack your site:
'http://dawtano.com/pp/index.php?page=somecleaverlycraftedhax'
Once it exists and is safe, add the '.php' to the file name and include that file... if it exists! If it doesn't exist, then you will need some code to handle that, probably by displaying a 'File not Found' message, or redirecting home, or something.
I prefer not to do this because it is a pain to make safe, and I feel like it is pretty ugly. What I do instead is put all the header/footer/navbar/title bar scripts into seperate 'display' functions, and put them in another file.
Then include this file with the function definitions, and call all the 'display' functions to set up the page. So every php script in your site might look like:
<?php
include 'html_display_functions.php';
/* put lines here to parse $_GET and $_POST, session_start()/$_SESSION, etc... */
print_html_pre_content();
print '<p>Hello, world!</p>';
print_html_post_content();
?>
Since every script will have this structure, you can just create a template file once. When you want to create a new page for your site, copy the template, rename the copy to the php filename you want, and add content between the two print functions.
You also keep the ability to modify the header/footer/navbar/title bar for the whole site in a central location, namely the included file with the functions.
You might be looking for some sort of Template Engine which allows you to create your pages out of variable parts. You could have a look at TBS, which is more or less what is suggested by the name. But there is a whole lot more engines out there which could do the job.
If that's already too much over the top, maybe Apache SSI (Server Side Includes) are a try for you.
A little suggestion from my side, I am often using Apaches mod_rewrite in connection with a single controller.php file. Apaches mod_rewrite will then send all request to the controller.php which will fetch the appropriate page parts for the requested page using TBS and return the respective page. So you have the controll of the page in one location only.
To your original question about.php could look like:
<?php
include('header.php');
?>
// original page content as html for about.php
// assuming header ends with the starting div <div> where you like the content to appear
// and footer starts with the closing div </div>
// if you need variable content here, simply use <?php echo $your_variable ?>
<?php
include('footer.php');
?>
The best way would be to use a switch statement:
http://php.net/manual/en/control-structures.switch.php
Something like this:
<?php
include("header.php");
$page = $_GET['page'];
switch($page)
{
case "about":
include "about.php";
break;
case "faq":
include "faq.php";
break;
case "help":
include "help.php";
break;
default:
include "home.php";
}
include("footer.php);
?>
Then just make all of your links look like this:
http://www.example.com/index.php?page=home
Just replace home with the correct page.