Skip to content

Instantly share code, notes, and snippets.

View martynchamberlin's full-sized avatar

Martyn Chamberlin martynchamberlin

View GitHub Profile
@martynchamberlin
martynchamberlin / fibonacci.js
Created October 28, 2013 18:21
Here's a bad, bad implementation of Fibonacci sequence. And yet it's the best I'm capable of coming up with by myself.
function fibonacci( n )
{
prev_1 = total = 0;
prev_2 = 1;
if ( n == 0 )
return 0;
if ( n < 3 )
return 1;
for ( var i = 0; i < n - 1; i++ )
{
@martynchamberlin
martynchamberlin / fibonacci.js
Last active December 26, 2015 19:29
Here's a pretty decent implementation of the fibonacci sequence. It's taken from pseudocode from my Discrete Mathematics textbook and modified to be compliant with the true sequence (see lines 7 and 8). I'm not getting any console errors when I call the function without the second parameter so it's all good. Wish Javascript made optional paramet…
function fibonacci(n, recursive)
{
if ( n == 0 )
return 0;
if ( n < 3 )
return 1;
if ( recursive !== true )
n--;
return fibonacci( n-1, true) + fibonacci( n-2, true);
}
@martynchamberlin
martynchamberlin / index.html
Last active December 27, 2015 07:09
This is the most drop-dead simple jQuery slideshow you could possibly have. Nothing fancy at all, but mind-boggling get-out-of-the-way simple. Actually using it for a client project.
<div id="slider">
<img>
<input type="hidden" value="/wp-content/themes/geek/images/slide1.png">
<input type="hidden" value="/wp-content/themes/geek/images/slide2.png">
<input type="hidden" value="/wp-content/themes/geek/images/slide3.png">
<input type="hidden" value="/wp-content/themes/geek/images/slide4.png">
</div>
@martynchamberlin
martynchamberlin / main.cpp
Last active December 27, 2015 17:49
Console C++ application. Gets user input and checks to see if it is a valid integer.
#include <iostream>
using namespace std;
bool isInt(char tempNumber);
int main()
{
char tempNumber[10];
while ( true )
@martynchamberlin
martynchamberlin / index.html
Last active December 27, 2015 20:09
Simple show/hide structure. I build this an absolute ton and need a place to reference it so I'm not constantly writing the same thing over and over again. The beauty of this structure is that you can have an unlimited number of .toggle elements on a page and there will not be any conflict. No unique IDs required.
<div class="toggle">
<a href="#" class="read-more">Read more</a>
<div class="hidden hiding" style="display:none">This text toggles back and forth</div>
</div>
@martynchamberlin
martynchamberlin / orderBySubArray.php
Last active January 4, 2016 13:38
Sort a PHP array based on a subarray.
<?php
function orderBySubArray( &$array, $new_key = 'order', $order = 'ascending', $start_at = 1 )
{
$output = array();
$count = count( $array );
$i = ( $order == 'ascending' ) ? $start_at : $count;
for ( $j = 1; $j <= $count; $j++ )
{
@martynchamberlin
martynchamberlin / RandomNumber.java
Created February 7, 2014 05:28
Verbose Java. This is the first Java program I've written that actually took me a few hours to write and took some thought. It's probably a lot more verbose in length than it needs to be — in fact, I know this for a fact. I hope some day to review it and smile at how bad a programmer I was. The main method is stored in UX.java. That name only ma…
/**
* Martyn Chamberlin
*/
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Random;
/**
* In true purist fashion, every field in this class is set to private, and more
@martynchamberlin
martynchamberlin / functions.php
Last active August 29, 2015 13:58
Move data from custom Wishlist Member registration fields to WP User Meta
<?php
/**
* Just paste this code into your theme's functions.php. When the user submits the form, the
* input fields (specified by $fields array) goes into a bunch of cookies. Once the person is
* actually logged in and has a wp_users row associated with then, we then take this cookie
* data and associated it with their account via the user_meta API. Lastly we delete the cookies.
*/
function move_data_from_wl_to_wp()
{
$fields = array( 'state', 'county', 'address1', 'address2', 'city', 'zip' );
@martynchamberlin
martynchamberlin / eval.php
Last active August 29, 2015 13:59
Simple PHP evaluluator
<?php
/**
* Often times in development, I find it convenient to test something out in
* the rough before I actually implement it in the software I'm working on.
* Usually this is to get the parameters and "ropes" of a built-in function
* into my head before testing it in a real (i.e., more moving parts, and
* therefore more difficult to debug) environment. I do this in whatever
* language I happen to be working in — whether that be Java, C#, or PHP.
*
* In terminal, you can run `php -a` to simulate a quick PHP test. That is
@martynchamberlin
martynchamberlin / force-http-wordpress.php
Created April 23, 2014 22:22
In case you need to force HTTPS on every single page of a WordPress site, here's how.
<?php
$using_ssl = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' || $_SERVER['SERVER_PORT'] == 443;
add_action('wp', 'check_ssl');
function check_ssl()
{
// All pages must be https. Redirect to https if not already
if (! $using_ssl)
{
header('HTTP/1.1 301 Moved Permanently');