Jump to content
php.lv forumi

Ģenerē idiotiskus li un ul


Cibiņš

Recommended Posts

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 by Cibiņš
Link to comment
Share on other sites

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 by Cibiņš
Link to comment
Share on other sites

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 by Cibiņš
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...