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 need to create a dynamic drop down menu using a Category menu type and have the basic structure working below but not sure how to add/edit the needed code to get it to display properly using the nav items in the admin - or even if this code will work with displaying the nav like that.

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

Here is my header code if it helps at all:

<?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

Hi drewh01,

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

Thanks, it's not quite working for me when I paste that in - or am I missing something?

Re: [drewh01] Creating a dynamic dropdown menu

By (Deleted User) - April 17, 2012

Hi drewh01,

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

ok, I got it displaying - but now the drop downs are not appearing and on the pages that do have a drop down - all of those nav items are displaying across the page.

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

Hi drewh01,

If you attach the script that's creating the page I can have a look and see what's happening.

Tom

Re: [drewh01] Creating a dynamic dropdown menu

By (Deleted User) - April 18, 2012 - edited: April 18, 2012

Hi drewh01,

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
Attachments:

index-(1).php 12K

Re: [Tom P] Creating a dynamic dropdown menu

By drewh01 - April 18, 2012

Thanks - is my old attached file the one I use or....?