Category Archives: Coding

selectize.js combobox example

Selectize.js ComboBox: Cloning and Destroying

Selectize.js is a javascript library that allows you to offer more complex HTML select boxes, such as combining a select and an input box, commonly known as an autocomplete combobox. Most importantly, they have a built-in stylesheet for Bootstrap 3. However, I discovered a problem when you are trying to add another form field dynamically, specifically using jQuery’s .clone() function.

However, selectize() does not clone well… it breaks horribly. The key you must .destroy() the selectize() prior to cloning. Of course, another problem occurs then: The select element, upon selectize.destroy(), will reset the value to the last option. Solution? Store the value, destroy(), then set the value.

// When add button is clicked
$('#add').on('click',function(){
   $('.combobox').each(function(){ // do this for every select with the 'combobox' class
      if ($(this)[0].selectize) { // requires [0] to select the proper object
         var value = $(this).val(); // store the current value of the select/input
         $(this)[0].selectize.destroy(); // destroys selectize()
         $(this).val(value);  // set back the value of the select/input
      }
   });
   $('#monsters .form-group:first')
      .clone() // copy
      .insertAfter('#monsters .form-group:last'); // where
      selectizeme(); // reinitialize selectize on all .combobox
});

Here’s a working demo of the functionality.

PHP Alternatives for Nested IF Statements

We’ve all done it. It starts as a single IF statement:

if (empty($var)){ // does it exist?
   $error = 'No username entered';
   return $error;
}
// everything is OK

Then we decide, we need to check something else, so we just add in one more IF statement:

if (empty($var)){ // does it exist?
   $error = 'No username entered';
   return $error;
   if (strlen($var) > 20){ // Is it too long?
      return 'Invalid username';
   }
}
// everything is OK

Continue reading

debian 8 "jessie" LAMP

Install Apache, MySQL, PHP5, and PHPMyAdmin on Debian “Jessie”

This is a quick guide to getting an Amazon EC2 server up and running with LAMP and PHPMyAdmin based on the LTS (Long Term Support) Debian version 8.5 “Jessie”. This guide was written using the Community AMI image debian-jessie-amd64-hvm-2016-04-03-ebs for Debian “Jessie” and assumes you are able to launch the server and connect via SSH.

Amazon assumes an EBS volume of 8GB (t2.nano) or 10GB (t2.micro), but if the server will be storing very little, the real minimum size of the EBS can be calculated safely as:
1.5GB + (RAM x 2)
That is roughly 3GB EBS for t2.nano, and 4GB EBS for t2.micro.

Continue reading

WebM Converter Batch File

Encode WebM Video Files on Windows for HTML5 with FFmpeg

If you want to put video on the web, you’ll probably just going to upload it to YouTube and use their embed code. However, if you want to embed video on your own site without YouTube you want to make use of HTML5’s video element. To do that, you’ll want to make two encodings of your videos: WebM (Google backed) and H.264 (current standard). H.264 is easy because it’s been a standard for a long time (I personally use HandBrake), but WebM is quite a bit more difficult. There hasn’t be a defacto winner when it comes to encoding, though Miro Converter has come close.

However, the core of Miro and many others is the open encoder FFmpeg so we’re just going to setup an easy way to use that in this tutorial. Here are our goals:

  1. Install FFmpeg on Windows
  2. Create a Batch file (.bat) that we can drag-and-drop video files on to create WebM video
  3. Write the HTML5 code to allow for maximum speed and compatibility

Continue reading

Tutorial: Exporting Messages from WhatsApp and Graphing with R

This tutorial is posted with permission by Prometheus09 on Reddit. It was originally posted as an album on Imgur.

Number of messages sent per week throughout the relationship

Number of messages sent per week throughout the relationship

So here is a quick tutorial on how to construct the graph shown above, showing the distribution of messages over a given time period. First of all we must obtain the dataset we are going to use, which for this tutorial is the chat history obtained from WhatsApp. This can be obtained following these instructions or for iMessages or FaceTime data you can use this (paid) software.
Continue reading

Connecting Remotely to MySQL Database on an Amazon EC2 Server

While this was tested using a TurnKey LAMP Server build (Debian Linux), it applies to any Debian/Ubuntu server with MySQL.

Top-Level View of Tutorial

  • Enable MySQL access through server’s firewall (Amazon AWS-EC2 Security Groups)
  • Create non-root MySQL user with % (any) Host permissions
  • Alter the bind-address in the MySQL configuration file (my.cnf) file
  • Restart MySQL

Continue reading

jQuery Mobile Filterable Collapsible Listview with Smart Expansion

Wow, that’s a mouthful. Basically, I wanted to make use of the data-filter option to search a listview, but instead of a listview, I wanted it to be a collection of collapsible elements. Do accomplish this I needed some custom CSS to make it look right. Here is the example code with the ID iamalist. The page’s content section ID is iama, which is used in the CSS.
Continue reading