Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 395
  • Last Modified:

Having weird problem with HTML links inside PHP files.

I have a simple navigation menu that's in a PHP file (nav.php) included for pages that need it.  When I launch nav.php in a browser, itlinks to the pages its supposed to.  So the first link, for example, I'm taken to:  http://localhost/expenses/Pages/reports.php as expected.  All is well so far.

But when I include actually include nav.php in another file and click the first link, it takes me to: http://localhost/Pages/reports.php.  This makes NO sense and I can't figure out why it's doing this.  The second file (dashboard.php - second block of code) just points to nav.php, but for some reason, it doesn't point to localhost\expenses\pages, but just localhost\pages.

<div id="nav">
    <ul>
        <li><a href="../../Pages/reports.php">Reports</a></li>
        <li><a href="../../Pages/user_management.php">User Management</a></li>
        <li><a href="../../Pages/credit_card_management.php">Credit Card Management</a></li>
    </ul>
</div>

Open in new window




<?php session_start();?>
<?php include ("../includes/db_connection.php");?>
<?php include ("../includes/layouts/header.php");?>
<?php include ("../includes/layouts/nav.php");?>
<?php include ("../includes/layouts/menu.php");?>


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

Open in new window

0
LB1234
Asked:
LB1234
4 Solutions
 
Randy DownsOWNERCommented:
Try using absolute links rather than the relative ones.
0
 
Marco GasiFreelancerCommented:
Can I guess that the second file where you include nav.php is not in the same directory as the first one? If so, using absolute links is the better thing, otherwise you should redesign the directory tree of your site...
0
 
LB1234Author Commented:
Marco, that is correct, they are in different directories.  Would it make my life easier by including them in the same directory?  It's puzzling to me that dreamweaver isn't smart enough to allow me to drag and drop this and keep it all straight.  The detective in me wants to know why this is failing.
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
Marco GasiFreelancerCommented:
Dreamweaver simply shouldn't be used at all: it produces a very messy code: as all tools which promise the paradise with a click it brings you directly to hell.

That said (maybe I have been exaggerated?), what is the best thing depends on how your site is and how many advanced is your work on it.
Personally, I use a site structure making the htacces redirect all request to index.php: here I process the request and serve the right page. This resides in the same directory of the index page, whereas php scripts reside in a dedicated directory and the same happen for javascript scripts, images and the rest of the files. This allows you to eventually move the whole site from one server to another without breaking links.

But you can decide to use absolute links and this allows you to distribute all files in several directories without regarding their relationship but if you move the site to another server you have to edit all links.

AFAIK, the best practice is to use relatives paths: easy to mantain and it helps you to organize the site in a functional way.
0
 
LB1234Author Commented:
Ok I figured out the issue without using absolute links, but still something is off.  All the references links were in the same directory so I made the changes below, and it worked, but curious as to why Dreamweaver did it the way it did, since clearly all files were in the same directory.  Why add ../../pages ?

I fixed the problem by changing:

<div id="nav">
    <ul>
        <li><a href="../../Pages/reports.php">Reports</a></li>
        <li><a href="../../Pages/user_management.php">User Management</a></li>
        <li><a href="../../Pages/credit_card_management.php">Credit Card Management</a></li>
    </ul>
</div>

Open in new window


<div id="nav">
    <ul>
        <li><a href="../../Pages/reports.php">Reports</a></li>
        <li><a href="../../Pages/user_management.php">User Management</a></li>
        <li><a href="../../Pages/credit_card_management.php">Credit Card Management</a></li>
    </ul>
</div>

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
Just a note.  Putting separate PHP start/stop tags like above puts the white space and line-endings that come between the tags into the rendered HTML page which generally is not wanted and can cause some problems like 'header' statements not working.  This is what I would do.
<?php
session_start();
include ("../includes/db_connection.php");
include ("../includes/layouts/header.php");
include ("../includes/layouts/nav.php");
include ("../includes/layouts/menu.php");

include ("../includes/layouts/footer.php");
?>

Open in new window

0
 
LB1234Author Commented:
Darn it, i Meant to paste the following FIX:

<div id="nav">
    <ul>
        <li><a href="reports.php">Reports</a></li>
        <li><a href="user_management.php">User Management</a></li>
        <li><a href="credit_card_management.php">Credit Card Management</a></li>
    </ul>
</div>

Open in new window

0
 
Marco GasiFreelancerCommented:
Sorry, but I don't see any difference between the two snippets above...

Are you saying that DW are adding ../../Pages to your links?! Don't ask me why...
Maybe you have set that value in some DW panel?
0
 
Marco GasiFreelancerCommented:
Oh, okay. So DW does things by its own choice, uh? You should terminate it and pass to Netbeans IDE: it's free and doesn't give issues, you can edit files directly from your web server and mutch more. The only thing it doesn't give you is the visual designer.

Anyway, I think you should look at some configuration of your project inside Dreamweaver...
0
 
LB1234Author Commented:
Ok I think i got it figured out.  When i initially drag and drop the file into the included file, the path is document relative and correct.  But now when i link that particular file into another as an include, the path is incorrect since it's relativity to the file is different.  After all, an include is just dropping the verbiage from the original file into itself.  So the paths would be all wrong.

Doesn't seem like a way to use an include without using absolute links since each file linking via include will have a different relative path to the original links.
0
 
Marco GasiFreelancerCommented:
Doesn't seem like a way to use an include without using absolute links since each file linking via include will have a different relative path to the original links

I don't understand what you mean: can you be more clear? English is not my language so maybe I'm too ignorant about some phrase structure...
0
 
Marco GasiFreelancerCommented:
Please, can you explain the B grading? How the problem has been fixed? What is the soution you found? Why you have no more replied to my comment?

The default grade is A, unless the answer provided is deficient (http://support.experts-exchange.com/customer/portal/articles/481419): perhaps my answer it is deficient, but, please, tell me why. I think the problem is the relative path and the wrong behavior of DW: if this is right, I think my answer is correct enough even if I didn't say what exactly DW did wrong.
0
 
Marco GasiFreelancerCommented:
Since you ignore my last comment, I requested attention of a moderator.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now