Solved

Having weird problem with HTML links inside PHP files.

Posted on 2014-01-21
13
368 Views
Last Modified: 2014-01-23
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
Comment
Question by:LB1234
13 Comments
 
LVL 29

Expert Comment

by:Randy Downs
Comment Utility
Try using absolute links rather than the relative ones.
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
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
 
LVL 1

Author Comment

by:LB1234
Comment Utility
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
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 350 total points
Comment Utility
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
 
LVL 1

Author Comment

by:LB1234
Comment Utility
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
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 150 total points
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:LB1234
Comment Utility
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
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
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
 
LVL 30

Assisted Solution

by:Marco Gasi
Marco Gasi earned 350 total points
Comment Utility
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
 
LVL 1

Author Comment

by:LB1234
Comment Utility
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
 
LVL 30

Assisted Solution

by:Marco Gasi
Marco Gasi earned 350 total points
Comment Utility
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
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
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
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
Since you ignore my last comment, I requested attention of a moderator.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Showing your events from Google Calendar in Google Maps Why? I travel all week and I thought it would be ideal if staff in office knew where I was based on my calendar. (OK real reason: my son wanted to see where I would be working, and I thoug…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now