I'm trying to set up my site with PHP but I'm a beginner.
I already know how to include the maincontents (just the content in a seperate .php file) by clicking on a navigation link.
But what if the included content has a subnavi and I also want to use this navi to include content into that one?
Does a include in the include work?
How do I tell the link in the included content, that he should first load index, then itself and then the content choosen from the subnavi?
Or is there a better way to structure my code?
Here's my code for the index.php:
<?php
if ($_GET['page'] == 'first' ){
include 'first.php';
}else if ($_GET['page'] == 'second' ){
include 'second.php';
}else if ($_GET['page'] == 'third' ){
include 'third.php';
}else if ($_GET['page'] == 'fourth' ){
include 'fourth.php';
}else include 'drawing.php';
?>
I call the links with:
href="index.php?page=first"
Can I use the same now for the subones? But what to add that it works?
EDIT: I precide my Questions once again cause after some answers now I got the feeling PHP is not able to do what I'm trying to achieve.
So let me explain my idea again:
My index.php is standalone coded (header & footer) and it has an include section where I want to load the contents. The link is already provided and works. The content I'm loading is not standalone coded. It only contains a div with all the stuff in it, nothing more. That's the reason for include, I think.
Ok, but now the loaded content in index.php has some more links to other includes. It's a subnavi. And therefore I also want to include content here into that just loaded content, which is also no standalone content (no header or footer).
The Problem I can image is that the content, that has been loaded into index.php of course does not know that it's a loaded content. So for that reason, when it should load another content in itself it can't rebuilt itself cause it doesn't now that his "creating pages" does exist. Right?
To precise my Question: Is there a kind of link for the loaded content's subnavi that says href="loadindexthenloadmeinindexthenloadtheincludedcontentIhavejustaskedfor"
If not, I'd love to learn how.
If this way is just simply unorthodox, then please tell me.
Thank you for your attention.
Yes, you can include within an include.
Think of an include as copying and pasting the contents of the included file into where the include "blah.php"
was.
A "better way" to structure this is to have a "master" view including other parts and pieces that should be independent from each other.
You should pay attention to what files you are including, since you might end up including some files more than once. This could be a problem if the include defines a function, which would cause your script to crash. In that case, you can use include_once
.
With that being said... Include within an include?
(sorry couldn't resist)
first of all :
$routes = array(
'first' => 'first.php',
'second' => 'second.php',
'third' => 'third.php',
'fourth' => 'fourth.php',
'fallback' => 'drawing.php'
);
$page = $_GET['page'];
if ( !array_key_exists( $page , $routes ))
{
$page = 'fallback';
}
include $routes[$page];
And yes , you can have includes inside includes.
You can do many other things with them too , like create a simple templating engine. Or pass data from include into a variable :
Lets say you have line:
$config = include 'conf.php';
and `conf.php' contains :
<?php
return array(
'foo' => 'lorem ipsum sit dolor amet',
'bar' => 'cogito ergo sum'
);
?>
Then it will pass this array inside $config
.. but this trick is rarely used. Not always the best way, apply reason.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With