Notes & Code Snippets

Read notes about design & development, or useful scripts and bits of code from me

Useful functions for generating INSERT and UPDATE

December 10, 2008

Keywords: mysql sql insert update query php

// Build an INSERT query by passing an array //
function buildInsert($array, $where){
	$query .= "INSERT INTO ".$where." (";
	$counter = 0;
	foreach($array as $k=>$v){
		if($counter == 0){
			$counter  ;
			$query .= '`'.$k.'`';
		}
		else{
			$query .= ', '.'`'.$k.'`';
		}
	}
	$query.= ") VALUES(";
	$counter = 0;
	foreach($array as $k=>$v){
		if(is_array($v)) $v = serialize($v);
		if($counter == 0){
			$counter  ;
			$query .= "'".addslashes($v)."'";
		}
		else{
			$query .= ", '".addslashes($v)."'";
		}
	}
	$query.= ")";
	return $query;
}
// Build an UPDATE query by passing an array //
function buildUpdate($array, $where, $extra=''){
	$query .= "UPDATE ".$where." SET ";
	$counter = 0;
	foreach($array as $k=>$v){
		if(is_array($v)) $v = serialize($v);
		if($counter == 0){
			$counter  ;
			$query .= "`".$k."` = '".addslashes($v)."'";
		}
		else{
			$query .= ", "."`".$k."` = '".addslashes($v)."' ";
		}
	}reset($array);
	$query.= $extra;
	return $query;
}

Back to top

Display results of an SQL query

December 10, 2008

Keywords: php database sql mysql phpmyadmin table

// This function is useful when you just want to find data without
// Loading PHPMyAdmin
function displaySQL($query){
	$data = array();
	$link = dbOpen();
	$result = mysql_db_query('nasorg_858', $query);
	while($row = mysql_fetch_assoc($result)) {
		array_push($data, $row);
	}
	echo '<table cellspacing="2" cellpadding="2" class="sqlResult">
<thead>
<tr>
';
	foreach($data[0] as $k=>$v){
		echo '	<td>'.$k.'</td>
';
	}
	echo '</tr>
</thead>
';
	foreach($data as $k=>$v){
		echo '<tr>';
		foreach($v as $rk=>$rv){
			echo '	<td>'.$rv.'</td>
';
		}
		echo '</tr>
';
	}
	echo '</table>';
	dbClose($link);
}

Back to top

Actionscript Gradual Slider

December 10, 2008

Keywords: actionscript action script object slide move

// This is an incredibly useful script that moves an MC to a target
// position from its current position.  It will slow down as it gets closer.
 
accel = 0.2;    // Set accel and speed higher to achieve a bouncing effect
speed = 0.1;  // But if they're set above 1.0, the object will disappear
y = 1;
x = 1;
 
// Target positions
// Note that either the x or y position scripts can be removed to
// only slide one direction
xpos = 50;
ypos = 50;
onEnterFrame = function(){
	y = y*accel   (ypos - box._y) * speed;
	box._y  = y;
	if (Math.abs(ypos - box._y) < 1) {
		box._y = ypos;
	}
 
	x = x*accel   (xpos - box._x) * speed;
	box._x  = x;
	if (Math.abs(xpos - box._x) < 1) {
		box._x = xpos;
	}
}
 
// The following code is only really for demonstration purposes
// It just moves the object with your mouse as you click or drag
onMouseDown = function(){
	xpos = _xmouse;
	ypos = _ymouse;
	onMouseMove = function(){
		xpos = _xmouse;
		ypos = _ymouse;
	}
}
onMouseUp = function(){
	onMouseMove = null;
}

Back to top

Flash Actionscript 2.0 Preload

December 10, 2008

Keywords: flash actionscript 2.0 preload preloader

// Since this preload script is meant to be part of a preloader MC,
// which can just be popped onto the main timeline with no additional
// coding, it's stopping the _parent.
_parent.stop();
onEnterFrame = function(){
	perc = Math.floor(_parent.getBytesLoaded() / (_parent.getBytesTotal() / 100));
	if(_parent.getBytesLoaded() == _parent.getBytesTotal()){
		_parent.play();
	}
	loading.text = "Loading... " perc "%";
	// This makes the loading circle rotate faster as it gets
	// closer to being finished
	spinner._rotation -= (perc / 1.5);
}

Back to top

1 2 3