Location: PHPKode > scripts > Stack > stack-2/stack2.class.php
<?php
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Original Author of file: Carey R. Dayrit <hide@address.com>
// Version 1.0
// ----------------------------------------------------------------------

/**
* A stack data structure. A stack is a LIFO (Last In First Out) data structure.
* This class uses a 2 dimensional array.
*
*Usage Example:
* $my_record=new STACK2('id','name');
* $my_record->push(235,'John');Insert to the top of stack 
* $my_record->push(536,'Paul');
* $my_record->push(758,'Mary');
* 
* $g=$my_record->pop(); Remove object from top of stack 
* $size=$my_record->size();Returns number of rows
* $my_record->is_empty(); Checks to see if size is zero
* $g=$my_record->top();Look at object at top of stack, but don't remove 
**/



class STACK2{
/*
Generic methods
stack::is_empty() 
stack::push() 
stack::size() 
stack::top() 
stack::pop();
*/
 var $record;//The Stack data
 var $colNames;//array defining column names
 var $numCols;//number of columns
 var $curStack;//Current Stack
 var $nextStack;




 //deconstructor for PHP 4 use register_shutdown_function()
 
 //arguments will define the columns of the record
 //columns must use associate arrays
 
 function STACK2(){
  $this->curStack=-1;
  $this->nextStack=0;
  //get the number of arguments
  $this->numCols = func_num_args();
  $this->colNames=func_get_args();
  $found_err=0;
   
  foreach($this->colNames as $k){
   if(!is_string($k)){
    $found_err=1;
   }
  }
  $err_message='RECORD_STACK() argument must be string of data type.';
   if($found_err){
    return $this->err($err_message);
  }
 }
 

 function push(){
  //push data to the record assuming return error message if arguments are more than the specified column names
  //get the number of arguments
  $numargs = func_num_args();
  //echo $this->nextStack;
  if($numargs!=$this->numCols){
   $err_message='The number of columns does not match';
   return $this->err($err_message);
  }else{
   $arg_list = func_get_args();
   //make the colNames as que
   //if there is a better option please do tell me
   $name=$this->colNames;
   foreach($arg_list as $k){
     $d=array_shift($name);
	 $this->record[$this->nextStack][$d]=$k;
    }	
   $this->curStack=$this->nextStack;
   $this->nextStack++;
   }
 }
 
 function pop(){
  //check if the stack is empty then pop
  //echo $this->is_empty();
  if($this->is_empty()){
   $this->curStack=0;
   $err_message='Cannot go further stack is empty';
   return $this->err($err_message);
  }else{
   //save the record
   $return=$this->record[$this->curStack];
   //destroy pop array
  // $this->record[$this->curStack]=array();
   $pig=$this->colNames;
   foreach($pig as $d){
    array_pop($this->record[$this->curStack]);
   }
   $this->nextStack--;
   $this->curStack--;
   //decrement the pointer;
   return $return;
  }
 }
 
 function size(){
  return $this->curStack+1;
 }
 
 function top(){
  if(!$this->is_empty()){
   return $this->record[$this->curStack];
  }else{
   $err_message='Stack is empty';
   return $this->err($err_message);
  }
 }
 
 function is_empty(){
 //get first column name
 $dummy=$this->colNames;
 //echo $this->record[$this->curStack][array_pop($dummy)];
 if( $this->curStack == -1 && $this->record[$this->curStack][array_pop($dummy)]==''){
  return 1;
  }else{
  return 0;
  }
 }
 
 function err($message){
  echo "<B>ERROR:</b>".$message;
  exit;
 }
}
?>
Return current item: Stack