Quick way to INSERT an array into a table

This is a useful function that I wrote to insert an array into a table in your database.

<?php
function insert_array($table, $data) {
	$cols = '(';
	$values = '(';
	foreach ($data as $key=>$value) {
		$value = addslashes($value);
		$cols .= "$key,";
		$values .= "'$value',";
	}
	$cols = rtrim($cols, ',').')';
	$values = rtrim($values, ',').')';
	$sql = "INSERT INTO $table $cols VALUES $values";
	mysql_query($sql) or die(mysql_error());
	return mysql_insert_id();
}

Here is an example of using this code:

<?php
$arr = array('title' => $_POST['title'], 'product_code' => $_POST['product_code'], 'description' => $_POST['description'], 'price' => $_POST['price']);
$product_id = insert_array("products", $arr);
?>

Create bit.ly links automatically

First things first get yourself signed up on bit.ly to receive an API key.

Once you have signed up you can use this PHP function to create links automatically (you will need to provide the bit.ly username and API key as well as the URL).

<?php
function create_bitly_link($apikey, $username, $url) {
	$ch = curl_init("http://api.bit.ly/shorten?version=2.0.1&longUrl=".urlencode($url)."&login=".$username."&apiKey=".$apikey);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
	curl_setopt($ch, CURLOPT_TIMEOUT, 15);
	$data = curl_exec($ch);
	curl_close($ch);
	if (preg_match('/<shortUrl>(.+?)<\/shortUrl>/is', $data, $m)) {
		return $m[1];
	}
	else {
		return false;
	}
}

Fancybox jQuery lightbox

FancyBox is a tool for displaying images, html content and multi-media in a modal window (lightbox) based on the jQuery library.

What I love about FancyBox is loading new windows in an iframe. For example one website that I built used the RPX system allowing users to log in via their favorite social networking site. Rather than just redirect users to the RPX page I had it load in a modal window so they were never taken away from my site.

$(document).ready(function(){
	$('a.iframe').fancybox({
		'frameURL': 'https://XX.rpxnow.com/openid/embed?token_url=XX',
		'frameHeight': 280,
		'hideOnContentClick': false
	});
});