I have noticed that in a couple of sites they can run the whole site from just the index.php, I also noticed the same effect in phpmyadmin to some extent.
An example of a site that does this is Try Open School, it is a demo site, and you can log in with (admin as password and admin as username, or teacher, teacher, or student, student). If you notice all that changes about the URL is the Query String.
I looked through the site and all I can Image is a whole lot of codes on just one page, But I do not think this is the case. I tried mine and I did something like this..
<?php
if($_GET['ref'] == 'home')
{
require_once("home.php");
}
elseif($_GET['ref'] == 'profile')
{
require_once("profile.php");
}
?>
I used the URL Query String to make reference to the different pages and to make other logical decisions.
I did a whole lot of this on the page, and the page contained several hundreds of if else statements. I do not know if I am doing what they did on their site.
I love the concept and would like to implement that kind of stuff on my own site.
I would appreciate all help on how to go about this. Thanks
If $_GET['ref'] is the only parameter for selecting page. a php switch, would probably be a better solution.
If you want to get fewer lines of code in your index file, you could put the switch in a separete php file!
Like this (index.php)
<?php
echo "<div id=header>Someinput</div>
<div id=menu>Some menu input</div>
<div id=site>";
require("switch.php");
echo "</div>
<div id=footer>Some footer input</div>";
?>
Then (switch.php):
<?php
switch($_GET['ref']){
case "page1":
include("page1.php");
break 1;
case "page2":
include("page2.php");
break 1;
default:
include("404.php");
}
?>
Related
I’m currently working on a small web project, and my plan is to build it all from the bottom up – to start by making a functioning website which uses only PHP, HTML and CSS, and then, if the user enables it, progressively enhance the website through the magic of Javascript, more particularly - jQuery. However, I’ve been running into some problems on the PHP side of things, and since I’m merely novice when it comes to that scripting language, what I can’t figure out is simply:
How to implement one PHP switch include navigation inside another one?
To better clarify what I’m trying to achieve, let me show you a little mock up of what my site structure looks like:
Header.php (Header)
Page1.php (Content page 1)
Page2.php (Content page 2)
Page3.php (Content page 3)
Page3.1.php (Content page 3 sub-content 1)
Page3.2.php (Content page 3 sub-content 2)
Page3.3.php (Content page 3 sub-content 3)
Footer.php (Footer)
As you can see, when it comes to this mock up, it’s on page 3 where I intend to nest one php navigation inside another one. This to enable some slideresque sub-navigation on that very page, but my efforts in doing that have so far been futile at best.
As far as code goes, this is what I’ve managed to scrap together.
<a class="link" href="index.php?id=page1">Page1</a>
<a class="link" href="index.php?id=page2"> Page2</a>
<a class="link" href=” index.php?id=page3">Page3</a>
<?php
switch($_GET['id']) {
default:
include('page1.php');
break; case "page2":
include('page2.php');
break; case "page3":
include('page3.php'); // *Nested navigation goes inside this page
}
?>
*Inside of page3.php I’ve put pretty much the same code, only that it links to the sub-pages of that page (page3.1.php, page3.2, and page3.3.php ) instead.
Unfortunately, this doesn’t seem to work – first of all, when I enter localhost/index.php I my web browser I get the following notice:
Notice: Undefined index: id in C:\xampp\htdocs\php-navigation\index.php on line 26.
However, when I click one of the links, or enter a url like index.php?id=page1 I don’t get that notice. So it seems like I need to define the ’id’ variable, but where and how to do that?
In addition to that, the nested navigation on page 3 doesn’t work at all, instead, when clicking on of the links to the sub-pages, I get redirected to the index page. I might have to do with the URL:s , but then again, what do I know?
Please help me figure out how to make this work - all help, even just a slight nudge in the right direction, is welcome and much appreciated- thanks in advance!
/ Johan Wendesten
The warning you get is about an undefined index - you are trying to access an element of an array that does not exist. You need to change
switch($_GET['id']) {
to
if (isset($_GET['id'])) {
$id = $_GET['id'];
} else {
$id = "page1";
}
switch($id) {
For page 3, first off you may have an issue here:
<a class="link" href=” index.php?id=page3">Page3</a>
You'll notice an extra space and a weird quote (” vs " - you want ").
For the subpages, we'd have to see the URL's generated, but I'm guessing it has to do with the links as well - to keep with the way you are doing it, I would probably have the link look like:
<a class="link" href="index.php?id=page3&subid=2">Subcontent 2</a>
and then in the php:
if (isset($_GET['subid'])) {
$subid = $_GET['subid'];
} else {
$subid = 1;
}
switch($subid) {
my freind english is not my first language so i am reading it again, but one mistake i found in your code is this.
for the page three thats why you are having problem for the page3 link
<a class="link" href=” index.php?id=page3">Page3</a>
the href part is wrong this one href=” it should be this href=" means your href part should be this
href="index.php?id=page3"
and to remove the error for id
add if statement like this
if($_REQUEST=='GET'){ //all of your switch statement will come here }
so your code should look like this
<?php
//start of if statement
if($_REQUEST=='GET'){
switch($_GET['id']) {
default:
include('page1.php');
break; case "page2":
include('page2.php');
break; case "page3":
include('page3.php'); // *Nested navigation goes inside this page
}
}//end of if statement
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 trying to find the best way to have a DIV be hidden if the url is something. For example I have 2 sites using the same template but only 1 of those sites I want to display something.
So site A is domain.com
Site B is site.domain.com
I want it so if site.domain.com is where people are at then do not show DIV ID="hide". I also need it to have this work for not just that specific URL but for anything that comes after it so site.domain.com/aboutus.php, site.domain.com/contact.php etc....
I would like to do this with PHP or JS.
Just check HTTP_HOST:
<?php
if($_SERVER['HTTP_HOST'] != 'site.domain.com'){
echo '<div>contents</div>'; // display div, he is not on site.domain.com
}
?>
You can probably do something like:
<php if($_SERVER['HTTP_HOST'] == 'example.com'): ?>
<div>This is example.com</div>
<?php else: ?>
<div>This is NOT example.com</div>
<?php endif; ?>
But I think you'd really be better off creating some sort of site1_settings.php that gets included on every page in one site, and then site2_settings.php that gets included in the second. For one thing, this will make it much easier to test your code locally.
I usually use strpos for this.
if(strpos($_SERVER["HTTP_HOST"], "site") !== FALSE)
{
// The user is on site.domain.com
}
else
{
// The user is on domain.com
}
Here would be my javascript implementation. Note: jQuery
if(window.location.indexOf("textinurl"))
{
$('#DivId').hide();
}
I have a custom CMS here entitled phpVMS, and I want to exclude a piece of code, a banner for a single page. phpVMS is steered using templates, for instance, the main template that codes the general layout for all pages is entitled layout.tpl. So, like I said, this displays whatever is in the template, on all of the pages. I have however created a special control panel, and therefore require to exclude the banner, because it slightly destroys the theme of it. Is there any PHP code that excludes a piece of code on a single site? I need to remove a single div...
<div id="slideshow"></div>
...on a single page.
Basically, I could create a new template but this is a very long winded and unefficient way within this CMS, and the final result isn't that great - because I can't reinclude the mainbox div which is the box defining the content on the centre white bit of the theme - it's already in the layout.tpl.
I hope you can somehow help me, hope I've included enough information there.
Thanks.
I don't think you can do what you're asking in PHP, but you might be able to do this on the client-side, by either hiding the div (CSS display:none) or by removing it with JavaScript. You might be able to do something like:
<?php
include("layout.tpi");
if (condition)
{
// Javascript:
echo "<script>document.getElementById('slideshow').style.display = 'none';</script>";
// OR jQuery:
echo "<script>$('#slideshow').hide();</script>";
}
?>
If you use a variable to determine you don't want to include the div, you could do this:
<?php if ($include) { ?>
<div id="slideshow"></div>
<?php } ?>
OR
<?php
if (!$include)
echo "<!--";
?>
<div id="slideshow"></div>
<?php
if (!$include)
echo "-->";
?>
EDIT: Obviously, there is no good reason to use the second method. The second method will only comment out the HTML so it will still show up in the source.
I'm not sure if this is what you are looking for, but seems simple
<?
$template = true;
if($template) {
?>
<div id="slideshow"></div>
<?
}
?>
On the template, you could have some code that reads:
if($_SERVER['PHP_SELF'] == /*control panel file*/) {
//exclude
}else{
//include
}
I've always thought about this and I can't decide what to do. Lets say I have a <div> and I want to echo specific links in it if the user is an admin or a just a member.
Would it be best to have the <div> in the HTML, and just do some if echos within it?
Or should you echo the whole entire div and everything inside it?
There is no possibilty of the div being empty.
You probably should go with code like (for simple application):
<div class="nav">
Index
Profile
<?php
if( $this->isAdmin()){
echo $this->getAdminLinks();
}
?>
</div>
Or rather something like this:
<?php
echo $this->getAdminLinks();
?>
and let that function care about whether to display or not function. For example Zend uses inline php in phtml templates (and I consider Zend great place where to learn).
If you're building complex application (multiple styles, complex navigation, many different types of pages and many conditions how to generate menu) or you use MVC framework, you should go with code:
<?php
echo $this->getNavigation();
?>
Code like this may get really hard to read (imagine 70 lines like this):
One
<?php echo $this->getSecond(); ?>
Third
<?php // some crazy stuff with 2 submenu levels
?>
Anyway, there's no one "good way" to do this, it's all based on what you need and how do you like your codes written.