limit number of categories of a php page

Asked

Viewed 70 times

0

ola I have a php function that shows the categories registered in the database, however I would like to limit the number of categories that appear, see the code

if ($output!='') {
                // @todo ul here is hacky but front end templates are limited, fix this when possible
                $output = '<ul>'.$output.'</ul>';
}
return $output;

wanted maybe limit within ul type to 10 categories at most

Complete code:

<?php

CLASS ISC_DEPARTAMENTOS_PANEL extends PANEL
{
    public function SetPanelSettings()
    {
        if (ISC_CATEGORY::areCategoryFlyoutsEnabled()) {
            $GLOBALS['SideCategoryListTypeClass'] = 'SideCategoryListFlyout';
            $output = $this->_generateFlyoutOutput();
        } else {
            $GLOBALS['SideCategoryListTypeClass'] = 'SideCategoryListClassic';
            $output = $this->_generateClassicOutput();
        }

        if (!$output) {
            $this->DontDisplay = true;
            return;
        }

    $GLOBALS['SNIPPETS']['departamentos'] = $output;
}

/**
* get the html for sub category list
*
* @param array $categories the array of all categories in a tree structure
* @param int $parentCatId the parent category ID of the sub category list
*
* return string the html of the sub category list
*/
protected function _getSubCategory($categories, $parentCatId)
{

    $output = '';
    //if there is sub category for this parent cat
    if (isset($categories[$parentCatId]) && !empty($categories[$parentCatId])) {
        $i=1;
        foreach ($categories[$parentCatId] as $subCat) {
            // If we don't have permission to view this category then skip
            if (!CustomerGroupHasAccessToCategory($subCat['categoryid'])) {
                continue;
            }
            $catLink = CatLink($subCat['categoryid'], $subCat['catname'], false);
            $catName = isc_html_escape($subCat['catname']);

            $GLOBALS['SubCategoryList'] = $this->_getSubCategory($categories, $subCat['categoryid']);

            //set the class for the last category of its parent category
            $GLOBALS['LastChildClass']='';
            if($i == count($categories[$parentCatId])) {
                $GLOBALS['LastChildClass']='LastChild';
            }
            $i++;

            $GLOBALS['CategoryName'] = $catName;
            $GLOBALS['CategoryLink'] = $catLink;
            $output .= $GLOBALS['ISC_CLASS_TEMPLATE']->GetSnippet("SideCategoryList");
        }
    }
    if ($output!='') {
        // @todo ul here is hacky but front end templates are limited, fix this when possible
        $output = '<ul>'.$output.'</ul>';
    }
    return $output;
}

/**
 * This method creates and returns front-end output for original, non-flyout-enabled category menus
 *
 * @return string
 */
protected function _generateClassicOutput ()
{
    $output = "";
    $categories = $GLOBALS['ISC_CLASS_DATA_STORE']->Read('RootCategories');

    if (!isset($categories[0])) {
        return $output;
    }

    foreach($categories[0] as $rootCat) {
        // If we don't have permission to view this category then skip
        if(!CustomerGroupHasAccessToCategory($rootCat['categoryid'])) {
            continue;
        }

        $GLOBALS['SubCategoryList'] = $this->_getSubCategory($categories, $rootCat['categoryid']);
        $GLOBALS['LastChildClass']='';
        $GLOBALS['CategoryName'] = isc_html_escape($rootCat['catname']);
        $GLOBALS['CategoryLink'] = CatLink($rootCat['categoryid'], $rootCat['catname'], true);
        // @todo ul here is hacky but front end templates are limited, fix this when possible
        $output .= $GLOBALS['ISC_CLASS_TEMPLATE']->GetSnippet("SideCategoryList");
    }

    return $output;
}

/**
 * This method creates and returns front-end output for flyout-enabled category menus
 *
 * @return string
 */
protected function _generateFlyoutOutput ()
{
    $categories = new ISC_SITEMAP_MODEL_CATEGORIES;
    $categories->setMaximumDepth((int)GetConfig('CategoryListDepth') - 1);
    $categories = $categories->getTree();

    $renderer = new Store_SiteMap_Renderer;
    return $renderer->setSiteMapTree($categories)
        ->setRootClasses('sf-menu sf-vertical')
        ->render();
}
}
  • 2

    If it comes from the bank, could use LIMIT 10 in the query, no?

  • The ideal is already limit in the same bank. If you want to limit in php itself you will have to go through the data of the array and create an auxiliary variable that goes from 1 to 10. And when the auxiliary variable is 11 out of the loop.

1 answer

0

If you don’t want to limit in the bank (which is ideal), you can do with the.

$lista = '';
for($i = 0;$i<=10;$i++){
     $lista .= '<ul>'.$output.'</ul>';
}

return $lista;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.