Garreth Skrevet 19. februar 2007 Del Skrevet 19. februar 2007 (endret) Hei. Har et cms som jeg henter ut kategorier fra. Klarer fint å hente ut alle, men jeg har lyst å skrive ut litt anerledes linker på de kategorierene som har underkategorier. Har lyst at htmlen skal se slik ut: Klikk for å se/fjerne innholdet nedenfor <ul id="venstremeny"> <li><a href="category.php?categoryID=12">Klær</a></li> <li class="toggler frukt"><a href="#frukt">Frukt</a> <ul class="accordion"> <li><a href="category.php?categoryID=14#frukt">Eple</a></li> <li><a href="category.php?categoryID=15#frukt">Pære</a></li> <li class="current"><a href="category.php?categoryID=16#frukt">Appelsin</a></li> </ul></li> <li class="toggler frukt"><a href="#Gronnsaker">Grønnsaker</a> <ul class="accordion"> <li><a href="category.php?categoryID=23#Gronnsaker">Potet</a></li> <li><a href="category.php?categoryID=24#Gronnsaker">Gullerot</a></li> <li><a href="category.php?categoryID=25#Gronnsaker">Agurk</a></li> </ul></li> <li class="toggler Biler"><a href="#Biler">Biler</a> <ul class="accordion"> <li><a href="category.php?categoryID=28#Biler">Opel</a></li> <li><a href="category.php?categoryID=29#Biler">Audi</a></li> </ul></li> <li><a href="category.php?categoryID=30">Sport</a></li> <li><a href="category.php?categoryID=31">Nyheter</a></li> <li><a href="category.php?categoryID=32">Kurs</a></li> </ul> Her er phpscriptet jeg prøver å modifisere til mitt bruk: Klikk for å se/fjerne innholdet nedenfor PHP $menuRootCid = $SITE->getSiteConfig('leftmenuroot', $SITE->getActiveLanguage()); // Get required categories from site, check that they are valid $runtimeCategory = $SITE->getRunTimeValue('category'); $menuRootCategory = $SITE->getCategoryById($menuRootCid); if(!is_a($menuRootCategory, 'Category') || !is_a($runtimeCategory, 'Category')) { // Menuroot or runtimeCategory is not valid, aborting return; } // Find the category IDs in backtrace, store in map. // The map will be used to find the active categories in menu $backtrace = array(); do { $backtrace[$runtimeCategory->getCategoryId()] = true; $runtimeCategory = $SITE->getCategoryById($runtimeCategory->getParentId()); } while(is_a($runtimeCategory, 'Category') && $runtimeCategory->getIndexedLevel() > $menuRootCategory->getIndexedLevel()); // Get rootlevel categories, check that there actually are any. $rootLevelCategories = $SITE->getCategoriesByParentID($menuRootCategory->getCategoryId()); if(!is_array($rootLevelCategories) || count($rootLevelCategories) == 0) { // There are no categories in below the topmenuroot, nothing to do.. return; } /** * Recusive method used to print the menu. * * @param Array $categories */ function recursivePrintLevel($categories, $backtrace, $level) { global $SITE; // Check provided category array, return if not valid if(!is_array($categories) || count($categories) == 0) { return; } $cssHtml = ''; if($level == 0) { $cssHtml = ' id="venstremeny"';//Top <ul> }elseif($level == 1) { $cssHtml = ' class="accordion"';//sub <ul>'s } echo "\n<ul".$cssHtml.">\n"; for($i=0; $i<count($categories); $i++) { $cssHtml = ''; $CategoryURL = $categories[$i]->getCategoryHtmlFile(true); if(isset($backtrace[$categories[$i]->getCategoryId()])) { $cssHtml = ' class="current"'; } //Cut down the categoryname word to one, so the cat name can be used for class $txtarray = explode(" ", $categories[$i]->getName()); $antallord = count($txtarray); $kutt = 1; if ($antallord > $kutt) { $txtarray = array_slice($txtarray, 0, $kutt); } $shorttext = implode(" ", $txtarray); //Replace letters $Text = $shorttext; $Replace = array("æ","ø","å","è"); $ReplaceWith = array("ae","o","aa","e"); $Filtered = str_replace($Replace, $ReplaceWith, $Text); if(is_array($subCategories) && count($subCategories) > 0){ //If the maincategory has children echo "\t<li class=\"toggler $Filtered\" title=\"".$categories[$i]->getName()."\"><a href=\"#$Filtered\">".$categories[$i]->getName()."</a>"; }else{// Used for maincategories without children, and for children links echo "\t<li".$cssHtml." title=\"".$categories[$i]->getName()."\"><a href=\"".$categories[$i]->getCategoryHtmlFile(true)."\">".$categories[$i]->getName()."</a>"; } $subCategories = $SITE->getCategoriesByParentID($categories[$i]->getCategoryId()); if(is_array($subCategories) && count($subCategories) > 0) { recursivePrintLevel($subCategories, $backtrace, $level+1); } echo "</li>\n"; } echo "\n</ul>\n"; } recursivePrintLevel($rootLevelCategories, $backtrace, 0); Jeg trenger altså hjelp til å skrive ut 3 typer linker. Hovedkategorier uten noen underkategorier Hovedkategorier med underkategorier Underkategorier Jeg har prøvd å lage en ifsjekk med denne koden Klikk for å se/fjerne innholdet nedenfor PHP $menuRootCid = $SITE->getSiteConfig('leftmenuroot', $SITE->getActiveLanguage()); // Get required categories from site, check that they are valid $runtimeCategory = $SITE->getRunTimeValue('category'); $menuRootCategory = $SITE->getCategoryById($menuRootCid); if(!is_a($menuRootCategory, 'Category') || !is_a($runtimeCategory, 'Category')) { // Menuroot or runtimeCategory is not valid, aborting return; } // Find the category IDs in backtrace, store in map. // The map will be used to find the active categories in menu $backtrace = array(); do { $backtrace[$runtimeCategory->getCategoryId()] = true; $runtimeCategory = $SITE->getCategoryById($runtimeCategory->getParentId()); } while(is_a($runtimeCategory, 'Category') && $runtimeCategory->getIndexedLevel() > $menuRootCategory->getIndexedLevel()); // Get rootlevel categories, check that there actually are any. $rootLevelCategories = $SITE->getCategoriesByParentID($menuRootCategory->getCategoryId()); if(!is_array($rootLevelCategories) || count($rootLevelCategories) == 0) { // There are no categories in below the topmenuroot, nothing to do.. return; } /** * Recusive method used to print the menu. * * @param Array $categories */ function recursivePrintLevel($categories, $backtrace, $level) { global $SITE; // Check provided category array, return if not valid if(!is_array($categories) || count($categories) == 0) { return; } $cssHtml = ''; if($level == 0) { $cssHtml = ' id="venstremeny"';//Top <ul> }elseif($level == 1) { $cssHtml = ' class="accordion"';//sub <ul>'s } echo "\n<ul".$cssHtml.">\n"; for($i=0; $i<count($categories); $i++) { $cssHtml = ''; $CategoryURL = $categories[$i]->getCategoryHtmlFile(true); if(isset($backtrace[$categories[$i]->getCategoryId()])) { $cssHtml = ' class="current"'; } //Cut down the categoryname word to one, so the cat name can be used for class $txtarray = explode(" ", $categories[$i]->getName()); $antallord = count($txtarray); $kutt = 1; if ($antallord > $kutt) { $txtarray = array_slice($txtarray, 0, $kutt); } $shorttext = implode(" ", $txtarray); //Replace letters $Text = $shorttext; $Replace = array("æ","ø","å","è"); $ReplaceWith = array("ae","o","aa","e"); $Filtered = str_replace($Replace, $ReplaceWith, $Text); if(is_array($subCategories) && count($subCategories) > 0){ //If the maincategory has children echo "\t<li class=\"toggler $Filtered\" title=\"".$categories[$i]->getName()."\"><a href=\"#$Filtered\">".$categories[$i]->getName()."</a>"; }else{// Used for maincategories without children, and for children links echo "\t<li".$cssHtml." title=\"".$categories[$i]->getName()."\"><a href=\"".$categories[$i]->getCategoryHtmlFile(true)."\">".$categories[$i]->getName()."</a>"; } $subCategories = $SITE->getCategoriesByParentID($categories[$i]->getCategoryId()); if(is_array($subCategories) && count($subCategories) > 0) { recursivePrintLevel($subCategories, $backtrace, $level+1); } echo "</li>\n"; } echo "\n</ul>\n"; } recursivePrintLevel($rootLevelCategories, $backtrace, 0); Men utskrivingen av menyen virker helt random hvilke linker som får hvilken kode. 1 hovedkategori uten underkategorier får plutselig koden som er ment for hovekategorier med underkategorier, så jeg er ganske sikker på at det ikke er den riktige måten å skrive scriptet på. Endret 19. februar 2007 av Garreth Lenke til kommentar
Garreth Skrevet 19. februar 2007 Forfatter Del Skrevet 19. februar 2007 Har jobbet litt mer det scriptet, og jeg har nesten klart å løse det selv, men jeg skulle gjerne hatt hjelp til å finne ut navnet på hovedkategorien de ulike undermenyene tilhører. Klikk for å se/fjerne innholdet nedenfor PHP //If the maincategory has children if(is_array($subCategories) && count($subCategories) > 0){ //Print the links to the maincategory with children echo "\t<li class=\"toggler $Filtered\" title=\"".$categories[$i]->getName()."\"><a href=\"#$Filtered\">".$categories[$i]->getName()."</a>"; // Get the children of the maincategory and print links to them recursivePrintLevel($subCategories, $backtrace, $level+1); }// Endif maincategory has children // If the category don't have any children, print out normal links else{ //Need to find the parent name !! $parentname = $categories[$i]->getName(); echo "\t<li".$cssHtml." title=\"".$categories[$i]->getName()."\"><a href=\"".$categories[$i]->getCategoryHtmlFile(true)."#$parentname\">".$categories[$i]->getName()."</a>"; } Nå ser htmlen min ut som dette: Klikk for å se/fjerne innholdet nedenfor <ul id="venstremeny"> <li><a href="category.php?categoryID=12#Klaer">Klær</a></li> <li class="toggler Frukt"><a href="#Frukt">Frukt</a> <ul class="accordion"> <li><a href="category.php?categoryID=14#Eple">Eple</a></li> <li><a href="category.php?categoryID=15#Paere">Pære</a></li> <li><a href="category.php?categoryID=16#Appelsin">Appelsin</a></li> </ul> </li> <li class="toggler Gronnsaker"><a href="#Gronnsaker">Grønnsaker</a> <ul class="accordion"> <li><a href="category.php?categoryID=23#Potet">Potet</a></li> <li><a href="category.php?categoryID=24#Gullerot">Gullerot</a></li> <li><a href="category.php?categoryID=26#Agurk">Agurk</a></li> </ul> </li> <li class="toggler Biler"><a href="#Biler">Biler</a> <ul class="accordion"> <li><a href="category.php?categoryID=28#Opel">Opel</a></li> <li><a href="category.php?categoryID=29#Audi">Audi</a></li> </ul> </li> <li><a href="category.php?categoryID=30#Sport">Sport</a></li> <li><a href="category.php?categoryID=31#Nyheter">Nyheter</a></li> <li><a href="category.php?categoryID=32#Kurs">Kurs</a></li> </ul> Som dere ser så printes nå f.eks. Opel linken #Opel istedet for #Biler som er hovedkategorien. Jeg vil på en måte gå opp en menylevel og hente navnet, men jeg aner ikke hvordan jeg skal få til. Noen som har peiling? Lenke til kommentar
Anbefalte innlegg
Opprett en konto eller logg inn for å kommentere
Du må være et medlem for å kunne skrive en kommentar
Opprett konto
Det er enkelt å melde seg inn for å starte en ny konto!
Start en kontoLogg inn
Har du allerede en konto? Logg inn her.
Logg inn nå