PHP Classes

File: collection.php

Recommend this page to a friend!
  Classes of Everton da Rosa   PHP Two Dimensional Array Collection   collection.php   Download  
File: collection.php
Role: Example script
Content type: text/plain
Description: Example of use
Class: PHP Two Dimensional Array Collection
Manage value collections in two dimensional arrays
Author: By
Last change:
Date: 9 years ago
Size: 3,187 bytes
 

Contents

Class file image Download
<?php

/*
 * Copyright (C) 2014 Everton
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * Example use of PTK\utils\Collection
 */

try{
    require
'examples.inc.php';
   
   
//creating a collection
   
$mycollection = new \Ptk\utils\Collection(array(
        array(
'name' => 'Homer', 'genre' => 'masc')
        ,array(
'name' => 'Marge', 'genre' => 'fem')
        ,array(
'name' => 'Lisa', 'genre' => 'fem')
        ,array(
'name' => 'Bart', 'genre' => 'masc')
        ,array(
'name' => 'Maggie', 'genre' => 'fem')
    ));
   
    echo
"The current record into collection is:".PHP_EOL;
   
var_dump($mycollection->getCurrent());
   
    echo
PHP_EOL."The next name is: ".$mycollection->getNext()['name'].PHP_EOL;
   
   
//advancing to the next record
   
$mycollection->goNext();
    echo
"The current row now is: ".$mycollection->getCurrent()['name'].PHP_EOL;
   
   
//back to the previous record
   
$mycollection->goPrevious();
    echo
"The previous record: ".$mycollection->getCurrent()['name'].PHP_EOL;
   
   
//going to end of collection
   
$mycollection->goLast();
    echo
"The last name is ".$mycollection->getCurrent()['name'].PHP_EOL;
   
   
//going to first record
   
$mycollection->goFirst();
    echo
"The first name is ".$mycollection->getCurrent()['name'].PHP_EOL;
   
   
//go to row #3
   
$mycollection->go(3);
    echo
"The #3 row is ".$mycollection->getCurrent()['name'].PHP_EOL;
   
   
//moving to next 2 positions
   
$mycollection->goFirst();
   
$mycollection->move(2);
    echo
"The name for 2 positions from first is ".$mycollection->getCurrent()['name'].PHP_EOL;
   
   
$mycollection->goLast();
    
//moving back 1 positions
   
$mycollection->move(-1);
    echo
"The name for 1 positions from last is ".$mycollection->getCurrent()['name'].PHP_EOL;
   
   
//removing elements
   
$mycollection->remove();
   
   
//adding elements
   
$mycollection->add(array('name' => 'Milhouse', 'genre' => 'fem'));
   
   
//editing element
   
$mycollection->goLast();
   
$mycollection->update(array('name' => 'Milhouse', 'genre' => 'masc'));
   
   
//sorting data
   
$mycollection->sort('genre', SORT_ASC, 'name', SORT_DESC);
    echo
'<pre>';
   
var_dump($mycollection->fetchArray());
    echo
'</pre>';
   
   
$collection = $mycollection->seek('[M*]', array('name'));
    echo
'<pre>';
   
var_dump($collection->fetchObject());
    echo
'</pre>';
} catch (
Exception $ex) {
    echo
$ex->getMessage();
    exit(
$ex->getCode());
}