Jump to content

[SOLVED] $_SERVER question for navigation


samoht

Recommended Posts

Hello all,

 

I have a remodeling site that has multiple services. Each service is in its own subfolder with an index.php. I have a leftnav bar that allows the user to navigate to each service - and I want to highlight the current service nav button.

 

My problem is with the url. since the basename will always be index.php - how should I set the condition for the test of the url?

Connected to this problem is - how do I code the path to the service so that it works on my local machine as well as when I upload the page (without having to manually change the path)

 

Currently I have

<?php  
$rpage = $_SERVER['SCRIPT_NAME'];
$page = substr(($rpage),0,5);
?>
<div id="ltnav">
<ul id="navleft">
        <li<?php if($rpage== "/homestead/remodel/index.php"){echo " class=\"current\"";}?>>
          <a href="http://www.nextgenwt.com/homestead/remodel/">Remodel</a>
        </li>

I don't want to put the full path in (at least not hard coded)

 

Thanks for any help

Link to comment
Share on other sites

<?php
$rpage = $_SERVER['SCRIPT_NAME'];
$page = substr(($rpage),0,5);
?>
<div id="ltnav">
<ul id="navleft">
        <li<?php if(stristr($rpage, 'remodel')){echo " class=\"current\"";}?>>
          <a href="http://www.nextgenwt.com/homestead/remodel/">Remodel</a>
        </li>

 

Great way to merge PHP with CSS!

 

BTW, you may not want to use dashed lines separating your columns. It gets muddy with IE6 when scrolling.

Link to comment
Share on other sites

Thanks!

 

I'll consider different column seperation, maybe an img?

 

Anyway, any ideas on how to get my <a href=""> to a relative path?

Can I

http://"<?php echo $_SERVER['ROOT'].$_SERVER['SCRIPT_NAME']."/realtivepath/";?>

 

or something like this - but script_name will give me to much. Can I go back 1 file?

??

Link to comment
Share on other sites

If each one is in a folder, you don't need the index.php stuff, and it will look nicer.

site.com/kitchens/ SHOULD work the same as site.com/kitchens/index.php

 

Why don't you define your Site's BASE url as a constant:

define('SITE_URL', 'http://site.com/');

and then whenever you have a link do:

 

print '<a href="'.SITE_URL.'kitchens/">';

Link to comment
Share on other sites

The problem is that the main page (something.com)'s index.php will also have the leftnav include so if I use a relative path of ../kitchens/ in leftnav.php - that wont work on the main page.

 

I like the idea of defining a base url  - but will that mean I hae to manually change that definition when I move the site from the local server to the live?

 

Is there a function that can find my base url for me?

Link to comment
Share on other sites

The problem is that the main page (something.com)'s index.php will also have the leftnav include so if I use a relative path of ../kitchens/ in leftnav.php - that wont work on the main page.

 

I like the idea of defining a base url  - but will that mean I hae to manually change that definition when I move the site from the local server to the live?

 

Is there a function that can find my base url for me?

 

The answer is almost too easy...  ;D That is, if you have access to the $_SERVER['SERVER_NAME'] variable.

At the beginning of your script, have this if statement.

<?php
if (stristr($_SERVER['SERVER_NAME'], "nextgenwt"))
{ // If we're on production server
$path = "/homestead/";
}
else { // If we're on localhost
$path = "/"; // I don't know your localhost path, so you'll have to fill in this one
}
?>

 

Your links will look like this:

<a href="<?php echo $path ?>remodel/">Remodel</a>

 

This solution will work on both servers, and work on all the pages.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.