Change the content of webpage when clicking different links - php

I'm not sure how describe my question perfectly or how to begin. But what i can start with is that i'm making a website in PHP for my school and i've never used php before. And what i need is to only change one part of the page i.e the sidebar. or the content, depending if the user clicks i.e school, contact etc.
on my index.php the sidebar shows the news. But in my navbar if you click i.e useful links then the sidebar should change from showing news to showing a couple of links.
And if it is possible, if you then click on one of the links it should only change the content under the navbar and the sidebar should still show the useful links.
<?php
include('include/header.php');
include('include/nav.php');
?>
<div class=content>
<h1>Heading 1</h1>
<p>Text goes here</p>
</div>
<?php include('include/nyheter.php') ?> <--not part of code! this is the sidebar
<?php include('include/footer.php')
For now, i've only got the news to be shown in the index.php using <iframe>code. but i want to get rid of the <iframe> and use <div class=sidebar">or use ID instead of class.
I've search around to find useful information, but most of them says something about AJAX and JQuery, which i don't know what is or even how to use it =(
If you need more information, just ask as spesific as possible, so that i can give the right information =)
EDIT
I've uploaded the entire website to my google drive so that i could share it with you.
as you may see, i've created a bunch of new .php files (most of them was from some other versions that was created in html, and are now converted to PHP.
The link is: https://docs.google.com/folder/d/0BxsqMLXovlRHdmFlOFYzMjlzRHc/edit?usp=sharing
by the way, the website is in Norwegian.
I hope this information can help you to tell me how to solve my question.
remember, i am totally new to PHP and other programming languages other than HTML & CSS.

You could use if sentence to change the page content according the url
for example
if($_GET['page'] == "about") {
echo "about page content";
}
elseif($_GET['page'] == "page1") {
echo "page1 content";
}
and so on
then you just put links yourpage.com/index.php?page=about into your menu and so on

<?php
// get current page url
$page = basename($_SERVER["REQUEST_URI"]);
if ($page === "index.php"){
// display news info
}else if($page === "usefulllink.php"){
// display userful links
}
?>

Use the php $_GET variable to set which page and navigation you want to use. This is accessed via variables in the url.
For example: www.yoursite.com?page=home&nav=first
You can then include in the php
<?php
$page = $_GET['page'];
$nav = $_GET['nav'];
//set some defaults
if(strlen($page) == 0){ $page = "home"; }
if(strlen($nav) == 0){ $nav = "about"; }
//Then using if statements you can select content and navigation
if($page == "home"){
//print navigation bar with selected link
if($nav == "about"){
//print content for the selected navigation
}
}
?>

Related

Multiple HTML Pages in one file

Hi guys I am making a website for a school project, but to keep in organised, I want to have all pages in one document, and when you go to the site the default page is index.php, and the other pages are linked like this: index.php?p=page
the problem now is, that I had this script, but don't know where I have it. I'm not skilled in PHP too, I got it from a friend of mine. It was like this:
?>
<php? some php code ?>
<html tag etc>
<head> Head stuff </head>
<Nav> <Header>
<php? some php code where I filled in what the link was of the content under it ?>
< The content of the page, excluding the header, navigator etc because that was above here. >
?>
Then I could do the same thing over and over again, and just change the PHP code and it worked.
<footer stuff>
</body>
</html>
I know that there are many scripts for this, and I searched Google for over an hour, but with no succes.
-Tristan
A very basic example of how it works :
<?php
$page = $_GET['page'];
if ( $page == 'one' ) {
echo 'This is page one!';
} elseif ( $page == 'two' ) {
echo 'This is page to';
}
// http://yoursite.com/index.php?page=one
// Outputs 'This is page one!'
Learn little more about GET request right here : $_GET
This will help you:
Serve multiple pages from 1 PHP file?1
If you want to make something like youtube.com/?watch=XXXXXXXXX
so you need just to change XXXXXXXXX to display multiple content.
but you still can use :
One-page navigation system.
Display / Hides Pages With JS
Example Of JavaScript:
we have website1 as a website
we have a - b - c - d as pages
js will hide all pages exept the page you want to show.

Is there PHP code I can use to automatically detect which link should use a CSS active property?

Sorry if that wording was confusing - I am very new to PHP, as I am designing a website for my small organization and only have previous experience in CSS and HTML.
Basically, I included the header and footer in the site using a PHP include so that it would only take one update to change all the links, as it is a pretty big site with a lot of pages that would take forever to update in HTML.
The problem I am having now is that since the php include is only one page, no matter what page you are on, the category in the header menu that is 'active' and highlighted blue is the one that I originally copied the menu from to create the .php file. Obviously, I want it to be whatever menu category you are actually in, and not just that same one every time. I am sure there is a very easy way to do this in PHP, but I was having trouble finding anything because I wasn't exactly sure how to word the question when I searched for it. Thanks in advance to anybody who can help!
Simplest method, using a global variable and a lot of conditional logic:
Any file on your site:
<?php
$current_page = 'name of page here';
include('menu.php');
... page content/logic here ...
menu.php
<?php
if (!isset($current_page)) { $current_page = 'home'; }
?>
<ul id="menu">
<li<?php ($current_page == 'home') : ' class="current"' : '' ?>>Home</li>
<li<?php ($current_page == 'foo') : ' class="current"' : '' ?>>foo</li>
etc...
</ul>
I don't know how your menu look like, but you can test the current page with PHP_SELF
<?php
$urls = array('index.php', 'include7.php', 'about.php');
$current_page = basename($_SERVER['PHP_SELF']); // if for example you are in /folder/index.php ; the basename will output only 'index.php'
?>
<?php foreach ($urls as $url): ?>
<?=$url;?>
<?php endforeach; ?>
In my example, I was in include7.php file, so in the output, only it was colored green, other 2 were colored red.
The method I prefer of doing this uses no PHP logic at all. In each of your pages, add a class to a high-level element, such as body:
<body class="home">
Then in the menu, add a matching class name to each menu option:
...
<li class="home">Home</li>
<li class="whatever">Whatever</li>
...
And then in the CSS:
body.home li.home a,
body.whatever li.whatever a
{
color: blue;
}
...or whatever style you want, using whatever selectors you prefer, the key being that the CSS looks for a matching body and menu item classname, and therefore only highlights the item for the current page.
This means that there's no PHP logic whatever. When you create a new page, add a body class for it, and a matching entry in the menu, and add it to the CSS.
I tend to use this method in situations like yours, when PHP isn't being used as a server-side language or a CMS system so much as a very simple template-builder. It keeps the server-side code very minimal, which matches the rest of the setup.
The way I would do it:
1) detect the page you're in from within PHP
2) update the parts of your header/footer that depend on the page you're in
detecting the page you're in
The $_SERVER['PHP_SELF'] superglobal variable will give you the path of the current page relative to the site root.
For instance, if your page is http://whatever.site.com/about.php, it will give you /about.php.
It means you can use the name of the current page as a key to change whatever behaviour from within your generic header/footer.
Depending on the way your pages are stored, you can easily synthetize a simple identifier for the current page (something like "index", "about", "contact", etc).
You can then put that in a global $PAGE variable allowing you to identify your current page from about anywhere in your PHP code.
updating the variable parts
If you need some item to change according to the page you're in (typically highlight the current page in your header), you can easily do it by checking $PAGE :
<li class='.<?php echo $PAGE=='about' ? 'selected' : 'unselected' ?>about
<li class='.<?php echo $PAGE=='contact' ? 'selected' : 'unselected' ?>contact
You might also want to do some less redundant code generation, for instance:
<?php
function menu_items ()
{
global $PAGE;
foreach (array ("home", "about", "contact") as $page)
echo "<li class='.(($PAGE==$page) ? 'selected' : 'unselected')."'>$page";
}
?>
<!-- header menu -->
<ul><?php menu_items(); ?></ul>

Suggestions for iframe which deliver content according to the parent url?

I hope someone can help me with this issue. So, I have a large website (1000+ webpages) and I want to put an iframe window on every page. My aim is to make this iframe load specific content according to the parent url address of each page on my website.
For example, when the user on my site loads www.mysite.com/Page-1, I want the iframe window to show ExternalPage-A, or when the user loads www.mysite.com/Page-2, I want the iframe to show ExternalPage-B.
Can someone give me an advise how to do it? Should I make a separate php document that will load in the iframe, where to put all the external links? Then, I should take somehow the parent url and tell the php document which link to be displayed. I am not very familiar with php and that is why I am asking.
Thanks!
Don't use $_SERVER['HTTP_REFERER'] because it won't always contain any data. This is set at the user's end, it cannot be trusted and isn't always set.
You would be better with your referring URL's and corresponding iframe URL's in a database. But putting a database to one side for a moment you could do something like this:
<?php
function get_iframe_url ($page) {
$page = parse_url ($page, PHP_URL_PATH);
/* Substitute the below IF statement for a database lookup
if you have lots of pages */
if ($page == '/news.php') {
return 'http://www.bbc.co.uk/news/';
} elseif ($page == '/contact.php') {
return 'http://www.yellowpages.com/';
}
}
?>
Then you can include this on every page:
<iframe src="<?php echo get_iframe_url ($_SERVER['REQUEST_URI']); ?>"></iframe>

How can I make php pages appear inside a div?

How can I make PHP pages appear inside a div called contenct, whenever a menu button is pressed?
For example I should bring product menu product.php page to the document body and head, footer will continue well without causing alteration.
I've tried include, but how could it put the link from appearing on the container div?
I hope I have understood!
leave a sample image:
You can use PHP's include() for that purpose.
An example:
<?php
include('head.php');
include('menu.php');
//your normal body code goes here
include('footer.php');
?>
include('file.php'); will read the contents of the php file and it will add / paste the contents in the main file. In this particular example, it will load the contents of head.php, menu.php, and footer.php.
Hope this helps!
Based on your question, im assuming you would like to page to NOT refresh when the navigation button is pressed, and to simply load the contents of a php file into the content area.
The smart way to do this would be to look into using jquery with AJAX, or LOAD to load external content as needed.
PHP has no direct way to manipulate the clients browser without refreshing or loading another page.
if you must do this in php, a very simple approach would be to have each menu item link to a query string. I.E.
HTML: menu.php
Home
Products
Clients
Contact
and to have the index.php file itself run a switch and case I.E.
PHP: index.php
<?php
if(isset($_GET['page']) && $_GET['page'] != '' ){
$page = $_GET['page']; // page being requested
}else{
$page = 'home'; // default page
}
include('head.php');
include('menu.php');
// Dynamic page based on query string
include($page.'.php');
include('footer.php');
?>
This is a very basic example, but might get you pointed in the right direction.
hope this helps
In the php file the user is viewing, put:
include('home.php');
this will execute the contents of the home.php file in the current php file
You could use AJAX and on the event's finish use result $('#content').empty().append(result)
I'd like to add AMAL's answer...
<?php
require('function.php');
include('head.php');
include('menu.php');
//Create a function here to fetch pages
echo '<div id="content">';
echo get_page();
echo '</div>';
include('footer.php');
?>
Then create a function page and add a function that parse the url and gets the php page. (quickly made function, but should do the trick, or help)
function get_page()
{
//http://example.com?page=home
$page = $_GET("page");
$directory = "pages";
if($page){
//security check (anti-hack)
if (!strpos($page,".")&&!strpos($page,"/")&&!strpos($page,"\\")&&!strpos($page,";")){
$path= $directory."/".$page.".php";
if (file_exists($path)){
require ($path);
}else{
echo "<p>Sorry, but that page does not exist.</p>";
}
}else{
echo "<p>Sorry, but those characters aren't allowed !</p>";
}
}else{
$path = $directory."/home.php";
require ($path);
}
}
Then your php page should look like this:
<p>This is the home page</p>
Add this piece of code in the content area of your content area in main file...
<li>BLOG</li>
<div class="container content">
<?php
if(isset($_GET['page']))
{
$page_name = $_GET['page'];
include("/".$page_name);
}
?>
</div>

How To Set CSS Classes On HTML Tags Which Are Included In The Page With PHP Include's

I'm building a website which uses a lot of repeated styles and HTML tags, such as the header, the navigation bar at the top, and the footer at the very bottom. Instead of typing all of that repeated code in again and again, I'd like to use PHP include statements to include everything that I need.
While that's all well and good, this poses a problem with my navigation bar. I use a CSS class attached to the navigation link to let the user know what page they're on (using a different background color). On each page, I manually specify which link gets that special class. If I were to simply include that content via a PHP include statement, I couldn't manually specify the class.
My question is: how would I be able to do that? Either by using a separate PHP script to find out which page the user is on, and then specify the class to style the link appropriately, or by using JavaScript to do the same thing? Or maybe there's something else that I'm missing? Who knows! And as such, I ask!
An easy way to do it would be to add a PHP variable like $page before the include and then use this variable in your included menu file to determine which menu item needs highlighting.
So in the file with the includes:
$page = "home";
include("menu.php");
And in the menu file:
<ul>
<li <?php if( $page == "home") echo 'class="active"' ?> >home</li>
<li <?php if( $page == "about") echo 'class="active"' ?> >about</li>
<li <?php if( $page == "contact") echo 'class="active"' ?> >contact</li>
</ul>
The example above isn't the tidiest or most optimal solution, but it gives you an idea of how you could achieve what you are trying to do by using PHP variables.

Categories