Skip to content

Instantly share code, notes, and snippets.

View gavinblair's full-sized avatar

Gavin Blair gavinblair

View GitHub Profile
@gavinblair
gavinblair / minetweet.php
Created January 21, 2011 22:29
script that periodically checks the minecraft server log, and tweets when players log in/out
<?php
//run via cron every 2 minutes
//Get the log file
$log = file_get_contents("/opt/minecraft/server.log");
$log = explode("\n", $log);
$loggedinusers = array();
//only look at lines that mention logging in or logging out
@gavinblair
gavinblair / rtrim.php
Created January 10, 2011 23:22
lose the last comma off a list
<?php
$sql = "UPDATE coupons SET ";
foreach($args as $key => $arg) {
$arg = $this->db->escape($arg);
$key = $this->db->escape($key);
$sql .= "$key = $arg, ";
}
//lose the last comma
$sql = rtrim($sql, ", ");
@gavinblair
gavinblair / citybudget.js
Created December 6, 2010 00:29
Example implementation of the city budget API using JSONP
//It involves two steps -- the ajax call and the call back function:
$.ajax({
url:'http://budget.opendatalondon.ca/api/budget/year/2010/org/803/format/jsonp',
type:'GET',
dataType:'jsonp',
success: abc
});
//the JSON is passed as a parameter to your callback function
function abc(budget){
<?php
//updated to fit http://drupal.org/writing-secure-code
function ajax_autocomplete(){
$q = $_GET['z'].'%';
$limit = intval($_GET['limit']);
$sql = "select distinct title from node where status = 1 AND (type = 'product' OR type = 'model_documentation') AND title LIKE '%s' order by title limit %d";
$result = db_query($sql, $q, $limit);
while($row = db_fetch_array($result)) {
echo "{$row['title']}\n";
$('fieldset.collapsible:not(.tao-processed) > legend > .fieldset-title').each(function() {
var fieldset = $(this).parents('fieldset').eq(0);
fieldset.addClass('tao-processed');
// Expand any fieldsets containing errors.
if ($('input.error, textarea.error, select.error', fieldset).size() > 0) {
$(fieldset).removeClass('collapsed');
}
// Add a click handler for toggling fieldset state.
$(this).click(function() {
if (fieldset.is('.collapsed')) {
@gavinblair
gavinblair / wrapping_if.php
Created November 18, 2010 18:52
Which method is better?
<?php
function myfunction(){
if($somecondition) {
...
... //many lines of code
...
} else {
return false;
}
@gavinblair
gavinblair / date_sub.sql
Created October 27, 2010 16:35
select rows with timestamp within the last 30 days
SELECT * FROM mytable WHERE timestamp > DATE_SUB(NOW(), INTERVAL 30 day);
@gavinblair
gavinblair / labels.css
Created October 20, 2010 17:09
Adds : after labels and * after required labels
/* Adds : after labels */
form label:after{
content:":";
}
/* Adds * after required labels */
form label.required:after{
content:"*";
}
@gavinblair
gavinblair / mymodule.module
Created September 30, 2010 21:26
Multilingual Module Pages in Drupal
<?php
function mymodule_menu(){
$languages = language_list('enabled');
$items = array();
foreach ($languages[1] as $lang) {
$items[t('my_path', array(), $lang->language)] = array(
'title' => t('Page Title', array(), $lang->language),
'page callback' => 'pagefunction',
$("input[type=text]").each(function(){
if($(this).attr("title").length > 0) {
//do the thing, to $(this)
}
});