Navigation List Issue

7 posts by 3 authors in: Forums > CMS Builder
Last Post: February 4, 2014   (RSS)

Hi there,

Have been having probs debugging this and I think I am running into issues with how _listItemStart and _listItemEnd work.

Here is my code:

<ul class="top">
<?php $inactiveChild=0; ?> 
<?php $childCount=0; ?> 
<?php foreach ($destinationsRecords as $record): ?>
 <?php if($record['active']==1 && $record['hide']==0 && $record['depth']<2): ?>
    <?php if($inactiveChild && !$record['_hasParent']): ?></ul></li><?php endif ?>
    <?php if($record['_hasParent']): ?><?php $childCount++; ?><?php else:?><?php $childCount=0;?><?php endif ?>
    <?php $inactiveChild=0; ?>
    <?php echo $record['_listItemStart']; ?><a href="/<?php echo $record['_link'];?>"><?php echo htmlspecialchars($record['name']);?></a><?php echo $record['_listItemEnd'];?>    
  <?php else: ?>
     <?php if($record['_hasParent'] && $childCount==0): ?><ul><?php $childCount++; ?><?php endif ?>
     <?php if($record['_hasParent']): ?><?php $inactiveChild=1;?><?php else: ?><?php $inactiveChild =0; ?><?php endif ?>
  <?php endif ?>
<?php endforeach ?>
</ul>

And here is what the output looks like with comments showing the missing list tags:

<ul class="top">
  <li><a href="#">Top Item</a></li>
  <li><a href="#">Top Item</a>
    <ul>
      <li><a href="#">Sub Item</a></li>
      <li><a href="#">Sub Item</a><!-- missing </li> has 3rd level under here -->
      <li><a href="#">Sub Item</a></li>
    </ul>
  </li>
  <li><a href="#">Item</a>
    <ul>
      <li><a href="#">Sub Item</a></li>
      <li><a href="#">Sub Item</a><!-- missing </li> has 3rd level under here -->
    </ul>
  </li>
  <li><a href="#">Item</a></li>
  <li><a href="#">Item</a>
    <ul>
      <li><a href="#">Sub Item</a></li>
      <li><a href="#">Sub Item</a></li>
      <li><a href="#">Sub Item</a><!-- missing </li> has 3rd level under here -->
    <!-- missing closing tags </ul></li> here -->
</ul>

I believe part of the problem may be due to restricting to two levels using $record['depth']<2

Any suggestions?

Many thanks!

By rconring - February 3, 2014 - edited: February 3, 2014

Make sure the viewer is using  getCategories(array( NOT getRecords(array( Then just place _listItemStart and _listItemEnd at the top and bottom of the foreach loop.  It will place the list tags in the appropriate places ... you do not have to track parent/child records.

Here is some info on it:  http://www.interactivetools.com/forum/forum-posts.php?postNum=2192758#post2192758

Also FYI, if you use the special fieldname "hidden" instead of "hide" you do not have to manually filter out hidden records.  The viewer omits them.

<ul class="top">
<?php foreach ($destinationsRecords as $record): ?>
  <?php echo $record['_listItemStart']; ?>
  <?php if($record['active']==1 && $record['hide']==0): ?>
    <a href="/<?php echo $record['_link'];?>"><?php echo htmlspecialchars($record['name']);?></a>  
  <?php endif ?>
<?php echo $record['_listItemEnd']; ?>
<?php endforeach ?>
</ul>

Ron Conring
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987

Hi,

Ron's suggestion seems to be on the money. Have you had any luck implementing his suggestion?

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Ron,

This works if I show all levels, if I add in  && $record['depth']<2 to restrict to two levels, I still get a bunch of empty list items such as:

<ul class="top">
  <li> <a href="#">Item</a>
    <ul>
      <li> </li><!--Empty Item-->
      <li> <a href="#">Item</a> </li>
      <li> <a href="#">Item</a>
        <ul>
          <li> </li><!--Empty Item-->
          <li> </li><!--Empty Item-->
          <li> </li><!--Empty Item-->
        </ul>
      </li>
      <li> <a href="#">Item</a> </li>
      <li> <a href="#">Item</a> </li>
    </ul>
    etc...

Maybe the solution is to show everything and then use css to hide the third level, messy but gets the job done.

Many thanks

Jan

Hi Jan,

I think the best way around the problem is to filter the depth level via the getCategories function instead of via PHP. Here is how I would do it:

  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('/home/greg/www/','','../','../../','../../../');
  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 from 'animal_wiki'
  list($animal_wiki, $animal_wikiMetaData) = getCategories(array(
    'tableName'   => 'animal_wiki',
    'categoryFormat' => 'twolevel'
  ));
?>
<ul class="top">
<?php foreach ($animal_wiki as $animal): ?>
  <?php echo $animal['_listItemStart']; ?>
    <a href="/<?php echo $record['_link'];?>"><?php echo htmlspecialchars($animal['name']);?></a>   
<?php echo $animal['_listItemEnd']; ?>
<?php endforeach ?>
</ul>

So in my example I've created a dictionary of animals. I'm using the getCategories function to retrieve the records, and set an option called categoryFormat to retrieve records with a maximum depth of two (highlighted in green). The possible options for this are: showall, onelevel, twolevel, breadcrumb

Then you can use a simple foreach loop to loop through each item and display the appropriate ul/li tags. 

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Greg, Perfect!! I forgot that those functions existed for restricting categories. Nice one :)