project pages:  
PHPCoreStdfuncs PDOAccess CoreTable CoreGroup CoreForm CustomPrint
PHPCoreStdfuncs PDOAccess CoreTable CoreGroup
Personal Projects:
MyQuotes - Ihr persönlicher Aktienrechner
 

PHPCoreStdfuncs - A useful Library for common programming tasks

The File stdfuncs.inc.php contains a bunch of commonly used functions. I designed it firstly to my own needs as PHP-Developer. The functions are grouped in some classes just to keep a bit of order and structure in it. The most important classes are:

Table of Contents

Sourceforge

Sourceforge Projectsite & Download

Credits

Corvent Ltd. is our company which made the development of this library possible

Requirements

PHP 5

top  

Debugging functions

 

Inspecting variables

The most important debugging function is "debug::show()"

$myVar = array("string" => "myString", "empty" => "", "number" => 90, "null" => null);
$myVar["sub_array"] = array("string" => "myString", "empty" => "", "number" => 90, "null" => null);
$myVar["sub_array_2"] = array("myString", "", 90, null);
$myVar["sub_array_3"] = array();

debug::show($myVar, "[title]");

creates something like this (probably more usable than the output of print_r..)

[title]
string myString
empty ""
number 90
null null
sub_array string myString empty "" number 90 null null
sub_array_2 0 myString 1 "" 2 90 3 null
sub_array_3 empty-array

The Second Argument is useful if you debug more than one variable, to seperate them from each other.

The command debug::get() works equally but returns the html-code instead of direcly printing it to the standard-output, in fact debug::show(...) is a shortcut from print(debug::get(...))

top  

Time measurement

function a_sample_func($ms) {
        debug::startMessung("a_sample_func");
        usleep($ms * 1000);
        debug::stopMessung("a_sample_func");
}

debug::startMessung("overall_time");

a_sample_func(100);
a_sample_func(200);

usleep(100 * 1000);

debug::stopMessung("overall_time");
debug::showLaufzeit();

Will produce something like this:

Time measurements in ms
overall_time
Calls 1
a_sample_func Calls 2 time 300.29392242432
Difference 100.22616386414
TOTAL 400.52008628845
top  

Array Functions

The class "arrayfunc" contains functions to group, sort or filter arrays
$tableData = array();
$tableData[] = array("name"=>"kern", "firstname"=>"mike");
$tableData[] = array("name"=>"hollenbeck", "firstname"=>"john");
$tableData[] = array("name"=>"schultz", "firstname"=>"roland");
$tableData[] = array("name"=>"kern", "firstname"=>"bill");

debug::show($tableData, "original");
original
0 name kern firstname mike
1 name hollenbeck firstname john
2 name schultz firstname roland
3 name kern firstname bill
top  

Sorting an array

Sorting by name ascending

$sorted = arrayfunc::tableSort($tableData, "name");
debug::show($sorted, "sorted by name");
sorted by name
0 name hollenbeck firstname john
1 name kern firstname mike
2 name kern firstname bill
3 name schultz firstname roland

Sorting by firstname descending, unsing the holdIndizes option

The 3. argument "DESC" indicates to sort reverse, the 4. argument is called holdIndizes, if set the key => record relation will stay the same (useful if the indizes are e.g. databese ids).

$sorted = arrayfunc::tableSort($tableData, "firstname", "DESC", true);
debug::show($sorted, "sorted by firstname");
sorted by firstname
2 name schultz firstname roland
0 name kern firstname mike
1 name hollenbeck firstname john
3 name kern firstname bill
top  

Grouping an array

The same array is used as in tableSort

Grouping by name

$grouped = arrayfunc::tableGroup($tableData, "name");
debug::show($grouped, "grouped by name");
grouped by name
kern
0 name kern firstname mike
1 name kern firstname bill
hollenbeck
0 name hollenbeck firstname john
schultz
0 name schultz firstname roland

...and the same using the holdIndizes option

$grouped = arrayfunc::tableGroup($tableData, "name", true);
debug::show($grouped, "grouped by name with \"holdIndizes\"");
grouped by name with "holdIndizes"
kern
0 name kern firstname mike
3 name kern firstname bill
hollenbeck
1 name hollenbeck firstname john
schultz
2 name schultz firstname roland
top  

Grouping an array assuming unique columns

The same array is used as in tableSort

Grouping by the unique column firstname

$grouped = arrayfunc::uniqueTableGroup($tableData, "firstname");
debug::show($grouped, "grouped by firstname");
grouped by firstname
mike name kern firstname mike
john name hollenbeck firstname john
roland name schultz firstname roland
bill name kern firstname bill

... and the same but using just the name in the result

$grouped = arrayfunc::uniqueTableGroup($tableData, "firstname", "name");
debug::show($grouped, "grouped by firstname");
grouped by firstname
mike kern
john hollenbeck
roland schultz
bill kern

Grouping by the non-unique column name

$grouped = arrayfunc::uniqueTableGroup($tableData, "name");
debug::show($grouped, "grouped by name");
grouped by name
kern name kern firstname bill
hollenbeck name hollenbeck firstname john
schultz name schultz firstname roland
Note the loss of data because name is not unique.

You may use this effect to create an array of all possible name-values in your data:

$grouped = arrayfunc::uniqueTableGroup($tableData, "name", "name");
debug::show($grouped, "grouped by name");
grouped by name
kern kern
hollenbeck hollenbeck
schultz schultz
top  

Transformation Functions

The same array is used as in the sorting examples.

top  

Transforming a Value to php-Code

trans::php2phpCode("tableData", $tableData);

will return (of course without highlighting..):

$tableData = array();
$tableData[0] = array("name" => "kern", "firstname" => "mike");
$tableData[1] = array("name" => "hollenbeck", "firstname" => "john");
$tableData[2] = array("name" => "schultz", "firstname" => "roland");
$tableData[3] = array("name" => "kern", "firstname" => "bill");
 
top  

Transforming a Value to JS-Code

trans::php2js("tableData", $tableData);

will return:

var tableData = [{"name":"kern","firstname":"mike"},{"name":"hollenbeck","firstname":"john"},{"name":"schultz","firstname":"roland"},{"name":"kern","firstname":"bill"}];