26 Nov 2011

MySQL snippet to group records by week.
SELECT COUNT(*), DATE_ADD(DATE(`orders`.`date`), INTERVAL (7 - DAYOFWEEK(`orders`.`date`)) DAY) AS `week_end` FROM `orders` GROUP BY `week_end`;

31 Oct 2011

Get the index of an element in an unordered set

JQUERY snippet to find the index of an item.

In this case if a list item is clicked, we get the index of the element in relation to it's parent container.

$("ul > li").click(function () { 
    var index = $(this).prevAll().length; 
}); 

4 Apr 2011

Duplicating a MySQL Table

To duplicate an existing table you can use the following command:
CREATE TABLE `NEW_TABLE` LIKE `OLD_TABLE`
To duplicate an existing table and it's contents you can use:
CREATE TABLE `NEW_TABLE` SELECT * FROM `OLD_TABLE`

11 Mar 2011

Compress PHP file

Use PHP itself to compress a PHP source file.

This command line option will strip all comments and white space.
php –w input.php > output.php

10 Mar 2011

Check request is via AJAX in PHP

A simple function to indicate whether a request was made via AJAX or not (note: this is easily spoofed so it's not a secure method).
function isAjax() 
{
   return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) 
   && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
}

8 Mar 2011

How to find duplicate rows with SQL

This article shows how to find duplicated rows in a database table. This is a very common beginner question. The basic technique is straightforward. I’ll also show some variations, such as how to find “duplicates in two columns”
http://www.xaprb.com/blog/2006/10/09/how-to-find-duplicate-rows-with-sql/

7 Mar 2011

Retrieving MySQL enum values in PHP

Retrieve an array of all the available enum values.
if ($result = mysql_query("SHOW COLUMNS FROM {$table} LIKE '{$column}'))
{
  $enum = mysql_fetch_object($result);
  preg_match_all("/'([\w ]*)'/", $enum->Type, $values);
}