Cibiņš Posted July 1, 2010 Report Share Posted July 1, 2010 (edited) Nu tātad lieta tāda ka itkā skripts kā tāds strādā. Skripta uzdevums ir izveidot menu no datubāzes ņemot vērā datubāzē norādījumus pēc parentiem. Tas viss ok, bet problēma ir tā ka ģenerē diezgan idiotisku li un ul tagu skriptu līdz ar to menu tiek sagrūsti vienā kaudzē nevis paklausot css norādījumiem. Kās varētu lūdzu palīdzēt tikt galā ar šo problēmu? Ģenerējošais skripts ir: <? $children = 0; $no_children = array(); function display_children($parent, $level) { global $no_children; global $children; $sql = sprintf("SELECT category_name, category_id, category_link FROM categories WHERE parent_id = %d", $parent); $result = mysql_query($sql); if($level > 0) echo '<ul>'; $list_id=''; while ($row = mysql_fetch_array($result)) { $children++; $list_id = 'list_'.$children; echo '<li><a href="#">'.$row['category_name'].'</a></li>'; display_children($row['category_id'], $level+1); } $child_product = display_products($parent); if($level > 0) echo "</ul>"; $no_children[] = 'list_'.$children; if($child_product) array_pop($no_children); } function display_products($parent) { $child_product = false; $sql = sprintf("SELECT product_name, product_id, product_link FROM products WHERE category_id = %d", $parent); $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { echo '<li><a href="#">'.$row['product_name'].'</a></li>'; $child_product = true; } return $child_product; } echo "<ul>"; display_children('',0); echo "</ul>"; ?> Iepriekšminētais skripts izģenerē šādu ul un li šmurgu <div> <ul> <li><a href="#">Sadala1</a></li> <ul> <li><a href="#">category1</a></li> <ul> <li><a href="#">category1-1</a></li> <ul></ul> <li><a href="#">category1-2</a></li> <ul></ul> </ul> <li><a href="#">category2</a></li> <ul></ul> <li><a href="#">category3</a></li> <ul> <li><a href="#">category3-1</a></li> <ul> <li><a href="#">Cat 3_1_1</a></li> <ul></ul> </ul> </ul> </ul> <li><a href="#">Sadala2</a></li> <ul></ul> </ul> </div> Bet pareizais skripts būtu <div> <ul> <li><a href="#">Sadala1</a> <ul> <li><a href="#">category1</a> <ul> <li><a href="#">category1-1</a></li> <li><a href="#">category1-2</a></li> </ul> </li> <li><a href="#">category2</a></li> <li><a href="#">category3</a> <ul> <li><a href="#">category3-1</a> <ul> <li><a href="#">Cat 3_1_1</a></li> </ul> </li> </ul> </li> </ul> </li> <li><a href="#">Sadala2</a></li> </ul> </div> Kā lai panāk ka tiktu ģenerēts pareizais skripts nevis tas šmurgs? Edited July 1, 2010 by Cibiņš Quote Link to comment Share on other sites More sharing options...
marcis Posted July 1, 2010 Report Share Posted July 1, 2010 what a free porn... http://webcache.googleusercontent.com/search?q=cache:O4fQ8w8N1lkJ:defektologs.lv/2008/12/01/rekursiva-lapas-karte/+site:defektologs.lv&cd=7&hl=lv&ct=clnk&gl=lv Quote Link to comment Share on other sites More sharing options...
bubu Posted July 1, 2010 Report Share Posted July 1, 2010 Debugot, debugot un vēlreiz debugot, nevis gaidīt, kad citi visu izdarīs tev priekšā. Quote Link to comment Share on other sites More sharing options...
Cibiņš Posted July 1, 2010 Author Report Share Posted July 1, 2010 Mārcim paldies!! :)) Quote Link to comment Share on other sites More sharing options...
Cibiņš Posted July 1, 2010 Author Report Share Posted July 1, 2010 (edited) Bet man nav saprotams kaa iisti izvilkt veel no viena papildus lauka 'pg' esoshos datus ieksh <a href="$kautkas"> ?? $s.='<li><a href="'.$row["pg"].'">'; function linkextract($pid,$arr){ if(!is_array($arr[$pid])){ return ""; } $s='<ul>'; foreach($arr[$pid] as $key=>$value){ $s.='<li><a href="'.$row["pg"].'">'; $s.=$value; $s.='</a>'; $s.=linkextract($key,$arr); $s.='</li>'; } $s.='</ul>'; return $s; } $links=array(); $sql="SELECT id, parent_id, menu_lv, menu_ru, pg FROM pg_pages"; $res=mysql_query($sql); while($row=mysql_fetch_object($res)){ if($lang == 'en'){ $links[$row->parent_id][$row->id]=$row->menu_ru; } else { $links[$row->parent_id][$row->id]=$row->menu_lv; } } echo linkextract(0,$links); Edited July 1, 2010 by Cibiņš Quote Link to comment Share on other sites More sharing options...
daGrevis Posted July 1, 2010 Report Share Posted July 1, 2010 Debugot! Piekrītu... Quote Link to comment Share on other sites More sharing options...
bubu Posted July 1, 2010 Report Share Posted July 1, 2010 Hints: masīva vērtības var būt arī masīvi, nevis tikai pliks strings. Quote Link to comment Share on other sites More sharing options...
Cibiņš Posted July 2, 2010 Author Report Share Posted July 2, 2010 (edited) Nu itkā panācu to ko vajag bet atkal jaunaas problēmas-ja ir vairāk par vienu apakšsadaļu piem Izvēlne 1 -> Izvēlne 1_1 Izvēlne 1 -> Izvēlne 1_2 Tad menu otrajam līmenim dublējas.....???? Es tā saprotu ka tas otrais "foreach" dublee to menu bet kaa lai panaak lai vienu reizi?? :@:@:@ <?php function linkextract1($pid,$arr1,$arr1_1){ if(!is_array($arr1[$pid])){ return ""; } $s1='<ul>'; foreach($arr1[$pid] as $key=>$value1){ foreach($arr1_1[$pid] as $key=>$hreflink1){ $s1.='<li><a href="?pg='.$hreflink1.'">'; $s1.=$value1; $s1.='</a>'; $s1.=linkextract1($key,$arr1,$arr1_1); $s1.='</li>'; }} $s1.='</ul>'; return $s1; } $links1||$pagenmb1=array(); $sql569="SELECT id, parent_id, menu_lv, menu_ru, pg FROM pg_pages WHERE menu_position='2' AND menucontent='1'"; $res1=mysql_query($sql569); while($row1=mysql_fetch_object($res1)){ $pagenmb1[$row1->parent_id][$row1->id]=$row1->pg; if($lang == 'en'){ $links1[$row1->parent_id][$row1->id]=$row1->menu_ru; } else { $links1[$row1->parent_id][$row1->id]=$row1->menu_lv; } } echo linkextract1(0,$links1,$pagenmb1); ?> Edited July 2, 2010 by Cibiņš Quote Link to comment Share on other sites More sharing options...
Cibiņš Posted July 2, 2010 Author Report Share Posted July 2, 2010 Nezin neviens??? Quote Link to comment Share on other sites More sharing options...
php newbie Posted July 3, 2010 Report Share Posted July 3, 2010 pēc pirmā acu uzmetiena tā funkcija ir rekursīva. tur varbūt vairākas apakšsadaļas. Palasi par rekursiju. Quote Link to comment Share on other sites More sharing options...
indoom Posted July 5, 2010 Report Share Posted July 5, 2010 iespējams tāpēc, ka ir divi foreach foreach($arr1[$pid] as $key=>$value1){ foreach($arr1_1[$pid] as $key=>$hreflink1){ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.