Dynamic Inclusion subpages
I merged the dynamic inclusion tutorial and Codegrrl's ConvertToPHP script. You will need to understand dynamic inclusions first. See dynamic subpages in action.
If you're already using dynamic inclusion and want to use more, here is a solution. For example, you've already used the dynamic inclusion, so your url look like: index.php?y=page but you want to separate the page into subpages.
You won't need your index.php or defaut.php/main.php here, but the page you want to divide. Let's call it page.php, you won't need any:
<!DOCTYPE>
<html>
<head>
<title>your title</title>
</head>
<body>
// CONTENTS
</body>
</html>
Since it's already in your index.php... open page.php, the page you want to divide and let the fun begin, paste this in the very first lines, this is your menu to navigate between the subpages.
<h1>Page title</h1>
<p>Select:
<a href="index.php?y=page">back</a> |
<a href="index.php?y=page&x=1">link 1</a> |
<a href="index.php?y=page&x=2">link 2</a> |
<a href="index.php?y=page&x=3">link 3</a> |
</p>
A few explanations... If your document name is random.php, replace page by random, of course. The x means the link will look like: index.php?y=random&x=value.
Now paste this:
<?php if(!$x) { ?>
Of course if you changed x by something else, don't forget to change it. Everything under that line will show up automatically if you go to index.php?y=page. Add the contents you want and when you're done, we will make the contents of link 1. Paste this after your default contents:
<? } elseif ($x == "1") { ?>
Once again, if you replaced x by something else, don't forget to replace it here too. The 1 can be replaced by anything you want, just don't forget to change it in your menu too. Add your contents after that line of code. If you go to index.php?y=page, it won't show up but if you access index.php?y=page&x=1, you will see these contents. When you're done adding the contents of this page, add this line:
<? } elseif ($x == "2") { ?>
Same as previously, add your contents for the second page under that line of code. It will be accessible here: index.php?y=page&x=2. A bit lost? This is what you should have in your page.php
<h1>Page title</h1>
<p>Select:
<a href="index.php?y=page">back</a> |
<a href="index.php?y=page&x=1">link 1</a> |
<a href="index.php?y=page&x=2">link 2</a> |
<a href="index.php?y=page&x=3">link 3</a> |
</p>
<?php if(!$x) { ?>
<!-- introduction contents, shows up when accessing index.php?y=page -->
<? } elseif ($x == "1") { ?>
<!-- link1 contents, shows up when accessing index.php?y=page&x=1 -->
<? } elseif ($x == "2") { ?>
<!-- link2 contents, shows up when accessing index.php?y=page&x=2 -->
<? } elseif ($x == "3") { ?>
<!-- link3 contents, shows up when accessing index.php?y=page&x=3 -->
<? } ?>
Did you notice the <? } ?> ? You will have to add this little code at the end of your page. You can have more than 3 subpages of course, each time your just have to add:
<? } elseif ($x == "SUBPAGE") { ?>
<!-- PAGE contents, show up when accessing index.php?y=page&x=SUBPAGE -->
Hope it's understandable! Don't forget you have to use dynamic inclusions first!
Dynamic Inclusion subpages sample files (1.5 KB, 251 hits)
7 Responses to “Dynamic Inclusion subpages”
Leave a Comment
Works? Doesn't work? Other tips? Please leave a comment! :)
This just isn't working for me for some reason. :\ I copied it word for word to make sure I wasn't making some dumb typo...but it still doesn't work. I can't seem to find any other tutorials for it online either--all I need is to make the content of several pages in one simple file, but no matter what x value I enter, it just shows the default content.
Hi Roxie, I've uploaded Dynamic Inclusion subpages sample files, you can download the zip and see the codes :) If it still doesn't work, please mail me with your index.php codes and subpages.php codes ^^
Thank you so much! When I uploaded the sample pages, it STILL didn't work, so I figured it must have been something about my version of PHP...so I switched it from version 5.2.2 to 4.4.7. And it worked. :) I didn't see any note on what version of PHP this was supposed to be for, so I was confused.
Hi Clara,
Firstly thank you for this invaluable tutorial it has helped me alot.
The subpages work fine, but I would like to integrate this into an existing page I have called News.php which by using the dynamic inclusion, the contents are placed in index.php as follows: index.php?action=news
From there one has the option to view comments from the entries listed. Currently, the link is
"<a href=\"individual_entry.php?id=".$id. "\">Leave a comment</a>";
which means that when the link is clicked it takes the user to a new page. What I’d like to understand, is , if it is possible, to use the sub pages to load the individual entry in the same content area.
So far I have tried with no success:
which loads the index page but no contenthref=\"index.php?action=individual_entry.php?id=".$id. "\">Leave a comment</a>";
"<a href=\"index.php?action=individual_entry.php&x=id=".$id. "\">Leave a comment</a>";
same as latter
"<a href=\"index.php?action=individual_entry&x=id=".$id. "\">Leave a comment</a>";
which gives an “Invalid ID specified ”
"<a href=\"index.php?action=individual_entry&x=individual_entry?id=".$id. "\">Leave a comment</a>";
same as latter
I’d be very obliged if you can advise me on this.
Thanks again
Hey Clara I'm having the same problem. The subpages dont seem to be loading. And I'm using PHP 5.2.4. I will I upload to see if you can figure out what the problem is. I've tried your system and this:
?php
if (isset($_GET['id'])) $PAGE = $_GET['id'];
else $PAGE = 'home';
switch ($PAGE) {
//--Index
case 'home':include ('home.php');break;
echo "Error 404!The page you request doesn't exist!";
break;
}
?>
Although your inclusion system is more affective, neither one of them seems to be working. I a nooby to php so I'm not really sure about all the keywords.
I'm confused. Where does the content to our page go in the code?
WAIT! I get it now. Thank you so much for this tutorial!