PHP嵌入html代码

WBOY
Release: 2016-06-23 14:14:32
Original
4754 people have browsed it

PHP HTML 无限分类

$link   = mysql_connect( '127.0.0.1', 'root', '520928' ) or die( mysql_error() );//连接数据库服务器
          mysql_select_db( 'test' );//选择数据库
          mysql_set_charset( 'utf8' );//设置字符编码
 
             $sql = "SELECT * FROM wxtree";//查询分类表
          $result = mysql_query( $sql );
            $tree = array();//初始化一个数组
          while( $row  = mysql_fetch_assoc( $result ) )
                 {                   
                     $tree[$row['fid']][$row['id']]  = $row['cat_name'];//创建分类数组                                  
                 }
            unset( $row );
 
          mysql_close( $link );//关闭数据库
  
        
echo '-------------------------------------------' . "\n";
 
//递归函数
  
function for_category( $arr = array(), $arr2 )
{
    
    echo "
    ";
        foreach ( $arr as $k => $v )
        {
            if ( isset( $arr2[$k] ) && is_array( $arr2[$k] ) )
            {       
                      echo "
  • $k $v" ;    
                for_category( $arr2[$k], $arr2 );            
                      echo "
  • ";          
            }
            else{
                echo "
  • $k $v
  • ";
            }
        }
        echo "
";

}
   for_category( $tree[0], $tree );
 
?>

这种改如何添加各种操作啊,while循环中的话可以添加,foreach如何添加

回复讨论(解决方案)

输出条目时上去不就行了?

怎么做啊,我刚学,while

while($arr = mysql_fetch_array($res)){?><tr><td ><a href="?ac=edit&type_id=<?php echo $arr['type_id'];?>">编辑</a>      <a href="?ac=del&type_id=<?php  echo $arr['type_id'];?>">删除</a></td></tr>
Copy after login

输出时怎么加?

你上面不是已经加了么?

那个是错误的啊,程序中用的是foreach循环,而不是while啊,while我也会

foreach($arr as $value){
echo "";
echo "
";
}
?>

foreach($arr as $value){
echo "
";
echo "
";
}
?>
上面那个打错了。
如果你assoc 出来是只有ID就直接加$value 否则,你应该知道如何做

 <?php$link   = mysql_connect( '127.0.0.1', 'root', '520928' ) or die( mysql_error() );//连接数据库服务器          mysql_select_db( 'test' );//选择数据库          mysql_set_charset( 'utf8' );//设置字符编码              $sql = "SELECT * FROM wxtree";//查询分类表          $result = mysql_query( $sql );            $tree = array();//初始化一个数组          while( $row  = mysql_fetch_assoc( $result ) )                 {                 	                      $tree[$row['fid']][$row['id']]  = $row['cat_name'];//创建分类数组                                                                   }          mysql_close( $link );//关闭数据库		          print_r($tree[02]);echo '-------------------------------------------' . "\n"; //递归函数  function for_category( $arr = array(), $arr2 ){	      echo "<ul>";    foreach ( $arr as $k => $v )    {          if ( isset( $arr2[$k] ) && is_array( $arr2[$k] ) )        {          	   echo "<li>$k $v  &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp编辑&nbsp&nbsp&nbsp 删除";             for_category( $arr2[$k], $arr2 );                        echo "</li>";                  }        else{             echo " <li>$k $v &nbsp&nbsp&nbsp<a href="space.php?do=mtag&view=hot"> <span>热门群组ddd</span> </a> &nbsp&nbsp&nbsp编辑&nbsp&nbsp&nbsp 删除 </li>" ;        }                  }     echo "</ul>";  }   for_category( $tree[0], $tree );?> <?php$link   = mysql_connect( '127.0.0.1', 'root', '520928' ) or die( mysql_error() );//连接数据库服务器          mysql_select_db( 'test' );//选择数据库          mysql_set_charset( 'utf8' );//设置字符编码              $sql = "SELECT * FROM wxtree";//查询分类表          $result = mysql_query( $sql );            $tree = array();//初始化一个数组          while( $row  = mysql_fetch_assoc( $result ) )                 {                 	                      $tree[$row['fid']][$row['id']]  = $row['cat_name'];//创建分类数组                                                                   }          mysql_close( $link );//关闭数据库		          print_r($tree[02]);echo '-------------------------------------------' . "\n"; //递归函数  function for_category( $arr = array(), $arr2 ){	      echo "<ul>";    foreach ( $arr as $k => $v )    {          if ( isset( $arr2[$k] ) && is_array( $arr2[$k] ) )        {          	   echo "<li>$k $v  &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp编辑&nbsp&nbsp&nbsp 删除";             for_category( $arr2[$k], $arr2 );                        echo "</li>";                  }        else{             echo " <li>$k $v &nbsp&nbsp&nbsp<a href="space.php?do=mtag&view=hot"> <span>热门群组ddd</span> </a> &nbsp&nbsp&nbsp编辑&nbsp&nbsp&nbsp 删除 </li>" ;        }                  }     echo "</ul>";  }   for_category( $tree[0], $tree );?>
Copy after login

//真正的效果是这样的,做一个无限级分类,采用递归调用的方法,这里的

  • 之间无法加入链接啊

    echo " 

  • $k $v     热门群组ddd     编辑    删除 
  • " ;
    双引号中的双引号要转义!
    echo " 
  • $k $v     热门群组ddd     编辑    删除 
  • " ;
    Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template