wuno
wuno

Reputation: 9885

php navigation using get or include

index.php is controlling the site. I have elseif include based on variable $p

right now $p does not have anything in it and i'm not understanding why.

Also is using include the industry standard or would GET be a better choice?

In the header file which is included on index.php but it just takes me to the page link instead of storing the page in $p

<?php
$pages = array(
    "home" => "HOME", 
    "services" => "SERVICES", 
    "employees" => "EMPLOYEES", 
    "contact" => "CONTACT"); 
$p = (isset($_GET['p'])) ? $_GET['p'] : "";
foreach ($pages as $url => $label) {
    echo '<li ';
if ($p == $url) { echo '<li><a class="active" href="' . htmlspecialchars(urlencode($url)). '.php">' 
. htmlspecialchars($label) . '</a></li>'; } else { echo '<li><a href="' . $url . '.php">' . $label . '</a></li>'; } 
}
?>

This is the index.php file:

<?php include('includes/header.php'); ?>

<?php 
    if ($p == "services") {
        include("services.php");
    } elseif($p == "employees") {
        include("employees.php");
    } elseif($p == "contact") {
        include("contact.php");
    } else {
        include("home.php");
    };
?>

<?php include('includes/footer.php'); ?>

Upvotes: 2

Views: 1708

Answers (1)

Michael Lea Crawford
Michael Lea Crawford

Reputation: 490

It really depends on what you are trying to do. Maybe I am misunderstanding your question, however include and $_GET are two completely separate things.

$_GET is an array of the query string out of your URL. So, for instance, if your URL was mysite.com/index.php?a=rawr&p=services then $_GET would be array("a" => "rawr", "p" => "services"); If you don't have any parameters in your URL assigned to p, then $_GET["p"] would be empty

Source: http://www.php.net/manual/en/reserved.variables.get.php

EDIT:

I think I'm starting to understand your question now. Using your current method would be fine, I would just change up the code slightly:

<?php
$pages = array(
    "home" => "HOME", 
    "services" => "SERVICES", 
    "employees" => "EMPLOYEES", 
    "contact" => "CONTACT"
); 
$p = (isset($_GET['p'])) ? $_GET['p'] : "";
foreach ($pages as $url => $label) {
?>
<li><a <?= $p == $url ? 'class="active"' : ""?> href="index.php?p=<?=$url?>" > <?=$label?> </a></li>
<?php
}
?>

Upvotes: 2

Related Questions