Creating a dynamic dropdown menu
31 posts by 7 authors in: Forums > CMS Builder
Last Post: October 12, 2012 (RSS)
By drewh01 - April 17, 2012
I am hoping to avoid hard coding the nav so the user can add/edit pages in the CMS admin...etc.
thx,
<ul id="sddm">
<li><a href="#">Home</a></li>
<li><a href="#">News</a></li>
<li><a href="#"
onmouseover="mopen('m1')"
onmouseout="mclosetime()">Hummingbirds 101</a>
<div id="m1"
onmouseover="mcancelclosetime()"
onmouseout="mclosetime()">
<a href="#">Attracting and feeding</a>
<a href="#">Injured hummer</a>
<a href="#">Predators</a>
<a href="#">Albino/leucistic</a>
<a href="#">Look-alikes</a>
</div>
</li>
<li><a href="#">Endangered</a></li>
<li><a href="#">Newsletters</a></li>
<li><a href="#">Free Stuff</a></li>
<li><a href="#"
onmouseover="mopen('m2')"
onmouseout="mclosetime()">Sponsors</a>
<div id="m2"
onmouseover="mcancelclosetime()"
onmouseout="mclosetime()">
<a href="#">Shopping</a>
<a href="#">Sponsoring</a>
</div>
</li>
<li><a href="#"
onmouseover="mopen('m3')"
onmouseout="mclosetime()">About us</a>
<div id="m3"
onmouseover="mcancelclosetime()"
onmouseout="mclosetime()">
<a href="#">About Sedona</a>
</div>
</li>
<li><a href="#">Contact</a></li>
</ul>
<div style="clear:both"></div>
Re: [drewh01] Creating a dynamic dropdown menu
By drewh01 - April 17, 2012
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/hummingb/public_html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
// load records
list($main_pagesRecords, $main_pagesMetaData) = getRecords(array(
'tableName' => 'main_pages',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$main_pagesRecord = @$main_pagesRecords[0]; // get first record
// show error message if no matching record is found
if (!$main_pagesRecord) { dieWith404("Record not found!"); }
?>
Re: [drewh01] Creating a dynamic dropdown menu
By (Deleted User) - April 17, 2012
To load the categories you've created use:
list($allPages, $selectedPage) = getCategories(array(
'tableName' => 'main_pages',
'categoryFormat' => 'onelevel', // showall, onelevel, twolevel, breadcrumb
));
And to output a list based on the category structure you can use this:
<!-- category menu -->
<ul>
<?php foreach ($allPages as $page): ?>
<?php echo $page['_listItemStart'] ?>
<?php if ($page['_isSelected']): ?>
<b><a href="<?php echo $page['_link'] ?>"><?php echo $page['name'] ?></a></b>
<?php else: ?>
<a href="<?php echo $page['_link'] ?>"><?php echo $page['name'] ?></a>
<?php endif; ?>
<?php echo $page['_listItemEnd'] ?>
<?php endforeach; ?>
</ul>
<!-- /category menu -->
The above is just the basic method of outputting those items (with the currently selected page highlighted). You can modify it to add any extra features you need.
Let me know if this helps,
Tom
Re: [Tom P] Creating a dynamic dropdown menu
By drewh01 - April 17, 2012
Re: [drewh01] Creating a dynamic dropdown menu
By (Deleted User) - April 17, 2012
Have you set up the main_pages table as a category type or as a multi-record type? In order for the above to work, it must be a category type.
Tom
Re: [Tom P] Creating a dynamic dropdown menu
By drewh01 - April 17, 2012
I know I need to edit the code a bit but wondering the best method to get the drop downs to play nice.
thanks!
Re: [drewh01] Creating a dynamic dropdown menu
By (Deleted User) - April 18, 2012
If you attach the script that's creating the page I can have a look and see what's happening.
Tom
Re: [Tom P] Creating a dynamic dropdown menu
By drewh01 - April 18, 2012
If I need to go that route, it would be fine at this point - but I thought I would see if I can make the dynamic method work first as it's something I always wanted to figure out.
Re: [drewh01] Creating a dynamic dropdown menu
By (Deleted User) - April 18, 2012 - edited: April 18, 2012
I've made some small changes to the file - let me know if the layout has improved.
I haven't attempted to modify the layout of the menu produced by the loop as our first objective is to get it working without the additional javascript features.
Tom
Re: [Tom P] Creating a dynamic dropdown menu
By drewh01 - April 18, 2012