PHP code example of andyfleming / handy

1. Go to this page and download the library: Download andyfleming/handy library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andyfleming / handy example snippets



class Person extends HandyModel {
	const TABLE_NAME = 'people';
}


# ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
#	Setup
# ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

	// Setup database handle
	$dbh = new mysqli( $host, $user, $pass, $database );

	// Pass Handy the database handle	
	Handy::setDefaultDB($dbh);
	
	// Require class
	0) {
	    foreach ($people as $person) {
	    	echo $person->get('first_name').": ".$person->get('age');
	        echo "<br />";
	    }
	}
	

$db->query("INSERT INTO `people` SET `x`='y', `a`='b'");

$newPersonID = $db->insert_id;
$newPerson = $db->query("SELECT * FROM `people` WHERE `id` = '{$newPersonID}'");

$newPerson = $newPerson->fetch_object('Person');

$newPerson = Person::create(array(
	'x' => 'y',
	'a' => 'b'
));

class Person extends HandyModel {
	
	const TABLE_NAME = 'people';
	
}


Person::create(array(
	'first_name' => 'John',
	'last_name' => 'Smith'
);

$person = Person::lookupByID(12);

// Returns single Person object or false
if (!$person) {
	echo "Person 12 was not found!";
} else {
	echo "Person 12's first name is ".$person->get('first_name');
}


$person = Person::lookup("`first_name`='Bob'");

// Returns single Person object or false

$people = Person::lookupEach("age > 30");

// Returns array of Person objects by ID or empty array

if (count($people) == 0) {
	echo 'No one found!';
} else {
	foreach ($people as $person) {
		echo $person->get('first_name').": ".$person->get('age');
		echo "<br />";
	}
}

/*

Array people
	17 => Object Person
	27 => Object Person
	28 => Object Person

*/


$person = Person::lookupRandom("`age` > 30");

// Returns single (random) Person object or false