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.

Matrix like background

October 17, 2013

<div id="background"></div>

<script>
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").text(content);
  }
</script>

<style>
#background {
background: black;
color: #0F0;
width:100%;
word-wrap:break-word;
}
</style>

Sort HTML table using jQuery

September 14, 2013

I’ve put together a script that allows you to make any HTML table sortable by clicking on the table headings. See the example below:

Name Age
John Smith 18
Ashley Washington 26
Patrick Green 19

To use, download ljSortable.min.js and include it near the bottom of your page (after the table) and give your table an id of “ljSortable”. For example:

<table id="ljSortable">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John Smith</td>
<td>18</td>
</tr>
<tr>
<td>Ashley Washington</td>
<td>26</td>
</tr>
<tr>
<td>Patrick Green</td>
<td>19</td>
</tr>
</table>
<script src="http://liljosh.com/ljSortable.min.js"></script>
HTML jquery sort sortable sorting table

Javascript Format Phone Number Function

July 2, 2013

This is a Javascript function to format any 10 digit phone number into the following format: (555) 555-5555

<script>
/*
* Created by http://liljosh.com
* Formats a phone number to be in (555) 555-5555 format
*/
function phoneFormat(phone) {
  phone = phone.replace(/[^0-9]/g, '');
  phone = phone.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3");
  return phone;
}
</script>

Example usage:

<div id="phoneNumber">555-555-5555</div>
<script>
  var phone = $("#phoneNumber").text();
  phone = phoneFormat(phone);
  $("#phoneNumber").text(phone);
</script>

See demo:

http://jsfiddle.net/Q7GNX/

format function javascript jquery phone number

Introduction to PHP PDO

June 12, 2013

CONNECTION

//replace host, database, user, password
$dbh = new PDO("mysql:host=mysql.host.com;dbname=DATABASE", 'USER', 'PASSWORD'); 

SELECT

$stmt = $dbh->prepare("SELECT id, name FROM table");
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row) {
  echo "$row[id]: $row[name]<br />";
}

SELECT WITH NAMED PARAMETER

$example = $_GET["example"];
$stmt = $dbh->prepare("SELECT id, name FROM table WHERE column = :value"); //value can be named whatever you'd like
$stmt->bindParam(":value", $example); //pass a variable to the named parameter
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row) {
  echo "$row[id]: $row[name]<br />";
}

INSERT WITH NAMED PARAMETER

$firstname = $_GET["firstname"];
$lastname = $_GET["lastname"];
$stmt = $dbh->prepare("INSERT INTO `users` (`firstName`, `lastName`) VALUES (:firstname, :lastname)"); 
$stmt->bindParam(":firstname", $firstname);
$stmt->bindParam(":lastname", $lastname);
$stmt->execute();

UPDATE WITH NAMED PARAMETER

$date = date("Y-m-d H:i:s");
$id = $_GET["id"];
$stmt = $dbh->prepare('UPDATE `users` SET `lastLogin` = :lastLogin WHERE `id` = :id');
$stmt->bindParam(":lastLogin", $date);
$stmt->bindParam(":id", $id);
$stmt->execute();
cheatsheet code samples mysql PDO PHP queries quickstart Security