Location: PHPKode > scripts > Tab Control > tab-control/tab_control.php
<?php
/**
*  control and make tabs Html
*  To learn how to use it, see the Example.php
*
* @package        tab_control
* @access        public
* @author        Rafia alotibi <hide@address.com>
* @License	      GPL
* @version       1.0
*/
class tab_control
{
    /**
    *    @var    array $tabs
    */
    var $tabs        = array();
    /**
    *    @var    array $style
    */
    var $style       = array();
    /**
    *    @var    integer $count
    */
    var $count       = 0;
    /**
    *    @var    integer $current
    */
    var $current     = 0;

    /** Constructor
    *  This is the initialization function,
    *
    *  that set dir parameters to default values
    *
    *  @access: public
    *  @param string $dir dir of table in OutPut
    *
    */
    function tab_control($dir='rtl')
    {

         $this->style  = array('width'    =>"96%",
                               'border'   =>"#D9D9D9",
                               'bgcolor'  =>"#FFFFFF",
                               'current'  =>"#F1F1F1",
                               'uncurrent'=>"#FFFFFF",
                               'dir'=>$dir,
                               );
    }

    /**
    *  function set_style, set table style
    *
    *  @access: public
    *  @param array $style style of table
    *
    */
    function set_style($style=array())
    {
         $this->style = array_merge ($this->style,$style);
    }

    /**
    *  function add_tab,add tab
    *
    *  @access: public
    *  @param integer $id  id
    *  @param string $url url
    *  @param string $title  title
    *  @param string $contents  contents
    *
    */
    function add_tab($id,$url,$title,$contents='')
    {
         $this->tabs[$id]  = array('url'=>$url,'title'=>$title,'contents'=>$contents);
    }

    /**
    *  function set_current,Use to set current tab,
    *
    *
    *  @access: public
    *  @param integer $id id of current tab
    *
    */
    function set_current($id)
    {
        $this->current = $id;
    }

    /**
    *  function get_tab_style,Use to get current tab style
    *
    *
    *  @access: private
    *  @param integer $id
    *  @return mixed $style of tab
    *
    */
    function get_tab_style($id)
    {
        if($id == $this->current)
        return "BORDER-RIGHT: {$this->style[border]} 1px solid;"
              ."PADDING-RIGHT: 15px;"
              ."BORDER-TOP: {$this->style[border]} 1px solid;"
              ."PADDING-LEFT: 15px;"
              ."BORDER-LEFT: {$this->style[border]} 1px solid;"
              ."WIDTH: 15%;"
              ."BORDER-BOTTOM: {$this->style[border]} 0px solid;"
              ."BACKGROUND-COLOR: {$this->style[current]}";
        else
        return "BORDER-RIGHT: {$this->style[border]} 1px solid;"
              ."PADDING-RIGHT: 15px;"
              ."BORDER-TOP: {$this->style[border]} 1px solid;"
              ."PADDING-LEFT: 15px;"
              ."BORDER-LEFT: {$this->style[border]} 1px solid;"
              ."WIDTH: 10%;"
              ."BORDER-BOTTOM: {$this->style[border]} 1px solid;"
              ."BACKGROUND-COLOR: {$this->style[uncurrent]}";
    }

    /**
    *  function get_space,Use to get space between tab
    *
    *  @access: private
    *  @param string $space dir of table in OutPut
    *  @return mixed $style of space between tab
    *
    */
    function get_space($space)
    {

        if($space == 'endSpace')
        return "BORDER-RIGHT: {$this->style[border]} 0px solid;"
              ."BORDER-TOP: {$this->style[border]} 0px solid;"
              ."BORDER-LEFT: {$this->style[border]} 0px solid;"
              ."BORDER-BOTTOM: {$this->style[border]} 1px solid;"
              ."BACKGROUND-COLOR: {$this->style[bgcolor]}";
        else
        return "BORDER-RIGHT: {$this->style[border]} 0px solid;"
              ."BORDER-TOP: COLOR: {$this->style[border]} 0px solid;"
              ."BORDER-LEFT: {$this->style[border]} 0px solid;"
              ."WIDTH: 1%;"
              ."BORDER-BOTTOM: {$this->style[border]} 1px solid;"
              ."BACKGROUND-COLOR: {$this->style[bgcolor]}";
    }

    /**
    *  function make_tabs,make tabs and return cells
    *
    *  @access: private
    *  @return mixed $cells output of tabs
    *
    */
    function make_tabs()
    {
         $this->count = count($this->tabs);
         if($this->count == 0) return ;
         $end = end($this->tabs);
         $i = 0;
         foreach($this->tabs as $tabs)
         {
             if( $end == $tabs )
             $style = $this->get_space('endSpace');
             else
             $style = $this->get_space('Space');

             $cells .= "<TD style=\"".$this->get_tab_style($i)."\">"
                     .  "<A href=\"$tabs[url]\" >$tabs[title]</A>"
                     .  "</TD><TD style=\"".$style."\">&nbsp;</TD>" ;
             $i++;
         }
         return $cells;
    }

    /**
    *  function get_out,
    *
    *  @access: public
    *  @param mixed $contents dir of table in OutPut
    *  @return mixed output of tabs
    *
    */
    function get_out($contents ='')
    {
        $tabs =  $this->make_tabs();
        $colSpan = $this->count*2;

        if($this->tabs[$this->current][contents])
        {
             $contents = $this->tabs[$this->current][contents];
        }

        return <<<EOF
<TABLE cellSpacing="0" cellPadding="0" width="{$this->style[width]}" border="0" dir="{$this->style[dir]}">
<TBODY>
<TR>
{$tabs}
<TR>
<TD style="BORDER-RIGHT: {$this->style[border]} 1px solid;
    BORDER-TOP: {$this->style[border]} 0px solid;
    BORDER-LEFT: {$this->style[border]} 1px solid;
    BORDER-BOTTOM: {$this->style[border]} 1px solid;
    BACKGROUND-COLOR:  {$this->style[current]}" colSpan="$colSpan">
<br>{$contents}
</TD>
</TR>
</TBODY>
</TABLE>
<br>
EOF;
}

}
?>
Return current item: Tab Control