Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Having weird problem with HTML links inside PHP files.

Posted on 2014-01-21
13
Medium Priority
?
391 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 30

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 31

Accepted Solution

by:
Marco Gasi earned 1050 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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 450 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 1050 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 1050 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Find out what you should include to make the best professional email signature for your organization.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

916 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