سؤال

I have a index file in my root directory.

I have a directory called pages.

Nav bar is in the includes directory.

If I want to include the navbar in index.php and also in each one of my pages.php which are located in the pages directory. What is the proper way to structure this nav bar so the php can move up and down directories by what page it is on.

each page is identified as

$page = currentPage

And here is my current navbar which is in the includes dir

<ul>
<?php if ($page == 'home') { ?><li class="active"><a href="index.php" class="active">Home</a></li>
<?php } else { ?><li><a href="../index.php">Home</a><?php } ?>

<?php if ($page == 'contact') { ?><li class="active"><a href="contact.php" class="active">Contact Us</a></li>
<?php } else { ?><li><a href="pages/contact.php">Contact Us</a><?php } ?>

<?php if ($page == 'services') { ?><li class="active"><a href="services.php" class="active">Services</a></li>
<?php } else { ?><li><a href="pages/services.php">Services</a><?php } ?>

<?php if ($page == 'employees') { ?><li class="active"><a href="employees.php" class="active">Employees</a></li>
<?php } else { ?><li><a href="pages/employees.php">Employees</a><?php } ?>

<?php if ($page == 'dashboard') { ?><li class="active"><a href="dashboard.php" class="active">Dashboard</a></li>
<?php } else { ?><li><a href="pages/dashboard.php">Dashboard</a><?php } ?>
</ul>

Directory looks like this.

root

root/includes

root/pages

Thanks for your help in advance. I realize I could just do some simple things to get around this but I really want to understand this for the future.

This is how the page is currently laid out:

<?php $page = 'contact'; ?>

<html>
<meta charset="utf-8"> 
<title>Contact Us</title>
<head>
<base href="http://www.mysite.com/Dir_IM_Working_on_root/pages/" />
<link rel="stylesheet" type="text/css" href="../css/styles.css" />
</head>

<body>
<div id="wrapper">

<div id="nav">
<?php include '../includes/nav.php'; ?>
</div><!-- CLOSING NAV DIV -->

<div id="main">

</div><!-- CLOSING MAIN DIV -->

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

</div><!-- CLOSING WRAPPER DIV -->
</body>
</html>
هل كانت مفيدة؟

المحلول

Easiest would be to output absolute URLs in all cases. (/test.php)

If you want to leave the possibility to run your site in a subfolder, you can prepend all paths with a variable. This can be changed to the current position of your site.

Another way is to add a

<base href="http://www.example.org/subfolder/" />

to your html code and change that accordingly in combination with absolute URLs. All URLs are based on what you put into the attribute. This is a very elegant solution, but it affects a lot, which might be surprising if one is not that trained in web linkage.

I think there is no "right" way. Typo3 uses the base tag variant and it's most likely the way I would use. Lots of other software use the variable variant (or wrap the path in a function -- which is just another way of achieving the same). Just try some ways and decide for the one which seems most appealing to you I'd say.

In my experience the only thing nearly nobody uses are relative URLs.

Update

Use this html in combination with base html tag.

<ul>
<li<?php if ($page == 'home') echo ' class="active' ?>><a href="index.php">Home</a></li>
<li<?php if ($page == 'contact') echo ' class="active' ?>><a href="pages/contact.php">Contact Us</a></li>
<li<?php if ($page == 'services') echo ' class="active' ?>><a href="pages/services.php">Services</a></li>
<li<?php if ($page == 'employees') echo ' class="active' ?>><a href="pages/employees.php">Employees</a></li>
<li<?php if ($page == 'dashboard') echo ' class="active' ?>><a href="pages/dashboard.php">Dashboard</a></li>
</ul>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top