Jump to content

PHP Nav function with subpage


alphamoment

Recommended Posts

I'm using this current PHP Code in my Index.php;

 

<?php $page = $_GET['page']; 

    if (!isset($page)) {
    include('home.php');
    }
    if ($page == "Home") { 
    include('home.php');
    }
    if ($page == "0101") { 
    include('0101.php');
    }
    if ($page == "0202") { 
    include('0202.php');
    } 
?>

Then I'm using this for my Navigation link;

 

<li><a href="?page=0101">Members</a></li>

Which displays as 

index.php?page=0101

 But on those pages I want to go into another page off of them, for example;
 

index.php?page=0101&subpage=0102

index.php?page=0202&subpage=0201

How can I achieve this?

Link to comment
Share on other sites

Why do you even need to mention the parent page in the URL? In other words, why can't you just say index.php?page=0102? Isn't the ID of the subpage already unique?

 

By the way, you really need to choose better identifiers for your pages. It might also be a good idea to review the overall architecture of your site. When people start to use weird numbering schemes to organize their pages, that's usually a bad sign (unless this was just an example).

  • Like 1
Link to comment
Share on other sites

you would make use of the same type of page controller logic (the conditional tests) that you have in your main file, within each of the pages that have subpages, to control what they do for any subpage they contribute to the navigation and are responsible for processing.

 

for your example of index.php?page=0101&subpage=0102, the main page controller in index.php will include/require 0101.php. the page controller within 0101.php would test for the existence of $_GET['subpage'] and run/include/require the appropriate code for the 0102 subpage action.

  • Like 1
Link to comment
Share on other sites

I agree with Jacques.

A good way is to save items in a database such as a section or category, article id's or as pretty/safe slugs

Using a properly set up relational database to handle what gets shown and when.

For instance when selecting a certain "page" you do a query and get all it's related "subpages"

What you should have is an index page with a loop of possible "subpages", if and when the GET value is set for "subpage" it should include a single view code or script of just that "subpages" content.

 

Sometimes I create a whitelist array of the pages allowed, other times I'll fetch the required page name from a database because would exist as a dynamic value.

 

You could try doing something like this to simplify your if's

if (isset($_GET['page']) && trim($_GET['page']) !='') {

$page = $_GET['page'];

} else {

$page = "home";

}

$script = dirname(__FILE__) . DIRECTORY_SEPARATOR . $page . ".php";
if (file_exists($script)) {
    require_once($script);
} else {
    echo "Page does not exist";
}

Then you can check for $_GET['subquery'] within each script

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.