Category Menus HELP

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

By zaba - February 25, 2014

I'm baffled by how this works.

Firstly If I create a new Category Menu, based on 2 levels, Parent Level and Child Level. How do I get it to output to display like the following when parsed?

Quite baffled as to what the code would be.

<ul id="menu">


<li><a href="#">PARENT A</a>
    <ul>    
    <li><a href="#">PARENT A CHILD 1</a></li>
    <li><a href="#">PARENT A CHILD 2</a></li>
    <li><a href="#">PARENT A CHILD 3</a></li>
   </ul>
</li>
<li><a href="#">PARENT B</a>
    <ul>
    <li><a href="#">PARENT B CHILD 1</a></li>
    <li><a href="#">PARENT B CHILD 2</a></li>
    <li><a href="#">PARENT B CHILD 3</a></li>
    </ul>
</li>

<li><a href="#">PARENT C</a>
    <ul>
    <li><a href="#">PARENT C CHILD 1</a></li>
    <li><a href="#">PARENT C CHILD 2</a></li>
    <li><a href="#">PARENT C CHILD 3</a></li>
    </ul>
</li>
</ul>

Secondly I will then be creating individual tables (some will be multi pages and some will be single pages for each of the Categories/sub categories above) 

How would I then populate the <a href above to marry up to the correct tables?

Sorry Ive searched the forum and can't find anything that will solve this for me.

By gregThomas - February 26, 2014 - edited: February 26, 2014

Hi Zaba,

Creating an indented category page is actually fairly easy, here is some example code for a test section I have:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  
  // 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($animalWiki, $animal_wiki) = getCategories(array(
    'tableName'   => 'animal_wiki',
    'loadUploads' => true,
  ));
 ?>

 <html>
  <body>
    <ul>
      <?php foreach($animalWiki as $animal): ?>
        <?php echo $animal['_listItemStart']; ?>
          <?php echo $animal['name']; ?>
        <?php echo $animal['_listItemEnd']; ?>
      <?php endforeach; ?>
    </ul>
  </body>
 </html>

So instead of using the getRecords function to retrieve the records, I'm using the getCategories function. This will add some extra fields, that include the _listItemStart and _listItemEnd. These variables contain the appropriate list html tag for each item (for example "<li>" or "<li><ul>"). 

Does your category system have a depth greater than two? If so it's possible to only retrieve the first two layers by adding:

'categoryFormat' => 'twolevel'

to your getCategories function.

Is there a particular reason that you're using multiple sections for each sub category item? If the data is going to be the same for each section, I would store the data directly in the category section.  Otherwise, I'd just have a link field in category section that you can add a link to the appropriate page.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By zaba - February 26, 2014

Hi Greg,

Yes I think I can follow that, each of the pages will have different layouts and data so there is a need to create different tables for the different types. I'll just add a link field in to marry those up, seems logical.

The only other thing I need to clarify is that on each of the Parent Section Pages I want to show the child links on that particular page. (I am using the main menu as pull downs which I have working)

So when you click on one of the linked pages it will show the parent heading and the child links for that particular page/section.

thanks

By gregThomas - February 26, 2014

Hi zaba,

You can display only the child links for a particular category using the rootCategoryNum in your getCategories function:

    'rootCategoryNum' => '8'

When you pass in a num value this way, only items below this in the category menu will be returned.

Let me know if you need an example of how this could be implemented.

Cheers!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Chris - February 28, 2014

Hi zaba,

Assuming you're using detail page URLs (which have the record number in the URL) to select your Parent Section Pages, you can use getLastNumberInUrl() to get the record number to use as a rootCategoryNum.

Here's an example:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  
  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
  
  $parentNum = getLastNumberInUrl(0);
 
  // get parent category
  list($parentCategoryRecords) = getRecords(array(
    'tableName'   => 'category',
    'where'       => mysql_escapef('num = ?', $parentNum),
    'allowSearch' => false,
  ));
  $parentCategory = @$parentCategoryRecords[0];
  if (!$parentNum || !$parentCategory) { die("Parent Category not found"); }
  
  // load child categories
  list($categoryRecords, $selectedCategoryRecord) = getCategories(array(
    'tableName'       => 'category',
    'loadUploads'     => true,
    'rootCategoryNum' => $parentNum,
  ));
  
?>

<html>
  <body>
    <h1><?php echo htmlencode($parentCategory['name']) ?></h1>
    <ul>
      <?php foreach($categoryRecords as $category): ?>
        <?php echo $category['_listItemStart']; ?>
          <?php echo htmlencode($category['name']); ?>
        <?php echo $category['_listItemEnd']; ?>
      <?php endforeach; ?>
    </ul>
  </body>
</html>

Note that when using rootCategoryNum, you won't get the parent record back, so I've included an extra getRecords() call to get the parent category.

Please let me know if you have any questions.

All the best,
Chris

By zaba - March 3, 2014

Thanks Chris,

I'll be working on this this week, appreciate your response. :-)