Inserting different variables in JavaScript

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

Thanks Greg,

One small typo fix made it work perfectly.

I changed this:

 $items = array();
  if($record['use_submenu']']){

 To This:

$items = array();
  if($record['use_submenu'] == 1){

Now the only thing remaining is to make the menu entries dynamic.

Unless you can think of a better approach, I guess that I'll have to create a few new sections.

One section with fields for a dynamic list of values for 'entry' and for 'use_submenu, and another section for any submenu links associated with that entry value.

I'll post my progress.

Thanks again,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

It took a while for me to get back to this, and attempt to get the menu items to be dynamic, but I’ve made some progress.

I've created a multi record editor called new_menu_categories with a title (used to populate the category 'entry' list field in the other table), a  use_submenus check box, and link URL and Text fields to ultimately be used as the menu values if the check box is not checked

And a second multi record editor called new_subcategories with a list field to assign the record to a menu category, and a submenu-link_URL and submenu_link_text field for the drop down submenu values.

I’ve got the .js.php file to output the information in the correct format, but I’m getting repeats of all the categories in the viewer navigation menu.

There should only be one entry for each menu Category, but there are as many entries as there are records with that Category chosen as the entry value.

I’ve tried all the tricks that I know and I’m stuck as to how make this work correctly.

It should make more sense after looking at: http://www.artculturegroup.com/index30.php

Thanks,

Jerry Kornbluth

Here’s the new working code for the .js.php file.

<?php $output = ''; ?>
<?php
foreach ($new_menu_categoriesRecords as $record) {
 
  // determine div class
  $divClass = "";
  if ($record['use_submenu'] == '1') { $divClass = "anylinkmenu"; }
 
  // determine variable name
  $entry = strtolower($record['title']);
 
  // determine item list    
  $items = array();
  if($record['use_submenu'] == 1){
     foreach ($new_menu_subcategoriesRecords as $record)
    if (strtolower($record['entry:label'])== $entry) { $items[] = array($record['submenu_link_text'], $record['submenu_link_url']); }
    }
    
  // output javascript
   echo "var $entry = {divclass:'$divClass', inlinestyle:'width:150px; background:#DAD2D0', linktarget:''};\n";
  echo "$entry.items = " . json_encode($items) . ";\n";
}

?> 

And here’s the code for the viewer:

<table  align="center" width="900px" border="0">
<tr>
<?php foreach ($new_menu_subcategoriesRecords as $record): ?>
<?PHP $spaced_entry = preg_replace("/[-_]/", "&nbsp;", $record['entry:label'] ); ?>
<td width="16%" valign="top" align="center" ><a href="<?php echo strtolower($record['submenu_link_url']) ?>" class="menuanchorclass" rel="<?php echo strtolower($record['entry:label']) ?>"><span class="sub_heading_font"><?php echo $spaced_entry ?></span></a></td>
<?php endforeach ?>
</tr>
</table>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Hi Jerry,

I think I can see what the problem is here. On the code that displays the HTML links, you're looping through the sub category menu items, so the number of menu items being displayed is the same as the number of sub category menu items for that particular item. 

I think the way to get around this would be to cycle through the category menu items instead. I think something like this would work:

<table  align="center" width="900px" border="0">
  <tr>
  <?php foreach ($new_menu_categoriesRecords as $record): ?>
    <?php $spaced_entry = preg_replace("/[-_]/", "&nbsp;", $record['title'] ); ?>
    <td width="16%" valign="top" align="center" ><a href="<?php echo strtolower(@$record['submenu_link_url']) ?>" class="menuanchorclass" rel="<?php echo strtolower($record['title']) ?>">
      <span class="sub_heading_font"><?php echo $spaced_entry ?></span></a>
    </td>
  <?php endforeach ?>
  </tr>
</table>

So instead of looping through the sub menu items and finding its parent link, we loop through the parents links instead.

The only potential issue I see is with the $record['submenu_link_url'] variable. I'm not sure if the parent categories has a submenu link URL field, or what link should be used if it doesn't.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com