简体   繁体   中英

Href link and linking

Ok so i have a little problem now, that i hope someone would be able to help me resolve.

I've made a website in html, and after finishing the design i decided to break it up in php include lines where it would be easier to navigate and continue to code.

For example:

HTML CODE

<html>
<head></head>
<body></body>
</html>

And after breaking it up:

PHP code

include 'php/head.php';
include 'php/body.php';

It would all be OK if i didn't had to make a sub menu's and categories in another folder. The main problem is that i have folder tree like this:

nslife(root)
   images
   css
   js
   php
   novisad(folder that contains the sub categories)
   index.php

Ok so building the page as i did by breaking the code up works excellent on the main index page but after copy/pasting the same code on to another new page which is a sub category all of the href links didn't work.

For example: Primary index page:

<img id="right-img" src="images/baners/baner500x60.jpg">

But after using it in my Secondary page from the sub folder "novisad" the image doesn't show.

I know it has to do something with ../ or // or anything else but tried every possibility and nothing works, and if it works for one page i doesn't work on another.

Potential solutions:

  1. Update all links to use an absolute path, or use ../ (which traverses up 1 level) to get to the right directory. Or use a single / to indicate the root directory at the start of every path.

  2. Define some constant like ROOT_URL , setting it equal to the root address of your site, and prepend this to every path in your code

  3. Use: $_SERVER['DOCUMENT_ROOT'] and prepend this to your paths

Here's a good article on the subject which you might like to read.

You are currently using relative urls, so when you reference images/baners/baner500x60.jpg , you need to have a folder called images in the same directory as your file that is referencing that location.

For your purposes, use absolute urls instead. Something like http://www.yourdomain.com/images/baners/baner500x60.jpg . These will always reference the same location regardless of the location of the file that's referencing it.

You definitely need the absolute path. Your code would be referring to a file like images/image.jpg but from the subfolder the actual path would be ../images/image.jpg . To fix this, use http://yoursite.com/images/image.jpg . You may run into issues if your site sees these as external links and won't run scripts from them as a result, in which case you need to find an alternate solution (different includes for that folder, or modify the includes so that you rewrite the links depending on depth of the subfolder).

对于img src属性,请使用绝对URL而不是相对URL,如下所示:

<img id='right-img' src='http://www.domain.com/nslife/images/baners/baner500x60.jpg'>

您可以使用完整地址http://url.com/images/images.png或将../images/image.png放在URL的上一级。

In my programming I set a variable called: $level = ''; which is set at the top of the page.

At the top level it is: $level = '';

Sub Folder it is set as: $level = '../';

Sub Sub Folder it is set as: $level = '../../';

and so forth. Then you simple prepend the all links with the variable.

$level.'css/style';

all of your links will respond correctly as a result.

For larger projects I do this on my conn page:

$db_host = "localhost"; 
    $db_username = "????"; 
    $db_pass = "????"; 
    $db_name = "????";

    $db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass, array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ));

    //$site_url = 'http://'.$_SERVER['SERVER_NAME'];    
    $site_url = 'http://localhost/demo';

    # this page stuff   
    $pg_uri = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    $pg_url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];
    $this_file = basename($_SERVER['SCRIPT_NAME'], ".php");
    $file_name = basename($_SERVER['SCRIPT_NAME']);

    # urls      
    $u_inc = $site_url.'/inc';  
    $u_panels = $site_url.'/panels';            
    $u_legal = $u_inc.'/legal';
    $u_forms = $u_inc.'/forms';              
    $u_admin = $site_url.'/admin';
    $u_plugin = $site_url.'/plugins';        
    $u_js = $site_url.'/js';    
    $u_regions = $site_url.'/regions';
    $u_menus = $site_url.'/menus';

    # paths
    $p_root = $_SERVER['DOCUMENT_ROOT'];     
    $p_inc = $level.'inc';
    $p_panels = $level.'panels';
    $p_legal = $p_inc.'/legal';              
    $p_forms = $p_inc.'/forms';
    $p_admin = $level.'admin';              
    $p_plugin = $level.'plugins';
    $p_js = $level.'js';                    
    $p_regions = $level.'regions';  
    $p_menus = $level.'menus';

Once the vars are committed to memory it flows with the rest of your programming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM