Jump to content

Recommended Posts

I'm relatively new to php so please forgive any noobish questions that I may have..  ;)

 

I've created a left menu (within an inc) that is simply a series of styled unordered lists.  I would only like to display the portion of the menu relative to what page the user is on.  So for example: if the user is looking at cars and the menu covers cars, trucks, and motorcycles; I would only like the cars section to be presented.

 

    <div id="left-nav">
       	  <!-- Cars -->
          <ul>
          	<li><a href="#">Car</a></li>
          	<li><a href="#">Car 1</a></li>
          	<li><a href="#">Car 2</a></li>
          	<li><a href="#">Car 3</a></li>
          </ul>
       	  <!-- Trucks -->
          <ul>
          	<li><a href="#">Truck</a></li>
          	<li><a href="#">Truck 1</a></li>
          	<li><a href="#">Truck 2</a></li>
          	<li><a href="#">Truck 3</a></li>
          </ul>
       	  <!-- Motorcycles -->
          <ul>
          	<li><a href="#">Motorcycle</a></li>
          	<li><a href="#">Motorcycle 1</a></li>
          	<li><a href="#">Motorcycle 2</a></li>
          	<li><a href="#">Motorcycle 3</a></li>
          </ul>
    </div>

 

I assume this can be handled by a simple id but, unfortunately I don't know the code implantation to make it functional.

 

Thank you much in advance!

 

Here is a better example of what I have done so far.  Hope this helps.  I was able to get only the "cars" section to display but I receive an error (Notice: Undefined variable: trucks in C:\server\www\BA\inc\left-nav.php on line 32 ) for trucks and motorcycles on the cars page.

 

Code on the included menu:

<div id="left-nav">

       	  <!-- Cars -->
          <?php if ($cars == true): ?>
          <ul>
          	<li><a href="#">Car</a></li>
          	<li><a href="#">Car 1</a></li>
          	<li><a href="#">Car 2</a></li>
          	<li><a href="#">Car 3</a></li>
          </ul>
          <?php endif; ?>

       	  <!-- Trucks -->
          <?php if ($trucks == true): ?>
          <ul>
          	<li><a href="#">Truck</a></li>
          	<li><a href="#">Truck 1</a></li>
          	<li><a href="#">Truck 2</a></li>
          	<li><a href="#">Truck 3</a></li>
          </ul>
          <?php endif; ?>

       	  <!-- Motorcycles -->
          <?php if ($motorcycle == true): ?>
          <ul>
          	<li><a href="#">Motorcycle</a></li>
          	<li><a href="#">Motorcycle 1</a></li>
          	<li><a href="#">Motorcycle 2</a></li>
          	<li><a href="#">Motorcycle 3</a></li>
          </ul>
          <?php endif; ?>

    </div>

 

Code on the corresponding page:

<?
$cars = true;
include('../inc/left-nav.php');
?>

The error is because you are looking for individual variables like $cars, $trucks etc.. but you only define one of them at a given time.  Instead of defining individual variables with a value of true, a better method would be to define a single variable with individual values like 'cars', 'trucks' etc...and then output based on the value of the single variable:

 

"corresponding page"

<?php
  $leftnav = 'cars';
  include('../inc/left-nav.php');
?>

 

left-nav.php

<?php
$list = array(
  'cars' => array(
    'Car',
    'Car 1',
    'Car 2',
    'Car 3',
  ),
  'trucks' => array(
    'Truck',
    'Truck 1',
    'Truck 2',
    'Truck 3',
  ),
  'motorcycles' => array(
    'Motorcyle',
    'Motorcyle 1',
    'Motorcyle 2',
    'Motorcyle 3',
  )
);		  
?>

<div id="left-nav">
<?php if ( isset($leftnav)&&isset($list[$leftnav]) ) { ?>
  <ul>
<?php   foreach ($list[$leftnav] as $link) { ?>
    <li><a href="#"><?php echo $link ?></a></li>
<?php   } ?>
  </ul>
<?php } ?>
</div>

Thank you... Works great! 

 

Finally, how do I apply individual links and exclude the first "li" for a title?

 

Example:

 

<ul>
  <li><h3>Car</h3></li>
  <li><a href="<?= $dots('cars/model/car-1.php'); ?>">Car 1</a></li>
  <li><a href="<?= $dots('cars/model/car-2.php'); ?>">Car 2</a></li>
  <li><a href="<?= $dots('cars/model/car-3.php'); ?>">Car 3</a></li>
</ul>

 

Thank you again for all the help!

but is $dots javascript or php?

 

I'm going to assume it is supposed to be some kind of php callback function to reformat the url...so there is a placeholder function in script below. 

 

$list = array(
  'cars' => array(
    'Car 1',
    'Car 2',
    'Car 3',
  ),
  'trucks' => array(
    'Truck 1',
    'Truck 2',
    'Truck 3',
  ),
  'motorcycles' => array(
    'Motorcyle 1',
    'Motorcyle 2',
    'Motorcyle 3',
  )
);		  

function dots($url) { return $url; }
?>

<div id="left-nav">
<?php if ( isset($leftnav)&&isset($list[$leftnav]) ) { ?>
  <ul>
    <li><h3><?php echo ucfirst($leftnav) ?></h3></li>
<?php   foreach ($list[$leftnav] as $k=> $link) { ?>
    <li><a href="<?php echo dots($leftnav.'/model/'.$leftnav.'-'.++$k.'.php'); ?>"><?php echo $link ?></a></li>
<?php   } ?>
  </ul>
<?php } ?>
</div>

 

 

It is php in my header file.

 


<?
	$dots = function($url=null){
	$tmp = dirname($_SERVER["PHP_SELF"]);
	$tmp = str_replace('\\', '/', $tmp);
	$tmp = explode('/', $tmp);

	$relpath = null;
	for ($i = 0; $i < count($tmp); $i++){
		if ($tmp[$i] != '' && $i >= 2) $relpath .= '../';
	}

	$relpath = ($relpath != null) ? substr($relpath, 0, -1) : '.';

	if ($url){
		if (substr($url, 0, 1) != '/')
			return $relpath .= ('/' . $url);
		return $relpath .= $url;
	}
	else
		return $relpath;
};
?>

From your PM, it sounds like there is no pattern to the links per-se; instead you need to be able to output arbitrary links.  Here is adjusted code for being able to specify arbitrary links:

 

$list = array(
  'cars' => array(
    'Car 1' => 'path/to/cars1.php',
    'Car 2' => 'path/to/cars2.php',
    'Car 3' => 'path/to/cars3.php',
  ),
  'trucks' => array(
    'Truck 1' => 'path/to/trucks1.php',
    'Truck 2' => 'path/to/trucks2.php',
    'Truck 3' => 'path/to/trucks3.php',
  ),
  'motorcycles' => array(
    'Motorcyle 1' => 'path/to/motorcycle1.php',
    'Motorcyle 2' => 'path/to/motorcycle2.php',
    'Motorcyle 3' => 'path/to/motorcycle3.php',
  )
);		  
?>

<div id="left-nav">
<?php if ( isset($leftnav)&&isset($list[$leftnav]) ) { ?>
  <ul>
    <li><h3><?php echo ucfirst($leftnav) ?></h3></li>
<?php   foreach ($list[$leftnav] as $label => $link) { ?>
    <li><a href="<?php echo $dots($link); ?>"><?php echo $label ?></a></li>
<?php   } ?>
  </ul>
<?php } ?>
</div>

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.