Page 1 of 1

[solved] Coloured button stays 'coloured'

Posted: Mon Nov 01, 2004 5:08 am
by hairyjim
Hi all,

In my head this was supposed to be straight forward and easy :? but it now seems I have started to make this overly complicated.

Ok, I have a product that has several modules available to be bought. The main product page for this item I wanted to have a navigation bar across the top that listed each available module.

When a user clicked on one of these module links I wanted the new page to load but have the link/button coloured differently to the other module buttons to help visually show the module they are viewing.

I started solving this by setting the bg to the links cell using CSS and switching the bg colour depending on the directory name they were viewing.

The below code outlines what I am trying to achieve. Now the problem I have just hit is that I check the directory name they are in for instance /volocity/classification/ would give me classification and based on this I switch the bg colour.

But this is not scalable, for if in the future I add a new dir to the classfication dir such as register then the below code evaluates to the 'default' outcome because it brings back 'register' as the dir. Now I could then start adding more switch evaluations but this seems cumbersome. I then thought well why not use reg ex. and find the top tier DIR in the URL, which would work and be scalable, but then I thought "this is getting a little silly".

Would someone please offer their advice and either say "yeah you are on the right track" or "I know a much easier way you numpty".

By the way I don't wish to start throwing page vars through the URL because I wish my prod pages URL to be kept simple http://www.mysite/products/product

Code: Select all

<?

$module_name = basename(dirname(__FILE__));

// The case 'somename' statement corresponds to the folder names on the server.

switch ($module_name):
    case 'volocity':
        echo "<div id="navcontainer">";
		echo "<ul id="navlist">";
		echo "<li id="active"><a href="/products/volocity/" id="current">Volocity</a></li>";
		echo "<li><a href="/products/volocity/visualization/">Visualization</a></li>";
		echo "<li><a href="/products/volocity/classification/">Classification</a></li>";
		echo "<li><a href="/products/volocity/restoration/">Restoration</a></li>";
		echo "<li><a href="/products/volocity/acquisition/">Acquisition</a></li>";
		echo "<li><a href="/products/volocity/3dm/">3DM</a></li>";
		echo "<li><a href="/products/volocity/volocity_le/">Volocity LE</a></li>";
		echo "</ul>";
		echo "</div>";
		break;
    case 'visualization':
        echo "<div id="navcontainer">";
		echo "<ul id="navlist">";
		echo "<li><a href="/products/volocity/">Volocity</a></li>";
		echo "<li id="active"><a href="/products/volocity/visualization/" id="current">Visualization</a></li>";
		echo "<li><a href="/products/volocity/classification/">Classification</a></li>";
		echo "<li><a href="/products/volocity/restoration/">Restoration</a></li>";
		echo "<li><a href="/products/volocity/acquisition/">Acquisition</a></li>";
		echo "<li><a href="/products/volocity/3dm/">3DM</a></li>";
		echo "<li><a href="/products/volocity/volocity_le/">Volocity LE</a></li>";
		echo "</ul>";
		echo "</div>";
        break;
	case 'classification':
        echo "<div id="navcontainer">";
		echo "<ul id="navlist">";
		echo "<li><a href="/products/volocity/">Volocity</a></li>";
		echo "<li><a href="/products/volocity/visualization/">Visualization</a></li>";
		echo "<li id="active"><a href="/products/volocity/classification/" id="current">Classification</a></li>";
		echo "<li><a href="/products/volocity/restoration/">Restoration</a></li>";
		echo "<li><a href="/products/volocity/acquisition/">Acquisition</a></li>";
		echo "<li><a href="/products/volocity/3dm/">3DM</a></li>";
		echo "<li><a href="/products/volocity/volocity_le/">Volocity LE</a></li>";
		echo "</ul>";
		echo "</div>";
        break;
	case 'restoration':
        echo "<div id="navcontainer">";
		echo "<ul id="navlist">";
		echo "<li><a href="/products/volocity/">Volocity</a></li>";
		echo "<li><a href="/products/volocity/visualization/">Visualization</a></li>";
		echo "<li><a href="/products/volocity/classification/">Classification</a></li>";
		echo "<li id="active"><a href="/products/volocity/restoration/" id="current">Restoration</a></li>";
		echo "<li><a href="/products/volocity/acquisition/">Acquisition</a></li>";
		echo "<li><a href="/products/volocity/3dm/">3DM</a></li>";
		echo "<li><a href="/products/volocity/volocity_le/">Volocity LE</a></li>";
		echo "</ul>";
		echo "</div>";
        break;
	case 'acquisition':
        echo "<div id="navcontainer">";
		echo "<ul id="navlist">";
		echo "<li><a href="/products/volocity/">Volocity</a></li>";
		echo "<li><a href="/products/volocity/visualization/">Visualization</a></li>";
		echo "<li><a href="/products/volocity/classification/">Classification</a></li>";
		echo "<li><a href="/products/volocity/restoration/">Restoration</a></li>";
		echo "<li id="active"><a href="/products/volocity/acquisition/" id="current">Acquisition</a></li>";
		echo "<li><a href="/products/volocity/3dm/">3DM</a></li>";
		echo "<li><a href="/products/volocity/volocity_le/">Volocity LE</a></li>";
		echo "</ul>";
		echo "</div>";
        break;
	case '3dm':
        echo "<div id="navcontainer">";
		echo "<ul id="navlist">";
		echo "<li><a href="/products/volocity/">Volocity</a></li>";
		echo "<li><a href="/products/volocity/visualization/">Visualization</a></li>";
		echo "<li><a href="/products/volocity/classification/">Classification</a></li>";
		echo "<li><a href="/products/volocity/restoration/">Restoration</a></li>";
		echo "<li><a href="/products/volocity/acquisition/">Acquisition</a></li>";
		echo "<li id="active"><a href="/products/volocity/3dm/" id="current">3DM</a></li>";
		echo "<li><a href="/products/volocity/volocity_le/">Volocity LE</a></li>";
		echo "</ul>";
		echo "</div>";
        break;
	case 'volocity_le':
        echo "<div id="navcontainer">";
		echo "<ul id="navlist">";
		echo "<li><a href="/products/volocity/">Volocity</a></li>";
		echo "<li><a href="/products/volocity/visualization/">Visualization</a></li>";
		echo "<li><a href="/products/volocity/classification/">Classification</a></li>";
		echo "<li><a href="/products/volocity/restoration/">Restoration</a></li>";
		echo "<li><a href="/products/volocity/acquisition/">Acquisition</a></li>";
		echo "<li><a href="/products/volocity/3dm/">3DM</a></li>";
		echo "<li id="active"><a href="/products/volocity/volocity_le/" id="current">Volocity LE</a></li>";
		echo "</ul>";
		echo "</div>";
        break;
	default:
        echo "<div id="navcontainer">";
		echo "<ul id="navlist">";
		echo "<li><a href="/products/volocity/">Volocity</a></li>";
		echo "<li><a href="/products/volocity/visualization/">Visualization</a></li>";
		echo "<li><a href="/products/volocity/classification/">Classification</a></li>";
		echo "<li><a href="/products/volocity/restoration/">Restoration</a></li>";
		echo "<li><a href="/products/volocity/acquisition/">Acquisition</a></li>";
		echo "<li><a href="/products/volocity/3dm/">3DM</a></li>";
		echo "<li><a href="/products/volocity/volocity_le/">Volocity LE</a></li>";
		echo "</ul>";
		echo "</div>";
endswitch;
?>
Cheers for all advice offered.

Jim

Posted: Mon Nov 01, 2004 6:14 am
by swdev
My idea would be to use an associative array to store the relatioship between the directory you are in and the links you want displayed.

Something like this (untested - just wote it off the top of my head :lol: )

Code: Select all

// add entries in this array for each directory
// that will not use the default handler
$dir_array['/volocity'] = 'volocity';
$dir_array['/volocity/classification'] = 'classification';
$dir_array['/volocity/classification/register'] = 'volocity';

$dir_name = dirname(__FILE__);

if (isset($dir_array[$dir_name]) == true)
{
  $module_name = $dir_array[$dir_name];
}
else
{
  $module_name = '';
}

switch($module_name)
{
  case 'volocity':
   {
      do whatever ...
      break;
    }
  case 'classification':
   {
      do whatever ...
      break;
    }
  default:
   {
      do whatever ...
      break;
    }
}
Hope this helps

Posted: Mon Nov 01, 2004 6:34 am
by hairyjim
Yeah I see how this could work, but I do not wish to have to specify each sub dir in an associative array as you suggest.

It would mean that every time I or another developer added another dir under /volocity/ they would have to mod this code and add it to the list.

Posted: Mon Nov 01, 2004 8:02 am
by swdev
I can see where you are coming from

So if you added another directory under the 'volocity' directory, what links do you want displayed?

e.g.

directory links
volocity volocity
volocity/classification ???
volocity/new ???
volocity.classification/register ???

please fill in the ???

Posted: Mon Nov 01, 2004 8:22 am
by hairyjim
The navigation bar will not change, well actually saying 'never' will probably not be true but it will change far less often (if at all).

So on all pages for the volocity product there is a nav bar that has the following links.

* Volocity
* Visualization
* Classification
* Restoration
* Acquisition
* 3DM
* Volocity LE

So lets say I have the following as a DIR structure. I have had to use dashes to signify directory depth.

Volocity //(uppermost dir)
-index.php
-features.php
-Classification
--features.php
--anotherfolder
---registration.php

When a user is viewing features.php in the volocity dir then the volocity nav button is a different colour to the other module links.

If a user is navigating features.php in the classification dir then the nav bar will have the classification button a different colour. The same would be true if they were navigating the registration.php file that is within the anotherfolder directory that is within the classification directory.

Posted: Mon Nov 01, 2004 10:20 am
by swdev
Ok I see what you mean.

Try out this code It uses the php function 'explode' to do its magic.

Code: Select all

function GetModuleName($Subject)
{
  $results = array();
  $limit = 4;
  $separator = '/';
  $ret = '';

  $results = explode($separator, $Subject, $limit);
  if(true == isset($results[2]) && (false == empty($results[2])) )
  {
    $ret = $results[2];
  }
  elseif (true == isset($results[1]) && (false == empty($results[1])) )
  {
    $ret = $results[1];
  }
  else
  {
    $ret = $results[0];
  }

  if ( ($ret == '\'') || ($ret == '/') )
  {
    $ret = '';
  }

  // this echo for diagnostic purposes only
  echo '<p>module name ~' . $ret . '~ from ~' . $Subject . '~</p>';
  return ($ret);
}

// Code to test GetModuleName function
$test_arr[] = '';
$test_arr[] = '/';
$test_arr[] = '/volocity/';
$test_arr[] = '/volocity/index.php';
$test_arr[] = '/volocity/Classification';
$test_arr[] = '/volocity/Classification/';
$test_arr[] = '/volocity/Classification/index.php';
$test_arr[] = '/volocity/Classification/registration';
$test_arr[] = '/volocity/Classification/registration/';
$test_arr[] = '/volocity/Classification/registration/index.php';
$test_arr[] = '/volocity/Classification/registration/something else';
$test_arr[] = '/volocity/Classification/registration/something else/';
$test_arr[] = '/volocity/Classification/registration/something else/index.php';

foreach ($test_arr as $val)
{
  $mod_name = GetModuleName(dirname($val));
}
and here is the output it produced

module name ~~ from ~~

module name ~~ from ~\~

module name ~~ from ~\~

module name ~volocity~ from ~/volocity~

module name ~volocity~ from ~/volocity~

module name ~volocity~ from ~/volocity~

module name ~Classification~ from ~/volocity/Classification~

module name ~Classification~ from ~/volocity/Classification~

module name ~Classification~ from ~/volocity/Classification~

module name ~Classification~ from ~/volocity/Classification/registration~

module name ~Classification~ from ~/volocity/Classification/registration~

module name ~Classification~ from ~/volocity/Classification/registration~

module name ~Classification~ from ~/volocity/Classification/registration/something else~

I have used the ~ character to delineate the strings.

Hope this helps

Posted: Mon Nov 01, 2004 10:54 am
by hairyjim
:lol:

I started working on using the 'explode' method to generate a list using the '/' as a seperator then performing a regex on the found results and doing stuff based on the last 'matched'.

Lets just say it started going nowhere.

Hmmm - there seems ot be a slight problem in what you have coded in the following lines:

Code: Select all

if ( ($ret == ''') || ($ret == '/') )
  &#123;
    $ret = '';
  &#125;
Gives me an error:
PHP Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING

Posted: Mon Nov 01, 2004 11:56 am
by swdev
that line should be

Code: Select all

i f   (   ( $ r e t   = =   ' \ \ ')   | |   ( $ r e t   = =   ' / ' )   )
Just replace all the double spaces with 1 space
essentially, the 1 test should read dollarret equals equals singlequote backslash backslash singlequote with no spaces

This line was an extra test to make sure that you don't get just a path separator charater being returned

Posted: Tue Nov 02, 2004 3:17 am
by hairyjim
I bow to you sir. You have been most helpful.

The function does its job and I will integrate it into my navigation template.

Posted: Tue Nov 02, 2004 5:01 am
by swdev
It has been my pleasure to help out.

Hope your site is successful.

Posted: Tue Nov 02, 2004 8:12 am
by hairyjim
Just to let you know I modded the code slightly.

The directory string I get back is /products/volocity/classification/whatever/whatever.php

So your code pulled out 'product' & 'volocity' and never got to the module directory. So I changed the code so the If statement checked array values 2 & 3 like thus :

Code: Select all

<?

function GetModuleName($Subject)
&#123;
  $results = array();
  $limit = 5;
  $separator = '/';
  $ret = '';

  $results = explode($separator, $Subject, $limit);
  if(true == isset($results&#1111;3]) && (false == empty($results&#1111;3])) && (false == strpos($results&#1111;3], php)) )
  &#123;
    $ret = $results&#1111;3];
  &#125;
  elseif (true == isset($results&#1111;2]) && (false == empty($results&#1111;2])) )
  &#123;
    $ret = $results&#1111;2];
  &#125;
  else
  &#123;
    $ret = $results&#1111;0];
  &#125;

  if ( ($ret == '\'') || ($ret == '/') )
  &#123;
    $ret = '';
  &#125;

?>
The only problem this threw at me was when the dir was just /product/volocity/index.php it would return the dir as index.php because value[3] was set, so I put in a check to find 'php' and if it did move to value[2]

Now everything works a charm.