Programming

ls – Quick Unix Commands

Posted in Programming on September 16th, 2008 by Jason – Be the first to comment

The ls command for unix/linux. It can be a useful command when you need it.

Use the ls command to list the items in the current directory. If you want to get some additional info about the files in the directory you can type in several different parameters

ls -l

This will give a lot of helpful information regarding each file such as its permissions.

Get your sort on! array_multisort()

Posted in Programming on July 15th, 2008 by Jason – Be the first to comment

Recently I was working on a project that does a lot of calculations on information stored in a database. My results were stored in an array, and the user info in another. I then had to sort one array, while at the same time maintain a relationship with the other. Well as my head began to steam looking for a solution to something fairly simple I came across array_multisort() and it did the trick!

Let me try to explain the problem and hopefully it will help to understand how to use this function

// I have my first array
$arr1 = array(0=>"f", 1=>"a", 2=>"c", 3=>"d");
// In other words $arr1 holds the values f,a,c and d
// Now for the second array
$arr2 = array(0=>"Bob, 1=>"Sally", 2=>"Fred", 3=>"Jason");

So we have the basic elements set up. Now my program requires to list the information in arr1 in order of grades, a-f with the highest grade listed first. PHP provides us with the sort() function.

// example of php sort() function
sort($arr1);
// our array now has the values a,c,d and f in order

Do you see the problem? Jason (Me) would now be associated with an F and not the D that he deserves. And I worked so hard for that D! So the problem is that you now have to sort the second array in the exact same way as the first array, so that the index of arr1 and arr2 still correlate. Enter the array_multisort(), its times like this you love PHP and how easy it can make life.

// We use the same array... ignoring our previous sort command
array_multisort($arr1, $arr2);
// Now our array contains the following information
// a,c,d and f
// Sally, Fred, Jason, Bob
// Sally => A, Fred => C, Jason => D, Bob => F

Yeah, I got a D! With the array_multisort you can do exactly what it says, sort multiple array while maintaining a link, or way to keep the data together in the arrays.

Although it is much easier to keep all your data inside a database and then have it sort when you retrieve it, sometimes you have to do the sorting outside of the database. This example my not have been the greatest, but it is used just to show how to use it.

One last thing, say the multi sort does not sort it how you want it, well you can change the sort order as well.

// Changing the sort order
array_multisort($arr1, SORT_ASC, $arr2);
// Note that SORT_ASC and SORT_DESC can be interchanged
// depending on what your needs are.
// You can also add multiple sorts
array_multisort($arr1, SORT_ASC, $arr2, SORT_DESC);

This last sort is the same as saying, sort arr1 ascending and then sort arr2 descending. It works very similar to sorting in a spreadsheet.

Creating executable Jar files in Java

Posted in Programming on July 8th, 2008 by Jason – Be the first to comment

When all is said and done, in your program in Java, usually you are left with several class files. Sending this file to a friend can be difficult, and thats when you use the .jar “Java ARchive”.

to create just a jar file. use:

jar cf myJar.jar *.java to make it executable jar cfe myJar.jar *.java *.class and to run it java -jar myJar.jar

the other method to create executable jar files is using the manifest.

java cfm myJar.jar MANIFEST.MF *.java *.class

The code stands for the following. “java” the compiler, “cfm” create – file – manifest, “myJar.jar” name of the jar file you will create, “MANIFEST.MF” this tells it will be a manifest file, “*.java” this will include any .java file in the current working directory, “*.class” will take any .class files in the current working directory.

Note that you do not need to include the .java files, only do this if you want to include your source code in your jar file.

Short Hand PHP, and Echo Statements

Posted in Programming on June 30th, 2008 by Jason – Be the first to comment

Quick ways to use the echo statement in php. Reduce the amount of your code with this quick tip.

Instead of writing out your echo statements you can use a quick short hand technique. To do this you must have “short_open_tag” allowed, this can be done in the php.ini file but is usually defaulted to true.

< ?='hello world'?> // will echo hello world

If doing this code doesnt produce the echo statement, there is a good chance that you do not have short_open_tag enabled.

Get the last Auto Increment ID with mysql_insert_id()

Posted in Programming on June 18th, 2008 by Jason – Be the first to comment

This function comes in handy if you need to know the key or index of the last insert into your mysql database. Other methods just don’t cut it.

By using mysql_insert_id() you will get the id of the last inserted statement.
/* Say you enter a new row into your database
After you have finished the insert statement you would do something like this.*/
$lastId = mysql_insert_id();

So why do I say other methods don’t cut it? When you thing about it, a lot of people just run into problems counting the number of rows in the database, and then obviously the next one would be the new one. This is a BAD IDEA!!! Consider the following example.

// rows in a table
1 carlos
2 suzy
3 jack
4 robert
5 mary
// If we insert another record, we can assume it will be 6.
6 fred
// If we counted the rows it would work fine.
// What happens when suzy doesnt like us and wants her name removed?
// We now have a list like this
1 carlos
3 jack
4 robert
5 mary
6 fred
// Now if we want to submit sam, using the row counting method we would say his id is 6 but in actuality it will be 7.

Because we dont want to mix up sam’s id with freds, we have to use the mysql_insert_id.

Ternary Conditionals, An alternative to If Then

Posted in Programming on April 12th, 2008 by Jason – Be the first to comment

Ternary Conditionals can be a great way to reduce the amount of code and simplify your life. I think of them as an alternative to if then statements in PHP. Although they do not; and I dont suggest that you try, completely replace the if then statement, they can be used in many cases where you would normally use if then.

Just to give you a brief look at the syntax for one of these conditionals

$value=(condition) ? true_value : false_value;
echo ($i=1) ? '$i is equal to 1' : '$i is not equal to 1';

The first thing you need is your condition, this is put inside the ( ) and followed by the “?”. Ned you will need the action to be performed if true, followed by a colon “:” and the action to be performed if false. Thats it, a pretty simple test. In the above example I want to echo a certain message, based on whether the variable $i is equal to 1. If it is then the echo statement will echo the true value “$i is equal to 1″ and if false, “$i is not equal to 1″.

There are many ways to use these conditionals, but I will leave it up to you to find the best ways to implement it. For now, consider it another tool in the belt.

PHP Text Formatting, ucwords() ucfirst()

Posted in Programming on February 2nd, 2008 by Jason – Be the first to comment

While working on a website for my work, I needed to do more text formatting than I normally do. I needed the first letter of a user input word to be capitalized. By using the php functions ucwords() and ucfirst() I was able to get this accomplished.

There are a couple different ways to use these two functions. One is if you want to capitalize the first letter of the first word in a string. You would use this for a sentence. Or if you wanted to capitalize the first letter of every word in a string, possibly for a title. The following are a few example codes of how to implement these functions

//capitalize the first letter of a string
$string = 'hello, my name is sam.';
echo ucfirst($string);
//This will print the following:
//Hello, my name is sam.
//capitalize the first letter of each word in a string
$string = 'hello, my name is sam.';
echo ucword($string);
//This will print the following:
//Hello, My Name Is Sam.

While this works good there are a couple things that you can do to make sure that the text appears as you really want it to. In the first example we wanted the first letter capitalized in the sentence, maybe we did this because users were not capitalizing things and it looks better capitalized. In the previous case there is an assumption that is made, but easy to forget. We assumed that the user would type all lower case letters. Wouldnt the world be a better place if we never received emails/posts/comments etc that were not in ALL CAPS. To correct an all caps string is very easy.

//capitalize the first letter of an ALL CAPS string
$string = 'HELLO, MY NAME IS SAM.';
echo ucfirst(strtolower($string));
//This will print the following:
//Hello, my name is sam.
//capitalize the first letter of each word in a string
$string = 'HELLO, MY NAME IS SAM.';
echo ucword(strtolower($string));
//This will print the following:
//Hello, My Name Is Sam.

By using the strtolower() function you can eliminate the ALL CAPS messages that everyone loves, this will make you string into all lowercase letters like we had in the first example and the capitalize the first letter of the string or of each word in the string.

Multiple OnLoad() Statements

Posted in Programming on January 12th, 2008 by Jason – Be the first to comment

It is sometimes necessary to use multiple onload statements within the body tag of you website. You might use them to preload images for hover over effects, or to set values of a form if there are values being passed from a different page, ie. $_GET values in php.

Okay, first to explain why in the world you would need to use an onload javascript for values passed to the page through the url. Yes you could just as easily use php to auto populate the form. In fact this is usually the case, and what I do for most pages.

To use these statements is fairly easy

body onload="function(values);function2(values)"

When using multiple javascript functions with body onload you need to separate them with a semicolon (;). You can also use the following code if you need to execute many functions on page load, but want to keep your code looking nice.

body onload="allfunctions()"

function allfunctions(){
function1();
function2();
function3();...
}

Great Advice to Start your Design

Posted in Ramblings, Websites on May 9th, 2007 by Jason – Be the first to comment

While working on the re-design of sidewaysgravity, this article came in very handy. It is good to gain a better understanding of how to start the design process. By creating the wireframes first and then applying the designs to the wireframe, it was alot easier to setup the design.

Digital-Web.com

Another late night of progress.

Posted in Websites on May 5th, 2007 by Jason – Be the first to comment

Well after much hard work with the design layout of the site, I came to a terrible realization. One, the colors that look so perfect on my macbook, dont look quite the same on my wifes G5, and after I corrected it, it looks yet another color on my Dell PC. So I still have much more work to do. But on the bright side, the main page is coming together, and I am getting the blog incorporated into it.