Dynamic site, using include to get content in specific div , how? - 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.

Related

PHP and web page displaying

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.

How to bring previous page specific text on next page

I have 2 pages of html: a.html & b.html.
a.html contains text "hello" and a link to b.html.
When I click the link to b.html, I want the "hello" text to also come on specific place in the b.html page.
How can i do this? Kindly let me know.
To bring it along, you'll need to pass it somehow. It could be something you keep in an input element on the screen, then when you submit it (as a form) it will be available as a $_REQUEST element. You could also do this by putting your desired welcome message in a hidden element and submitting it in the same manner.
Alternatively, you could put the value you want in a URL string manually. For example, the link in a.html to go to b.html could be called by:
<a href="b.html?welcome=welcome%20to%20the%20show">
Doing things on the URL string is more PHP than html, and if your server's not setup correctly you may not be able to get values from the URL string in an HTML document. So, in that case it's probably easier just to make it a .php file. Again, not that it can't be done, but it might not be setup to work that way by default.
.html pages are static, so you'd have to copy the desired text manually into the second page.
If you want to do it via PHP, then the simplest way is to put the common text into a file, like:
my_html.html:
<p>This is the common text which should be included in both pages</p>
and
a.php:
... lots of stuff ...
<?php include('my_html.html'); ?>
... more stuff ...
and
b.php:
... other stuff ...
<?php include('my_html.html'); ?>
... more other stuff ...
It sounds like you want to "include" some common text between multiple pages. If you're limited to client-side functionality, it's possible to do this by writing some Javascript to populate an HTML element with a string via DOM, and then include that javascript on both pages. Both HTML pages would have to define the target element.
If you have server-side technology flexibility, I would suggest you look at PHP. Implement both pages as .php pages, and use include() to include a common file (e.g. common.php) in both pages.
You could use php sessions and store the text there so e.g.
a.html
<?php
session_start();
$_SESSION['session_name']="Hello";
echo $_SESSION['session_name'];
?>
b.html
<?php
session_start();
echo $session['session_name'];
?>
A session is more or less a cookie and what you are doing here is storing the string "Hello" in a cookie name "session_name". You can then display the cookies content by using the php echo command :)

include header vs include page

I'm currently using include 'header.php' and include 'footer.php' in every page, and as far as I know that's how most people do it. I thought of a way that I personally thought would be better, however. I thought of making index.php, then in the index include the page. This would both eliminate the need for a footer and eliminate the need for include twice in every page. I'm really new to php, however, so I don't know how I would do this. I tried using POST and GET methods, but it doesn't seem to work. What I want to achieve is including pages in the header using a URL such as http://mysite.com/index.php?page=history and then load history.php. If I need to clarify something, just ask. Sorry if I don't accept an answer right away, I'm really drowsy. I'll get to it when I can.
It is not a problem if you include 2 pages in a file, like header.php and footer.php...
Just writing 2 lines of code in each page is not a matter.
You can decide what pages you want to include dynamically in every page by using if statement, instead of passing the page name in the url.
If you'll do it via index.php, you will no doubt do it wrong.
Nothing bad - every newbie does it this way.
Just because you're thinking of includes, while you should be thinking of templates.
You can make it via index.php, no problem. But there should be not a single HTML tag in this index! As well as in the actual page.
No matter if you're doing it in separate pages or via index.php, the scenario should be the same:
Get all data necessary to display particular page.
Call a template.
Thus, your regular page would look like
code
code
code
include 'template.php';
while index.php would look like
get page name
sanitize page name
include page
include 'template.php';
now you can decide what to choose
First off i agree with Meager... Take a look at soem frameworks. Most will use a two step view which essentially does this althoug in a more complex and flexible way.
With that said it would look something like this:
<?php
$page = isset($_GET['page']) ? $_GET['page'] : 'home'; // default to home if no page
if(file_exists($page.'.php')) {
// buffer the output so we can redirect with header() if necessary
ob_start();
include($page.'.php');
$content = ob_get_clean();
}
else
{
// do something for error 404
}
?>
<html>
<head></head>
<body>
<?php echo $content; ?>
</body>
</html>
You could get more complex than that. One thing you want to do uis make sure you dont blindly assume that the page in the $_GET var is safe... make sure the file exists on your server or otherwise sanitize it...

Code to detect current page

Is there a php or javascript code that can detect the current user's page and then add <a class="active"> to an item in a ul (my menu). I include my menu in my pages with PHP include so making change is easy; I only have to edit it once. But, with that method, I can't individually set each page to have a class="active". How can I do this?
You several options, e.g.,
The part that handles navigations can read the request URI directly. This can be done by reading $_SERVER['REQUEST_URI'] (don't forget this may include the query string).
At some point, you must know what page you're on, because you decide which content you display based on that. You can define a function that handles the navigation markup and pass it the name of the current page so that it knows which one it is.
If I'm understanding your question correctly, what I usually do is set a variable before I include the header, like
$current = "home";
And then in the header I'd have an if statement in each link
<a href="/home" <?php if ( $current == "home" ) { echo "class='active'" } ?>>Home</a>
Could be ways to improve it, but it's simple if your menu isn't too big.
In PHP, you can look at the value of $_SERVER['REQUEST_URI'].
In JavaScript, you can examine window.location.

Preventing jQuery from Loading

If jquery is added in globally used header.php across the site then How to stop to load jquery library only for those pages of site which doesn't need actually? If we can't use more than one header.
purpose of question is to not to penalize those page with slow loading which actually don't need.
Your site shouldn't need more than one global-header, if you opt to even use headers to begin with. If it does, just include jQuery on all pages. It's a small cached file, it won't hurt the browsing experience.
By using the google-hosted version, it may be the case that many of your uses already have it cached before they even reach your site.
I have been guilty of pounding my fist into the nail while asking everyone else to move the hammer that's in the way...
Why not tackle the problem from the other end and use jQuery to optimize the first load?
If you have big pages that are already taking a while to download, why not section off the less-performant areas and use $().load() to fill those in?
The page will load quicker (better user experience) and you don't have to be adding any additional processing to pages that don't need it.
Cheers,
-jc
assuming you are loading the jQuery file from a correctly-configured webserver (or from google's CDN), it will be cached and not re-downloaded on each page. Assuming a visitor will hit at least one page on your site that needs jQuery then you really won't gain anything by trying to remove it from loading on pages that don't use any javascript features from the library.
First, use the compressed jquery for production. It's much smaller. Second, IIRC, once jquery is downloaded with the first page, it will be cached and won't need to be retrieved from your server for each subsequent request.
Otherwise, if you really need to explicitly load jquery only on those pages that need it, you would have to have some way for the body of your page to tell header.php that it doesn't need to load jquery. If header.php is loaded before "body.php" then that's pretty hard to do without some fancy output buffering or such.
If you're using a templating system like Smarty you could have a conditional in the master page template to check a $loadjquery flag that you set in body.php before sending the whole page off to be rendered. But that's complicated too.
Your question is very general, some specific would be great, maybe even a link to the site. Personally if you are using a CMS I would try to make some sort of "flag" for that page, or if you are simply loading a page and then loading the header from that page, insert a variable before you load the header and use that as your flag for loading jQuery.
An example:
If a user wants to see www.mysite.com then the following file would be loaded: www.mysite.com/index.php with the following code:
<?php $needJQuery = true;
include('header.php');
echo 'content will go here';
include('footer.php'); ?>
header.php would include something such as this:
<?php if ($needJQuery) { ?>
<script src="/jquery/jquery-min-3.2.1.js" />
etc. for all the content that you need above/below.
<?php } ?>
For the pages that don't need jQuery loaded, you would either leave $needJQuery undefined or you would do as follows:
<?php $needJQuery = false; ?>
Hope this helps,
As stated earlier, modify the header file so it'll check for the presence of flag variable and only output the jquery headers if needed.
If you don't have the ability to modify the header file, you could load it with output buffering turned on and filter the output before it heads out to the client:
<?php
ob_start();
include('header.php');
$header = ob_get_flush();
$cleanheader = some_operation_that_removes_the_jquery_script_tags($header);
echo $cleanheader
?>

Categories