Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Alex Williams http://www.alexwilliams.ca

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

PHP: Sorting an associative array

This basically sorts an associative array... somethin' like that.

$people = new Array();

$people[0]['id'] = 1;
$people[0]['name'] = "Dave";
$people[1]['id'] = 2;
$people[1]['name'] = "Alex";
$people[2]['id'] = 3;
$people[2]['name'] = "Chris";

function cmp($a, $b)
{
   return strcmp($a['name'], $b['name']);
}
usort($people, "cmp");

PHP variable check

ugh

$page = isset($_GET['page']) ? $_GET['page'] : 'home';

PHP: Dynamically call a function from list of allowed actions

This function simply calls another function based on the argument, assuming that argument ($action) is in the "allowed" list of actions.

settings-config.php
$settings['actions'] = "list,view,delete,update";


Catalog-class.php
public function process_action($action) {
	global $settings;
	
	$allowed_actions = explode(",",$settings['actions']);
	if (is_numeric(array_search($action, $allowed_actions))) {
		$f_name = "product_" . $action;
		$this->$f_name();
	} else {
		$this->error_msg = 'ACTION_NOT_ALLOWED';
	}
}
private function product_list() {
}
private function product_view() {
}
private function product_delete() {
}
private function product_update() {
}
private function product_create() {
}


Even if product_create() exists, if you do process_action('create'), it will not work... it's basically like a function wrapper.. or something like that...

UPDATE: Yes I know I could have created an array like
$settions['actions'] = array('list','view','delete','update');
but I've modified this code a bit for snippet/display purposes... anyways this is just a reminder for myself, not of actual use or interest for others... comments are welcome though :D

PHP: Connecting Flash to a Database (remoting)

There are many ways to do this.

Depending on the backend language you're using, whether PHP, ColdFusion or other... you'll need to create some components (CFC's for ColdFusion), (Classes for PHP)... these are referred to as "services"

Then you'll need to connect Flash to a remoting gateway (I use AMFPHP for PHP: www.amfphp.org).

Either way, you'll need NetServices.as from Flash MX Remoting, call a gateway and retrieving a list of functions from your classes/components/services.

Enough with the theory, here's how to do it with PHP:
/* /flashservices/services/Catalog.php */
class Catalog {
        var $products_array = array();

// Constructor: Contains the list of methods available to the gateway
function Catalog() {
	$this->methodTable = array (
		"getProducts" => array (
			"description" => "Get list of products",
			"access" => "remote",
			"arguments" => "" // arguments could be optional, not tested
		)
	); // end methodTable
}

function getProducts() {	
	// your code goes here

	return $this->products_array;
}
}


!!!!!!!!! The code below is now deprecated !!!!!!!!
---- see my other snippet to do this in Flash 8 ----

Flash ActionScript (PHP Gateway):
#include "NetServices.as"
NetServices.setDefaultGatewayUrl("http://yourserver.com/flashservices/gateway.php");
gw = NetServices.createGatewayConnection();
CatalogREMOTE = gw.getService("Catalog", this);
CatalogREMOTE.getProducts();

getProducts_Result = function(result) {
	_root.products_results = result;
}


You parse the _root.products_results array however you want :D

Load a PHP Class automatically upon instantiation (PHP5)

When you instantiate a class, it will try to load the file automatically.
function __autoload($class_name) {
    require_once("classes/" . $class_name . ".php");
}

Usage:
$auth = new Auth;

Will automatically load the file "classes/Auth.php"
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS