Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Having weird problem with HTML links inside PHP files.

Posted on 2014-01-21
13
380 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
ID: 39797535
Try using absolute links rather than the relative ones.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39797545
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
ID: 39797630
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 31

Accepted Solution

by:
Marco Gasi earned 350 total points
ID: 39797670
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
ID: 39797672
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 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 150 total points
ID: 39797694
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
 
LVL 1

Author Comment

by:LB1234
ID: 39797707
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 31

Expert Comment

by:Marco Gasi
ID: 39797709
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 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 350 total points
ID: 39797724
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
ID: 39797751
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 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 350 total points
ID: 39797775
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 31

Expert Comment

by:Marco Gasi
ID: 39800936
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 31

Expert Comment

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn how to count occurrences of each item in an array.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

840 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