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.

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

Create bit.ly links automaticallyFirst 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).

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;
 }
}