简体   繁体   中英

dynamically change href value using php

i have this problem that i need to change the directory address of href in anchor, i'm hoping to use php to make it work.. for example if i'm inside the home link my href value would be "index.php" but if im inside the folder of products link home href value will be change to "../index.php" because the header is just included to every page.. and also if you could suggest any alternatives for it, i would be happy to hear it.. thanks!

<div class="header_link">
     <a href="../"><div class="links"><p>Home</p></div></a>
     <a href="products/"><div class="links"><p>Products</p></div></a>
     <div class="links"><p>Packages</p></div>
     <div class="links"><p>Price List</p></div>
</div>

You should use absolute paths instead of relative ones. Relative paths will become difficult to manage down the road, and as you've discovered they have to change depending on which page you're viewing.

Instead of linking to ../index.php , just link to /index.php . This will work regardless of which page you're on.

Try using the base tag within the <head> of your html.

<base href="http://www.mysite.com">

or

Make all href values absolute from the domain.

<a href="/index.php">Home</a>

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