Skip to content

Instantly share code, notes, and snippets.

View gavinblair's full-sized avatar

Gavin Blair gavinblair

View GitHub Profile
@gavinblair
gavinblair / pagination_view.php
Created June 7, 2010 15:33
Pagination Display
<ul class='pager'>
<?php
//The goal is to have a pager showing (if available) 5 numbers,
//with the current page number in the middle, if available.
// <<First and <Previous are shown if you can't see page 1,
// and Next> and Last>> are shown if you can't see the last page.
$start = $currentpage <= 2 ? 1 : $currentpage - 2;
$end = $currentpage > ($totalpages - 2) ? $totalpages : $currentpage + 2;
if ($end == $totalpages && $start > 2) {
$diff = $totalpages - $currentpage;
<select name=”gender”>
<option value=”1”>Male</option>
<option value=”2”>Female</option>
</select>
@gavinblair
gavinblair / thumbnail.php
Created June 8, 2010 15:19
Generate thumbnails on the fly
<?php
if (!isset($_GET['src'])) {
//for testing, navigate to thumbnail.php?img=page/to/yourimage.jpg
echo "<img src='/thumbnail.php?src={$_GET['img']}' />";
} else {
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header ("Pragma: no-cache"); // HTTP/1.0
header("Content-type: image/pjpeg");
@gavinblair
gavinblair / index.php
Created June 10, 2010 16:06
Load a mobile theme if the user is using a smartphone
<?php
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), "iphone") !== FALSE
|| strpos(strtolower($_SERVER['HTTP_USER_AGENT']), "ipod") !== FALSE
|| strpos(strtolower($_SERVER['HTTP_USER_AGENT']), "android") !== FALSE
|| strpos(strtolower($_SERVER['HTTP_USER_AGENT']), "blackberry") !== FALSE ) {
include 'mobile.php';
} else {
?><!DOCTYPE ...
var jQT = new $.jQTouch({
'icon': '/presentation/libraries/jqtouch/themes/mytheme/img/icon.png',
addGlossToIcon: true,
startupScreen: '/presentation/libraries/jqtouch/themes/mytheme/img/startup.png',
statusBar: 'black',
preloadImage: [
//...
],
useFastTouch: false //allows us to do $().submit() on forms
});
@gavinblair
gavinblair / nonscary.js
Created June 15, 2010 16:52
Functions to make plain old Javascript easier to use, without a framework
//How can we make this one work with classes and tags?
function $(id){
return document.getElementById(id);
}
function html(id, html){ $(id).innerHTML = html; }
function css(id, style){ $(id).style.cssText += ';'+style; }
//iPhone/iPad/iPod browser animations
//if animations crash due to excessive tapping, refresh the page
var crashchecker = setInterval(function(){
if ($(".current").length == 0) {
window.location="/";
clearInterval(crashchecker);
}
}, 500);
@gavinblair
gavinblair / option1.php
Created June 16, 2010 18:33
Which is more readable? Keep in mind: this is for a view file.
<?php foreach($pages as $page) { ?>
<div id="<?php echo $page['slug']; ?>" <?php echo $user->uid && $_GET['t'] == $page['slug'] ? "class='current setcurrent'" : ""; ?>>
<div class="toolbar">
<h1><?php echo $page['section']; ?></h1>
<?php if(isset($page['parent'])) { ?>
<a href="?t=<?php echo $page['parent']; ?>" class="parent">Back</a>
<?php } else { ?>
<a id="logout" href="/logout" class="button">Logout</a>
<?php } ?>
</div>
@gavinblair
gavinblair / select.js
Created June 18, 2010 14:47
jQuery select input manipulation
// Add options to the end of a select
$("#myselect").append("<option value='1'>Apples</option>");
$("#myselect").append("<option value='2'>After Apples</option>");
// Add options to the start of a select
$("#myselect").prepend("<option value='0'>Before Apples</option>");
// Replace all the options with new options
$("#myselect").html("<option value='1'>Some oranges</option><option value='2'>More Oranges</option><option value='3'>Even more oranges</option>");
<?php
/**
* Themed attachments
*/
echo "<ul class='attachments'>";
foreach ($files as $file) {
if ($file->list) {
$type = explode('.', $file->filename);
$type = $type[count($type)-1];
$href = check_url(($file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()))));