WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : phpCollection
Categories : PHP, PHP Classes, Object Oriented Click here to Update Your Picture
Hasin Hayder
Date : Mar 10th 2005
Grade : 4 of 5 (graded 6 times)
Viewed : 5343
File : 4097.zip
Images : No Images for this code example.
Search : More code by Hasin Hayder
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

This class implements collection object (dictionary object) like Java in PHP. It has a built in iterator. You can perform all abstract collection methods in it.


collection.php
<?php

/*******************************************************************************************
*
* Class Name : phpCollection
* Version    : 1.0
* Written By : Hasin Hayder
* Start Date : 10th March, 2005
* Release License : LGPL
*
********************************************************************************************
*
* This class implements Collection object in PHP.
*
*/

class phpCollection
{
    var
$length;
    var
$elements = array();
    var
$__cnt=0;
    var
$__temp;
   
    function
add($key, $item)
    {
            if (!
array_key_exists($key,$this->elements))
            {
               
$this->elements[$key] = $item;
               
$this->length = count($this->elements);
               
$this->__temp = array_values($this->elements);
                return
true;
            }
        return
false;
       
    }
   
    function
remove($key)
    {
       
$dummy = array();
        foreach(
$this->elements as $k=>$v)
        {
                if (
$k != $key)
                {
               
$dummy[$k] = $v;
                }
        }
       
$result = false;
        if (
count($this->elements)!=count($dummy)) $result = true;
       
$this->elements = $dummy;
       
$this->length = count($dummy);
       
$this->__temp = array_values($this->elements);
        return
$result;
    }
   
    function
key_exists($k)
    {
        foreach(
$this->elements as $k=>$v)
        {
                if (
$k == $key)
                {
                return
true;
                }
        }
        return
false;
    }
   
    function
has_next()
    {
        if((
$this->__cnt)<($this->length-1))
            {
           
$this->__cnt++;
            return
true;
               
            }
            else
            return
false;
    }
   
    function
rewind()
    {
           
//just set the iterator position to first
           
$this->__cnt = 0;
    }
   
    function
current()
    {
       
         return 
$this->__temp[$this->__cnt];
           
    }
   
    function
itemat($pos)
    {
        return
$this->__temp[$pos];
    }
   
    function
removeat($pos)
    {
           
$keys = array_keys($this->elements);
           
$curkey = $keys[$pos];
            return
$this->remove($curkey);
    }
   
    function
item($key)
    {
            return
$this->elements[$key];
    }
}
?>



Usage:
<?php
include_once("collection.php");
$col = new phpCollection();
$col2 = new phpCollection();

$col->add("a","123"); //add an object
$col->add("b","13"); //add an object
$col->add("c","134"); //add an object

$col2->add("ba","Ayesha"); //add an object
$col2->add("ca","Hasin"); //add an object

$col->add("d",$col2); //add col2 object to col collection

$col->rewind(); //set iterator position to 0

echo "<br>"."Item : ".$col->current(); //retrieve current element
while ($col->has_next()) //iterates
{
echo
"<br>"."Item : ".$col->current(); //retrieve current element
}


echo
"<hr>";

$ncol = $col->item("d"); //retrieve object
$ncol->rewind(); //use that object
echo $ncol->itemat(0);
?>



My Box - PHP Class that calculates the volumetric weight of a package.
Categories : PHP, Object Oriented, PHP Classes, Beginner Guides, Payment Gateways
Advanced Image WaterMarker
Categories : PHP, PHP Classes, GD image library, Graphics, Object Oriented
EasyPhpThumbnail Class - The EasyPhpThumbnail class allows you to generate thumbnails and handle image manipulation for GIF, JPG and PNG on-the-fly.
Categories : PHP, PHP Classes, Object Oriented, Graphics, GD image library
Access_user Class - an easy to use system for protecting pages and register users.
Categories : PHP, Classes and Objects, Object Oriented, PHP Classes, Authentication
Football News Aggregator
Categories : PHP, Object Oriented, PHP Classes, Rich Site Summary (RSS), HTML and PHP
SPL and ITERATOR : examples
Categories : PHP, Object Oriented, PHP Classes, Sessions
An updated OOP - Inheritance
Categories : PHP, PHP Classes, Object Oriented
Class to Create protected URLs
Categories : PHP, PHP Classes, URLs
Form is a utility class for generating html forms. It provides form initialization and regex based data validation (both server and client side) with a convenient interface. This version obsoletes version 1.0a
Categories : HTML, PHP, PHP Classes, Regexps
shopping cart class with add/edit/delete product functionality.
Categories : PHP, PHP Classes, Ecommerce
HTML_Graphs provides a simple PHP interface for creating pure HTML charts.
Categories : Graphics, PHP, PHP Classes, Charts and Graphs
very simple ftp class
Categories : PHP, PHP Classes, FTP
DBXML- A Class to backup databases in XML Format using web interface
Categories : PHP, PHP Classes, Databases, MySQL, XML
FormChecker Package - validate any data via classes and patterns.
Categories : PHP, Form Processing, PHP Classes, Regexps
Menu in sliding bar or tree style. Handles frames by using small amount of javascript. Handles external and internal pages. Allows custom code to replace a menu item.
Categories : PHP Classes, PHP, Java Script, DHTML