Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

include file in another directory alters links

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

include file in another directory alters links

Postby Linnie » Mon Apr 22, 2013 1:57 am

Hi. I created an include file for my navigation bar called nav.php
I put it into an includes folder in my root directory.
I created a new directory called 'articles', because my root directory was getting crowded.
When I uploaded a page in the articles directory, the nav bar links have been extended to include the 'articles' directory and are now incorrect. For example if I click on the home link...it says articles/index.php which is crazy, it should say just index.php
The other pages on the site are just fine, it is the new directory.
The page in question is http://askingangels.com/articles/affirmations.php
If you see that page, all the links are now not working because links at bottom and top of page are in separate includes files. How do I fix these links, do I need to do something called absolute path or something? :( thanks for your help
Linnie
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Apr 22, 2013 1:45 am

Re: include file in another directory alters links

Postby seandisanti » Mon Apr 22, 2013 10:16 am

Please show your code for the nav.php file
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: include file in another directory alters links

Postby Linnie » Mon Apr 22, 2013 11:36 pm

There is the code:

<table style="background-color: rgb(204, 255, 255); width: 971px; height: 32px; text-align: left; margin-left: auto; margin-right: auto;"



border="0" cellpadding="2" cellspacing="2">

<tbody>

<tr>

<td style="font-family: Helvetica,Arial,sans-serif; text-align: center; color: black; background-color: rgb(0, 0, 153);">

<a href="index.php"><small><font color="#ffffff">Home</font></small></a></td>

<td style="font-family: Helvetica,Arial,sans-serif; text-align: center; background-color: rgb(0, 0, 153);"><a



href="contactyourangels.php"><small><font color="#ffffff">Contact

your Angels</font></small></a></td>

<td style="font-family: Helvetica,Arial,sans-serif; text-align: center; color: white; background-color: rgb(0, 0, 153);"><a



href="howdoiask.php"><small><font color="#ffffff">How Do I Ask?</font></small></a></td>

<td style="font-family: Helvetica,Arial,sans-serif; text-align: center; background-color: rgb(0, 0, 153);"><a



href="prayerpage.php"><small><font color="#ffffff">Prayer Page</font></small></a></td>

<td style="font-family: Helvetica,Arial,sans-serif; text-align: center; background-color: rgb(0, 0, 153);"><a



href="articles.php"><small><font color="#ffffff">Articles</font></small></a></td>

<td style="text-align: center; font-family: Helvetica,Arial,sans-serif; background-color: rgb(0, 0, 153);"><a



target="_blank" href="jewellery"><small><font color="#ffffff">Jewellery</font></small></a></td>

<td style="text-align: center; background-color: rgb(0, 0, 153);"><a href="psychicreadings.php"><small><span



style="font-family: Helvetica,Arial,sans-serif;"><font color="#ffffff">Psychic

Readings</font></span></small></a></td>

<td style="text-align: center; background-color: rgb(0, 0, 153);"><a href="contact.php"><small><span



style="font-family: Helvetica,Arial,sans-serif;"><font color="#ffffff">About/Contact</font></span></small></a></td>

</tr>

</tbody>

</table>


Again thanks :)
Linnie
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Apr 22, 2013 1:45 am

Re: include file in another directory alters links

Postby seandisanti » Tue Apr 23, 2013 9:37 am

is the file doing the including in your articles folder? References are based on the original script, not the included files. One way around it is to use a getRoot() function. like so:
Code: Select all


function getRoot($uri '') {
    if (
$uri == '') {
        
$uri $_SERVER['PHP_SELF'];
    }
    
$depth substr_count($uri'/') - 1;
    
$root '';
    for (
$x 1$x <= $depth$x++)
        
$root '../' $root;
    return 
$root;
}
 

and then just use that to make sure that your relative paths are relative to the root, rather than the script calling them. Here is an example of it in use for my autoload()
Code: Select all


function __autoload($name) {
    
$root getRoot();
    
$theClass $root '_includes/classes/' $name '.php';
    if (
file_exists($theClass)) {
        include(
$theClass);
    } else {
        die(
'Tried to load non-existent class: ' $theClass);
    }
}
 
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: include file in another directory alters links

Postby Linnie » Tue Apr 23, 2013 6:37 pm

Wait, I just fixed this!!!!
Did some research and then went and put an extra '/' in the includes file, so instead of the link being this:

<a href="index.php">

I changed it to this:

<a href="/index.php">

that's all I did and now the nav bar has the correct links in my articles directory.
Yay! Thanks for your help anyways :D
Last edited by Linnie on Tue Apr 23, 2013 7:30 pm, edited 1 time in total.
Linnie
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Apr 22, 2013 1:45 am

Re: include file in another directory alters links

Postby seandisanti » Thu Apr 25, 2013 7:15 am

the '/' before the file name does the same as getRoot() if you're only running one domain on the server.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 5 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.