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.
Related
In the thedigicraft online tutorial for developing dynamic websites, the main example traverses between pages using the browser's response to this: ?page=#. It also works on my project; when I type http://localhost/trying/?page=1, it goes to the project's homepage. I'd like to know how one sets what page opens with ?page=2 and other numbers. In the tutorial's project example, he then uses the _get array to define defaults, but there's no explanation for how his ?page=2 opens up that project's about page, for example. What am i missing - would appreciate any light on this.
In your example page is a GET parameter. The page being loaded is whatever is defined in the server's configuration. In apache it is documentroot. https://httpd.apache.org/docs/2.4/mod/core.html#documentroot
So your page probably is index.php. In that page you could have something simple like:
if($_GET['page'] == 2) {
echo '<title>Other page</title>';
} else {
echo '<title>Home page</title>';
}
Which would display Home Page as the title for every page unless http://localhost/trying/?page=2 is loaded.
You can extrapolate this concept to SQL queries for content loading etc..
I have three pages on my localhost 1 is my index page,2 is my universal header page which is under includes folder of and 3 is my html file which is under html folder.The header file is included in both the index file and html file like that...
for index.php-include("includes/header.php");
for html.php-include("../includes/header.php");
and my header has the link of index.php page that is (./index.php)
Now my questions is that when i open my index page and click on link of index page from my header it takes me to same index.php page but when in open html.php page and then click index.php page link from header it does not go to index.php page but it goes to this page-
(localhost/educational%20website/html/index.php) how to solve that.
And i also want to know that write now i am on localhost but when i make my site live is there any need to change the paths because i am making around 150 pages with your technique plaese so please answer me that kind of technique that is used for both localhost and on live
Your are including paths relatively, use a (absolute) base path in your index.php to fix this:
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/header.php');
One way is to define a variable or a constant for the site's url in the header.php file. Then in all your other pages, you could just use this variable/constant when you need to mention the other urls.
Eg(put this as first line in your header.php file):
define('SITE_URL', 'http://localhost/educationalwebsite');
Here, we have defined a constant named SITE_URL. Then in other pages, you are already including this header file. Isn't it? So, this constant will be available in your index.php, html.php and other pages.
And suppose for a link in your html.php file(to point to the index.php), you could use it like this:
Home
If you want to include link to the html.php file residing inside html folder, it would be like:
HTML
By using this way, if you are uploading the whole site to a live server, you only need to change one line, ie. the first line in header.php, where we have defined the SITE_URL constant. Just change it's value to the new URL of the home directory of your website.
Hope this helps
I'm attempting to create a dynamic website where certain site content loads in to index.php's body.
I currently have the site divided in to 3 sections: Header, Body, and Footer where each of these sections are dynamically separate from each other.
<?php
if(!file_exists('content/header.php'))
{
die('Sorry we are experiencing technical difficulties. Please come back later.');
}
else
include('content/header.php');
?>
<?php
include('content/body.php');
?>
<?php
include('content/footer.html');
?>
Now what I'm hoping to do is have certain page content load in to body.php if lets say I click on a hyperlink that says "register" so that a user my register them selves to the site, register.php will load its contents in to body.php.
I've tried searching around but I think I'm asking the wrong questions in Google search so I figured I'd explain my self here and hopefully someone would guide me in the right direction, Thank you for your time.
If you want it so the user doesn't get redirected look at AJAX.
Otherwise you could have the register redirect to index.php?page=register and your code be
<?php
$page = isset($_GET['page']) ? 'body' : someSanitizingFunction($_GET['page']);
include('content/'.$page.'.php');
?>
That's obviously a unsecure implementation but you'll get the idea from it.
You should also look at a template engine like Smarty, that may be what you're after.
To create simple layouts in PHP you can write pages like about.php, contact.php and register.php, then
you can put this code in your body.php
$whitelist = array("about", "register", "contact");
if(in_array($_GET['page'], $whitelist)) {
include($_GET['page'].".php");
} else {
include("error404.php");
}
Your url should be like index.php?page=register.
I also recommend to have a look in MVC frameworks with Smarty, Twig or TemplatePower. It is a good way to sort with layouts.
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.
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.