wait Posted May 27, 2012 Share Posted May 27, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/263218-how-do-i-display-select-content-in-an-unordered-list/ Share on other sites More sharing options...
.josh Posted May 27, 2012 Share Posted May 27, 2012 can't even begin to help you without seeing how the rest of your page is setup, url structure, etc.. Quote Link to comment https://forums.phpfreaks.com/topic/263218-how-do-i-display-select-content-in-an-unordered-list/#findComment-1348986 Share on other sites More sharing options...
wait Posted May 27, 2012 Author Share Posted May 27, 2012 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'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/263218-how-do-i-display-select-content-in-an-unordered-list/#findComment-1348995 Share on other sites More sharing options...
.josh Posted May 27, 2012 Share Posted May 27, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/263218-how-do-i-display-select-content-in-an-unordered-list/#findComment-1349002 Share on other sites More sharing options...
wait Posted May 27, 2012 Author Share Posted May 27, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/263218-how-do-i-display-select-content-in-an-unordered-list/#findComment-1349015 Share on other sites More sharing options...
.josh Posted May 27, 2012 Share Posted May 27, 2012 what is $dots(...) supposed to do? Is that supposed to be javascript or php? Quote Link to comment https://forums.phpfreaks.com/topic/263218-how-do-i-display-select-content-in-an-unordered-list/#findComment-1349019 Share on other sites More sharing options...
wait Posted May 27, 2012 Author Share Posted May 27, 2012 That fixes my issue with nested directories. The left nav is on every page so $dots function creates relative links based on where you are within the site. I didn't want to build my left-nav with absolute links. Quote Link to comment https://forums.phpfreaks.com/topic/263218-how-do-i-display-select-content-in-an-unordered-list/#findComment-1349022 Share on other sites More sharing options...
.josh Posted May 27, 2012 Share Posted May 27, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/263218-how-do-i-display-select-content-in-an-unordered-list/#findComment-1349024 Share on other sites More sharing options...
wait Posted May 27, 2012 Author Share Posted May 27, 2012 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; }; ?> Quote Link to comment https://forums.phpfreaks.com/topic/263218-how-do-i-display-select-content-in-an-unordered-list/#findComment-1349025 Share on other sites More sharing options...
.josh Posted May 27, 2012 Share Posted May 27, 2012 okay, well there you have it then. I did use dots(...) instead of $dots(...) (no $) so you'll have to change that. Quote Link to comment https://forums.phpfreaks.com/topic/263218-how-do-i-display-select-content-in-an-unordered-list/#findComment-1349026 Share on other sites More sharing options...
wait Posted May 27, 2012 Author Share Posted May 27, 2012 Sent you a PM. Quote Link to comment https://forums.phpfreaks.com/topic/263218-how-do-i-display-select-content-in-an-unordered-list/#findComment-1349030 Share on other sites More sharing options...
.josh Posted May 27, 2012 Share Posted May 27, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/263218-how-do-i-display-select-content-in-an-unordered-list/#findComment-1349053 Share on other sites More sharing options...
wait Posted May 27, 2012 Author Share Posted May 27, 2012 Exactly what I was looking for! Thank you for spending the time to help. Quote Link to comment https://forums.phpfreaks.com/topic/263218-how-do-i-display-select-content-in-an-unordered-list/#findComment-1349062 Share on other sites More sharing options...
.josh Posted May 27, 2012 Share Posted May 27, 2012 no problem! Quote Link to comment https://forums.phpfreaks.com/topic/263218-how-do-i-display-select-content-in-an-unordered-list/#findComment-1349066 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.