Skip to content

Instantly share code, notes, and snippets.

View alancoleman's full-sized avatar
:octocat:

Alan Coleman alancoleman

:octocat:
View GitHub Profile
@alancoleman
alancoleman / drupal6-drupal7-module-upgrade.php
Last active December 28, 2015 02:39
Drupal 6 -> Drupal 7 Module upgrade
<?php
# db_fetch_object
// Drupal 6
while ($record = db_fetch_object($result)) {
// Do stuff with $record, which is an object
}
// Drupal 7
foreach ($result as $record) {
@alancoleman
alancoleman / output_serial_number_in_sql.sql
Last active December 25, 2015 04:09
Output an incremented serial number as part of a select statement
SET @serial=0;
SELECT @serial := @serial+1 AS `serial_number`
@alancoleman
alancoleman / diff_commands.sh
Last active December 24, 2015 23:49
diff commands for shell
# The difference between two folders
diff folder_1 folder_2
# The difference between two files
diff file_1.txt file_2.txt
# The difference between two folders recursive
diff -r folder_1 folder_2
# Create a patch file from a diff
@alancoleman
alancoleman / select_two_counts.sql
Created October 3, 2013 15:47
Make two counts at the same time, the first of all users, the second of all active users.
SELECT COUNT(*) AS users, COUNT(IF(a.active BETWEEN '2013-09-01 00:00:00' AND '2013-09-31 23:59:59',1,NULL)) AS active
FROM lsoncc_Users AS u
LEFT JOIN lsoncc_UsersFlags AS a
ON u.userid = a.userid
@alancoleman
alancoleman / basic_php_file_download.php
Created October 1, 2013 14:19
Basic PHP File download
<?php
$file = '/tmp/file.xml';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
@alancoleman
alancoleman / druapl_6_mail_send.php
Last active December 24, 2015 09:39
Send a simple mail from Drupal 6 using drupal_mail hook
<?php
drupal_mail( 'control_centre_feed', 'feed', '[email protected]', language_default(), $params, '[email protected]');
function control_centre_feed_mail ($key, &$message, $params) {
switch ($key) {
case 'feed':
$message['subject'] = t($params['filename']);
$message['body'][] = t($params['filepath']);
break;
}
@alancoleman
alancoleman / create_write_read_file.php
Last active December 23, 2015 03:29
Function to create a file, add some content and write out to the browser.
<?php
// createFile function
function createFile($filename) {
// Declare content
$content = "Some content here";
$morecontent = "And some more content here";
// Add content to file
file_put_contents(
@alancoleman
alancoleman / basic_apache_bash_admin
Last active December 22, 2015 08:59
Some basic BASH Apache admin for reference
//Create fqdn file in conf.d and add localhost
$ echo "ServerName localhost" | sudo tee /etc/apache2/conf.d/fqdn
//File path and gedit to add vhost, so that it will resolve to localhost
sudo gedit /etc/hosts
//Restart Apache
$ sudo /etc/init.d/apache2 restart
// Sites available folder
@alancoleman
alancoleman / bash_db_backup_and_restore
Last active December 22, 2015 06:59
Simple method for backing up and restoring a database using bash
// Backup using mysqldump
mysqldump my_database > database_backups/my_database_backup.dump;
// Restore using mysql
mysql> create database my_database_restore;
mysql> use my_database_restore;
mysql> source database_backups/my_database_backup.dump;
@alancoleman
alancoleman / mysql_dob_decider_examples.sql
Last active December 22, 2015 05:28
Two examples of working with dob in MySQL as age rather than date.
SELECT dob FROM from table
-- all dob between 20 and 25 years of age
WHERE FLOOR(DATEDIFF(now(), CAST(dob AS DATE))/365.25 ) BETWEEN 20.0 AND 25.0
-- all dob equal or older than 40
AND Year(Curdate()) - Year(dob) - ( Right(Curdate(),5) < Right(u.dob,5)) <= 40
-- dob within the last six months
AND dob >= DATE_SUB(NOW(), INTERVAL 6 MONTH)