Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery menu not loading PHP child categories

We've had a custom jQuery menu which has worked well on our OpenCart store. However the 2nd level child categories will not display, the PHP isn't correct on either the altered or original menu. Anything with a category with 2 or more depth will not show.

So we have a 2 deep category at http://ocart.site/opencart/index.php?route=product/category&path=25_29_59

But you see it will not display in the main menu at http://ocart.site/opencart

If I add back in the default parts of the original default menu, it actually makes the breaks the menu. You can see the comparison of old and new menus at http://ocart.site/defaultmenutoJQ.html

Something is blocking the load of display somewhere, any ideas on what it could be?

Here is all the relevant code at JSFiddle https://jsfiddle.net/mtq5khz0/

  <?php if ($categories) { ?>
  <div id="cssmenu">
    <ul>
      <?php foreach ($categories as $category) { ?>
      <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
          <?php for ($i = 0; $i < count($category['children']);) { ?>
          <ul>
            <?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
            <?php for (; $i < $j; $i++) { ?>
            <?php if (isset($category['children'][$i])) { ?>
            <li><a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?></a></li>
            <?php } ?>
            <?php } ?>
          </ul>
          <?php } ?>
        <?php } ?>
      </li>
    </ul>
  </div>
  <?php } ?>

Entire header code for OpenCart - Edited Code https://jsfiddle.net/v5vmLbjj/

Entire header code for OpenCart Default Installation https://jsfiddle.net/tneqy2qt/

(Category dropdown menu code, close to footer of code block)

and here is the JQuery menu code the PHP works with https://jsfiddle.net/ezhnnbsg/

like image 653
me9867 Avatar asked Dec 20 '16 08:12

me9867


1 Answers

Your internal loop code seems quite odd. Following the code, it looks like it would work, but it's possible error prone. So I rewrote it a little:

if ($categories) {
    echo '<div id="cssmenu">';
    echo '  <ul>';
    foreach ($categories as $category) {
        echo '    <li><a href="' . $category['href'] .'">' . $category['name'] . '</a>';

        if (count($category['children']) > 0) {
            $columns = array_chunk($category['children'], ceil(count($category['children']) / $category['column']));
            foreach ($columns as $children) {
                echo '      <ul>';
                foreach ($children as $child) {
                    echo '        <li><a href="' . $child['href'] . '">' . $child['name'] . '</a></li>';
                }
                echo '      </ul>';
            }
        }
    }
    echo '    </li>';
    echo '  </ul>';
    echo '</div>';
}
like image 118
Ronald Swets Avatar answered Nov 15 '22 06:11

Ronald Swets