php hack技巧文章翻译转载

以后将开始边学习和边翻译,边转一些o' relliy的《PHP HACKS》一书的文章。
这次讲的是在PHP里实现类似TAB VIEW的效果哦,的确麻烦些

<?php
 $tabs = array();

 function tabs_header()
 {
 ?>
 <style type="text/css">
 .tab {
  border-bottom: 1px solid black;
  text-align: center;
  font-family: arial, verdana;
   }
 .tab-active {
  border-left: 1px solid black;
  border-top: 1px solid black;
  border-right: 1px solid black;
  text-align: center;
  font-family: arial, verdana;
  font-weight: bold;
   }
 .tab-content {
  padding: 5px;
  border-left: 1px solid black;
  border-right: 1px solid black;
  border-bottom: 1px solid black;
   }
 </style>
 <?php
 }

 function tabs_start()
 { 
      ob_start();
 }

 function endtab()
 {
   global $tabs;

   $text = ob_get_clean();
      $tabs[ count( $tabs ) - 1 ][ 'text' ] = $text;

   ob_start();
 }

 function tab( $title )
 {
   global $tabs;

   if ( count( $tabs ) > 0 )
  endtab();
  $tabs []= array(
    title => $title,
    text => ""
  );
   }

   function tabs_end( )
   {
  global $tabs;

  endtab( );
  ob_end_clean( );

  $index = 0;
  if ( $_GET['tabindex'] )
   $index = $_GET['tabindex'];

   ?>
   <table width="100%" cellspacing="0" cellpadding="0">
   <tr>
   <?php
  $baseuri = $_SERVER['REQUEST_URI'];
  $baseuri = preg_replace( "//?.*$/", "", $baseuri );
  $curindex = 0;
  foreach( $tabs as $tab )
  {
     $class = "tab";
     if ( $index == $curindex )
     $class ="tab-active";
  ?>
  <td class="<?php echo($class); ?>">
  <a href="<?php echo( $baseuri."?tabindex=".$curindex ); ?>">
  <?php echo( $tab['title'] ); ?>
  </a>
  </td>
  <?php
   $curindex += 1;
   }
  ?>
  </tr>
  <tr><td class="tab-content" colspan="<?php echo( count( $tabs ) + 1 ); ?>">
  <?php echo( $tabs[$index ]['text'] ); ?>
  </td></tr>
  </table>
  <?php
  }
  ?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值