Lil Josh

A tech blog about web development with PHP, MySQL, jQuery, CSS3 and various other programming languages. You will not find anything about ColdFusion, ASP.NET, C# on this blog.

AJAX made easy using jQuery

January 14, 2014

AJAX can seem daunting but it’s never been easier to implement with jQuery! So first, let me give my explanation of what AJAX is and can be used for. Imagine you have a piece of content you’d like to have updated every so often (e.g. an average number from a database), but you don’t want the user to have to reload the page to get the updated number. This is where AJAX comes into play – you can setup an AJAX call to GET the output of a script that queries the database and then use the results to update your page with jQuery. So let’s dive into this example:


<!DOCTYPE html>
<script src="//"></script>
var updateInterval=setInterval(function(){updateNumber()},3000);
function updateNumber()
type: 'GET',
url: 'ajaxScript.php'
}).done(function( averageTime ) {
<div id="num"></div>


//get the average time from the last 10 minutes
$stmt = $dbh->prepare("SELECT AVG(time) averageTime FROM `myTable` date >= DATE_SUB(NOW(),INTERVAL 10 MINUTE) LIMIT 1");
$row = $stmt->fetch();
echo $row["averageTime"];

Velocity Cheat Sheet

October 28, 2013

Decided to make a velocity cheat sheet/quick start guide. Hopefully this is useful for anyone just starting out and also a good reference for experienced velocity developers. If I missed anything feel free to let me know.

## This line is a comment
## The next line is a variable assignment
#set($foo = "hello world")
## The next line uses the variable
## Velocity is good with dates
<p>Today is $_DateTool.getDate()</p>
## If your using velocity against XML data
## you can get a single node like so:
#set($singleNode = $_XPathTool.selectSingleNode('/path/to/node'))
## or you can select multiple nodes:
#set($multipleNodes = $_XPathTool.selectNodes('/path/to/nodes'))
## and iterate through the nodes like so:
#foreach ($node in $multipleNodes)
## You can use if statements like so:
## you can get the child nodes like so:

Also a useful reference chart provided by (

Description Usage Example
Variables #set() #set($foo = “hello”)
Comments (single line) ## ## Comment
Comments (multi lines) #* *#  #*
* Comments
* on multiple lines
Arithmetic Operators +   –   *   /   % #set($age = $age + 1)
(not just equivalency, can be used to compare objects)
== #if($gender == “female”)
Logic Operators &&   ||    ! #if($state == “Indiana” && $country == “USA”)
Range Operator
(used in loops)
Escape Character
Reference a variable $age

Jack is $age years old

Reference a variable
(if value is null, print nothing)
Assign a value to a variable
(string literal)
#set ( $!foo = “Velocity” )
Refer to a hashtable key
or a get (Address) method
Conditional Statement #if ()
 #if ($foo)





Loop #foreach ($criterion in $criteria) ## loop ArrayListCurrent Value: $!criterion#end

Swapping variables in PHP

I’ve created a short and sweet PHP function swap() to exchange/swap the values of two PHP variables. Here it is in action:

$cat = "cat";
$dog = "dog";
echo "$cat goes meow and $dog goes woof"; //dog goes meow and cat goes woof
function swap(&$var1, &$var2) {
$tmp = $var1;
$var1 = $var2;
$var2 = $tmp;

If you’re running this function a lot (e.g. millions of iterations) it’s actually faster to just perform the swap without the function. For example:

for($i = 0; $i < 10000000; $i++) {
$tmp = $var1;
$var1 = $var2;
$var2 = $tmp;

Thanks @dnaslave for providing efficiency feedback! :]

Matrix like background

October 17, 2013

<div id="background"></div>
var int=self.setInterval(function(){updateBackground()},100);
function updateBackground()
var content = "";
for(var i=0;i<2048;i++){
content += Math.round(Math.random()).toString();
#background {
background: black;
color: #0F0;