4

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.

3 Answers 3

10

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?

enter image description here (sorry couldn't resist)

Sign up to request clarification or add additional context in comments.

2 Comments

It's just straith simple. Every Page has 3 subcontents. The index should be able to include the maincontents. And the maincontents just the subcontents. So index > maincontent > SubA, SubB or SubC.
@Melros Yeah, that's fine. It's actually a very good thing to separate presentation from your business logic (by using maincontent instead of doing outputting stuff in index.php)
2

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.

11 Comments

+1 For suggesting a templating engine that is simple AND uses PHP directly.
Ok, as a PHP beginner I really don't understand a thing now. I tried to put my code in the included content and try to open the subcontent in the included content but now I got an "endless" inclusion.
ok, here's the though I'm having: the link in the loaded content: href="index.php?page=first?sub=subfirst" I know it doesn't work. But do you know what I mean? Is this the right way somehow?
@Melros sounds like you are including file in itself, creating endless .. thing. As for that URL. the index.php decides what to do with $_GET['page'] , and includes first.php then first.php file does something with $_GET['sub']. Etc.
Thats exactly what happend and what I'm trying to fix. That's my Question.
|
0

Sure you can. It's called recursive including

6 Comments

I googled it but can't find a simple example. Do you know a good place?
How did you come up with this term? You can't even call it recursive since it doesn't include the previous file.
I just updated my question with code. Is this a good start to begin with?
yes, it works for the first page. So far I got. But my question was how I now can load another content in the loaded one. F.e. I loaded second.php into index.php. And second.php has a nav to three more contents which I want to include in second.php. Remember: second.php only contains content with no header or footer.
@Melros: just add another include in file you want to
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.