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-10 of 11 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");

Flash 8 AS2 REMOTING with AMFPHP

This code should be used to perform remoting with Flash 8 using ActionScript 2.

// remoting
import mx.remoting.Service;
import mx.remoting.PendingCall;
import mx.rpc.RelayResponder;
import mx.remoting.debug.NetDebug;

NetDebug.initialize();

var myResponder:RelayResponder = new RelayResponder(this,"say_Result","say_Fault");
var siteCFC:Service = new Service("http://127.0.0.1/flashservices/gateway.php",null,"HelloWorld",null,myResponder);
var temp_pc:PendingCall = siteCFC.say("Hola chicos!");

function say_Result(re){
	trace(re.result);
}
function say_Fault(result){
	trace("error!");
}

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

Clone dev DB schema to test DB in Rails

Ruby on Rails

Command to clone the 'dev' DB to 'test' DB
rake clone_structure_to_test

*note* clones the structure/schema but NOT the content in the tables.

Center HTML content horizontally using CSS

Ahh the infamous CSS centering thing... make sure to specify the width and set the right/left margins to 'auto'

#content {
        margin: 0 auto;
        width: 740px;
}


Your HTML would look something like this:
<div id="content">
        This is centered
</div>

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"

JavaScript automagically hide and show an HTML tag (DIV, P, whatever)

If you want to hide or show an HTML tag using JavaScript, you can use the function below. It also changes the name of the link.

Here's the HTML code:
<a href="javascript:viewMore('two');" id="xtwo">... more</a>
<p>I see one.</p>
<p id="two" style="display:none">I see two.</p>


Here's the JavaScript code:
function viewMore(div) {
	obj = document.getElementById(div);
	col = document.getElementById("x" + div);
	
	if (obj.style.display == "none") {
		obj.style.display = "block";
		col.innerHTML = "... less";
	} else {
		obj.style.display = "none";
		col.innerHTML = "... more";
	}
}

HABTM relationship MySQL tables for use in Rails

This is an example of the tables I create when I want to have an HABTM relationship between 2 tables:
Table 1: products
Table 2: tags

DROP TABLE IF EXISTS `products_tags`;
DROP TABLE IF EXISTS `tags`;
DROP TABLE IF EXISTS `products`;

CREATE TABLE `products` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(100) NOT NULL,
  `price` decimal(10,2) NOT NULL,
  PRIMARY KEY  (`id`)
)

CREATE TABLE `tags` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(64) NOT NULL,
  PRIMARY KEY  (`id`)
)

CREATE TABLE `products_tags` (
  `product_id` int(11) NOT NULL default '0',
  `tag_id` int(11) NOT NULL default '0',
  PRIMARY KEY  (`product_id`,`tag_id`)
)


« Newer Snippets
Older Snippets »
Showing 1-10 of 11 total  RSS