Skip to content

Instantly share code, notes, and snippets.

View gavinblair's full-sized avatar

Gavin Blair gavinblair

View GitHub Profile
@gavinblair
gavinblair / page.tpl.php
Created June 14, 2011 19:33
Drupal - Get access to the $node variable in page.tpl.php
<?php
//we want the node, outside of node.tpl.php!
$node = $variables['node'];
@gavinblair
gavinblair / mymodule.php
Created June 14, 2011 18:01
Drupal 6 - make external image_buttons work like D7
<?php
//you could also add this to your theme's template.php file, by replacing "phptemplate_image_button" with "themename_image_button" in the function name.
function phptemplate_image_button($element) {
// Make sure not to overwrite classes.
if (isset($element['#attributes']['class'])) {
$element['#attributes']['class'] = 'form-'. $element['#button_type'] .' '. $element['#attributes']['class'];
}
else {
$element['#attributes']['class'] = 'form-'. $element['#button_type'];
}
@gavinblair
gavinblair / resend.php
Created June 13, 2011 21:22
Drupal - resend activation email
<?php
$uid = 120;
$user = user_load(array('uid' => $uid));
$op = 'status_activated';
_user_mail_notify($op, $user);
@gavinblair
gavinblair / random_math_question_positive_only.php
Created June 6, 2011 17:26 — forked from SeanJA/random_math_question_positive_only.php
Generate a random math question that always has a positive answer
<?php
$ops = array('-', '+');
$answer = -1;
while($answer < 0 || $answer > 99) {
$num1 = rand(0, 100);
$num2 = rand(0, 100);
$num1 -= 50;
$num2 -= 50;
@gavinblair
gavinblair / ui-tabs.js
Created June 3, 2011 15:37
Make back/forward buttons work on jQuery UI tabs.
//change the # in the url when switching tabs
//makes refresh work!
$("#tabsMenu li a").click(function(){
window.location.hash = $(this).attr("href");
});
//make back/forward buttons work on tabs
if($("#tabsMenu").length > 0) {
//prevent scrolling when clicking on a tab
var doit = true;
@gavinblair
gavinblair / db_query.php
Created May 25, 2011 20:11
Drupal 7 allows you to pass an array as the second parameter in db_query(). However, in Drupal 6 you need to pass each value in as a separate parameter. Here's how to use call_user_func_array() to call db_query with an unknown number of fields and values
<?php
//$values is an associative array where the keys are field names and values are the values we want in the database
//create a string with enough %s's for fields and values. Wrap the values in single quotes.
$percents_fields = "";
$percents_values = "";
foreach($values as $field => $value) {
$percents_fields = "%s, ";
@gavinblair
gavinblair / daterange.php
Created April 12, 2011 21:48
The user can input TO and FROM dates and times for an event. How do we display the event time nicely?
<?php
function daterange_range($starttime, $endtime = null) {
$return = "";
//set the endtime to the start time if there isn't an endtime provided
$endtime = empty($endtime) ? $starttime : $endtime;
//turn them into timestamps if they are not timestamps already
$endtime = ((int)$endtime == $endtime && is_int($endtime))? $endtime:strtotime($endtime);
$starttime = ((int)$starttime == $starttime && is_int($starttime))? $starttime:strtotime($starttime);
//boolean values to prevent test duplication,
@gavinblair
gavinblair / template.php
Created April 7, 2011 15:14
Use the latest jQuery with your Drupal theme
<?php
function {theme_name}_preprocess(&$vars, $hook) {
if (arg(0) != 'admin' && $hook == "page") {
// Get an array of all JavaScripts that have been added
$javascript = drupal_add_js(NULL, NULL, 'header');
// Remove the original jQuery library and Drupal's JS include
unset($javascript['core']['misc/jquery.js']);
unset($javascript['core']['misc/drupal.js']);
@gavinblair
gavinblair / msg.php
Created March 4, 2011 20:11
Minecraft Server Message command
<?php
unset($argv[0]);
$message = implode(" ", $argv);
shell_exec("curl -d '{\"name\":\"SERVER\",\"message\":\"$message\"}' http://10.0.1.104:80/up/sendmessage");
@gavinblair
gavinblair / case-sensitive-like.sql
Created February 28, 2011 20:04
Case Sensitive LIKE in MySQL
// this returns 1 (true)
select 'A' like 'a'
// this returns 0 (false)
select 'A' like binary 'a'
//from http://www.delphifaq.com/faq/databases/mysql/f801.shtml