Reputation: 209
I've used this method on a few website builds with no obvious problems. But I'm wondering if it's an okay/proper way to utilize PHP variables as website navigation/page indicators so that site-wide changes can be easily managed in 1 file (upper.php)? Are there any drawbacks? Maybe too many requests to the server, load times, improper coding or negative S.E.O. ramifications?
I've omitted surrounding code such as < body >, etc. for simplicity and indicated where more code would be with "..."
index.php
<?php $urhere="home"; include ("upper.php"); ?>
<div>This is the Home page content</div>
...
about.php
<?php $urhere="about"; include ("upper.php"); ?>
<div>This is the About page content</div>
...
contact.php
<?php $urhere="contact"; include ("upper.php"); ?>
<div>This is the Contact page content</div>
...
upper.php
...
<meta name="description" content="
<?php
if ($urhere=="home") echo "Home page description";
if ($urhere=="about") echo "About page description";
if ($urhere=="contact") echo "Contact page description";
?>
...
<title>
<?php
if ($urhere=="home") echo "Home page Title";
if ($urhere=="about") echo "About page Title";
if ($urhere=="contact") echo "Contact page Title";
?>
</title>
...
<div id="nav">
<ul>
<li><a href="/home"<?php if ($urhere=="home") echo " class=\"urhere\""; ?>>Home</a></li>
<li><a href="/about"<?php if ($urhere=="about") echo " class=\"urhere\""; ?>>About</a></li>
<li><a href="/contact"<?php if ($urhere=="contact") echo " class=\"urhere\""; ?>>Contact</a></li>
</ul>
</div>
Upvotes: 1
Views: 1559
Reputation: 1871
I see some serious maintenance problems with this approach.
Every time you add a new page, you have to add three (for now) if-statements.
upper.php knows too much about other pages -- their window titles and metadata.
I would suggest that instead of using the $urhere
variable to make all the decisions in upper.php, you set variables, like window title and metadata, which can be directly used by upper.php. Here's an example:
upper.php
<meta name="description" content="<?php echo $description ?>"/>
...
<title><?php echo $title ?></title>
...
<?php
$pageTypeToLinkMap = array(
'home' => array(
'url' => '/home',
'linkText' => 'Home'
),
'about' => array(
'url' => '/about',
'linkText' => 'About',
'sublinks' => array(
'who-we-are' => array(
'url' => '/who-we-are',
'linkText' => 'Who We Are'
),
'what-we-do' => array(
'url' => '/what-we-do',
'linkText' => 'What We Do',
'sublinks' => array(
'business' => array(
'url' => '/business',
'linkText' => 'Business'
),
'technology' => array(
'url' => '/technology',
'linkText' => 'Technology'
)
)
)
)
),
'contact' => array(
'url' => '/contact',
'linkText' => 'Contact'
)
);
function getLinkElement($pageType, array $linkData)
{
$class = $urhere == $pageType ? 'urhere' : '';
$anchorElement = "<a href=\"$linkData[url]\" class=\"$class\">$linkData[linkText]</a>";
if (isset($linkData['sublinks']))
{
$sublinkElements = array();
foreach ($linkData['sublinks'] as $sublinkPageType => $sublinkData)
$sublinkElements[] = getLinkElement($sublinkPageType, $sublinkData);
$sublinksListElement = '<ul>' . implode($sublinkElements) . '</ul>';
}
else
$sublinksListElement = '';
return "<li>$anchorElement$sublinksListElement</li>";
}
?>
<div id="nav">
<ul>
<?php foreach ($pageTypeToLinkMap as $pageType => $link) echo getLinkElement($pageType, array $link); ?>
</ul>
</div>
index.php
<?php
$pageTitle = "Home";
$description = "This is home.";
$urhere = "home"; // I guess you still need this variable to make decisions in the "nav" div.
?>
<div>This is the Home page content</div>
about.php
<?php
$pageTitle = "About";
$description = "About us";
$urhere = "about";
?>
<div>This is the About page content</div>
EDIT
In response to your comment, here's how you can improve your upper.php while keeping the same original structure:
upper.php
<?php
$titles = array(
'home' => 'Home page Title',
'about' => 'About page Title',
'contact' => 'Contact page Title'
);
$descriptions = array(
'home' => 'Home page description',
'about' => 'About page description',
'contact' => 'Contact page description'
);
$linkClasses = array_fill_keys(array_keys($titles), '');
$linkClasses[$urhere] = 'urhere';
?>
...
<meta name="description" content="<?php echo $descriptions[$urhere] ?>"/>
...
<title><?php echo $titles[$urhere] ?></title>
...
<div id="nav">
<ul>
<li><a href="/home" class="<?php echo $linkClasses['home'] ?>">Home</a></li>
<li><a href="/about" class="<?php echo $linkClasses['about'] ?>">About</a></li>
<li><a href="/contact" class="<?php echo $linkClasses['contact'] ?>">Contact</a></li>
</ul>
</div>
There you go, if-less code.
Upvotes: 1
Reputation: 3156
There is no right or wrong way to build a webpage, and there are many options. In general, I like to use a templating system of some type, which makes the design of the page easy to seperate from the business logic behind it.
http://jonathanmh.com/best-php-mvc-frameworks-of-2013/
Upvotes: 0