How do I create a website icon?

September 10th, 2009 No comments

It’s a piece of cake…

Just create a file called favicon.ico (an icon file) and put it in the root of your domain. Optionaly, you can include a meta tag to do it.

More information is here: http://en.wikipedia.org/wiki/Favicon

Also, here is an online generator: http://www.favicon.cc/

There is one gotcha… On your browser, the icon is almost NEVER updated, unless you load just the icon, i.e. http://yoursite.com/favicon.ico then it will refresh the cache, and the new icon will always appear.

Categories: web Tags:

JavaScript Primer

September 10th, 2009 No comments

So you want to learn JavaScript huh? Way to go! SOA powered by JavaScript is the future of the web. (Trust me).

What is JavaScript? It’s a scripting language that all modern web browsers run that allows you to manipulate web pages and do sweet stuff.

To get started, first, you need an easy way to run JavaScript. While you could write javascript in an html page and run it, it’s generally easier to be able to run scirpts without making an html page. To do this, install Firebug, and use it’s console. (Just install firebug, click on the little cock-roach icon in the lower left of Firefox, and click the console tab, now reload your window.).

Once you have the firebug console working, type the following into the console, and you should get a popup.

alert('Hello World!');

Here’s a video that shows how to install and use Firebug:

If you want to learn more, head over to w3schools’ JavaScript tutorial.

Categories: html, javascript Tags:

So you want PHP Templates, eh?

September 2nd, 2009 No comments

PHP Templates! Everyone wants them, some people try to provide them to others, but deep down inside, you all know smarty isn’t the way to go… Allow me to show you a better way.

Just use PHP! Yes, that’s right! PHP is almost designed from the ground up natively support templates!

Here’s the secret… When you include a php file (using the include(“somefile.php”) method, the php code inside the included file has the same variable scope as the code block that included it… So… Take the following two files….

(index.php)
<?php
  go();
  function go(){
    $a = "Hi There!";
    include('my_view.php');
  }
(my_view.php)
<html>
<body>
<?php echo $a; ?>
</body>
</html>

The output would be:

Hi There!

If you’re a noob, another name for this (if you make extra effort to not put anything but loops, and echos in your template) is MVC (Model View Controller). While it isn’t a complete implementation, it’s a really really good start and makes writing and maintaining a site WAY easier. Cake PHP uses this approach for it’s views (aka templates) as well. Java uses this, but instead of the “view” using the local variables where a page is displayed, the data you want the view (or jsp page in java) to have access to are put in the request object as an “attribute”.

I, personally, put the variables I want to use in a “template” (aka “view”) in a global array ($GLOBALS['params']['name'] = “bob”;) It isn’t the best practice, because some part of your code far away could write to this and mess something up, but for simple sites, I find it quick and easy.

Of course, if you REALLY want a templating system, you could use SMARTY. (Blech!) But, I really recommend you just get used the to the echo statements and use php to render templates.

So, some sample code from a site of mine is:

<?php
require_once(BASE_FS_PATH . '/classes/BookService.php');
 
switch($_REQUEST['action']){
	case 'Research':
		$book = BookService::importBook($_POST['ean']);
		$_GLOBALS['params']['book'] = $book;
		include(BASE_FS_PATH . '/views/pages/research.php');	
		break;
	case 'mark_not_sellable':
		$book = BookService::importBook($_GET['ean']);
		$book->sellable = 'no';
		$book->save();
		$_GLOBALS['params']['msg'] = 'Book Marked As Not Sellable.';
		show_details($book);
		break;
	case 'mark_not_available':
		$book = BookService::importBook($_GET['ean']);
		$book->available = 'no';
		$book->save();
		$_GLOBALS['params']['msg'] = 'Book Marked As Not Available.';
		show_details($book);
		break;
	case 'details':
		$book = BookService::importBook($_GET['ean']);
		show_details($book);
 
		break;
	default:
		include(BASE_FS_PATH . '/views/pages/research.php');
		break;
}
 
function show_details($book){
	$_GLOBALS['params']['book'] = $book;
	include(BASE_FS_PATH . '/views/pages/details.php');
}

And the “view” for this code (details.php) is:

<body style="width:950px; margin: 0 auto; text-align: left;" class=" yui-skin-sam">
		<?php 
			include(BASE_FS_PATH . '/views/pieces/menu_and_js.php');
		?>
 
		<div style="text-align: center;">
			<?php echo $_GLOBALS['params']['msg']; ?>
		</div>
 
		<table>
		 <tr>
		  <td>
			<h1><?php echo $_GLOBALS['params']['book']->title; ?></h2>
			<ul>
			<li>Ean: <?php echo $_GLOBALS['params']['book']->ean; ?></li>
			<li>Available: <?php echo $_GLOBALS['params']['book']->available; ?></li>
			<li>Imported: <?php echo $_GLOBALS['params']['book']->import_date; ?></li>
			<li>Cost: <?php echo $_GLOBALS['params']['book']->cost; ?></li>
			<li>Change In Inventory: <?php echo $_GLOBALS['params']['book']->change_in_inventory; ?></li>
			</ul>
			<h2>Urls</h2>
                         (More Code That's been removed for this example)

Anyways, there are LOTs of templating engines out there you can use instead, but I would discourage it. This is the fastest, simplest, most powerful way to do templates (IMO).

Categories: 100 Weeks of PHP, hacks, php5 Tags:

Setting up a PHP Development Server on Windows

August 19th, 2009 No comments

I know of four different ways to do this. I’m going to cover them in the order of my personal preference (and ease).

Install WAMP

Wamp is probably the easiest quickest way to get an apache server running PHP on your computer.

Just download the install here: Wamp Download run it, use the tool to make sure you don’t have anything running on port 80, and start the two services (apache, and mysql) by right clicking the icon in the task bar and clicking “Run All Services”

Download and Run Uniform

Uniform is different from WAMP and XAMP in that there is nothing to install, you just download it, and run it. This provides you with the ability to create a seperate “server” for each project you do, with it’s own mysql and apache binaries, configs, etc… I, personaly, perfer WAMP and writing all my apps to be location agnostic, so that they run anywhere and co-exist peacefully, but you may prefer this.

Install XAMPP

XAMPP is like WAMP. You download it, install it, and click go, but the only time I tried it (a few years ago) I ran into some difficulties. At the time they didn’t have a control panel like WAMP. This may in fact be the superior way to go, but I like my WAMP.

Setup Apache, MySQL, and PHP Binaries.

I did this once. It took me a few days (this was like 3 years ago, when my experience with PHP was ‘nill) If your a masochist this is the way to go. You’ll learn all the nitty gritties (or atleast be impeded by them). Hopefully you can beat my time.

Categories: 100 Weeks of PHP, php5 Tags:

ASP StateServer you MUST set the MachineKey!!!

August 18th, 2009 No comments

I just spent 2 hours beating my head against the wall trying to get the aspnet_state.exe stateserver to work right… Everything was configured, on my desktop the site worked flawlessly when running on Cassinni, and yet, whenever I moved it to production, the sessions were still disappearing when we restarted w3wp.exe… The problem? IIS6 was generating a new Machine Key for each worker process… (I assume this is the default behaivor, but I don’t know). Just setting that in stone in the web.config file fixed the problem. And now, I am dancing with glee because we can use worker process recycling, and end the w3wp.exe process at will without effecting anyone. YAY!!!!

Categories: Asp.Net Tags:

Tell us what you want to know!

July 30th, 2009 2 comments

We love learning. There’s nothing we like more then learning, except sharing what we’ve learned. Without teaching, there isn’t much point to learning. So, tell us what you want to know. Tell us what obscure technology, or command, or syntax you want to know more about, and We’ll write an article about it! Just leave a comment below with what you want to know.

Categories: 100 Weeks of PHP Tags:

Use Other People’s wheels when possible…

July 30th, 2009 No comments

Week #1

    ALWAYS look for a wheel before you invent it.

I work at a software company called “Infusionsoft”. We write this kickbutt marketing software. One of our Software Engineer III’s (that’s the highest). Criticizes anyone anytime they invent something that already exists in our code. He is like a walking Google of existing code.

This is a rule that applies across all programming languages and other aspects of life. If someone else has done it, see if you can either use what they did, or learn from it. Don’t feel bad if you re-invent a few wheels along the way. Sometimes it is easier to just write your own rather then spend hours pouring through searches for someone elses.

min-height CSS Hack

July 17th, 2009 No comments

I needed a min-height hack for css that worked everywhere. I searched for a while, and found this link: http://www.greywyvern.com/code/min-height-hack. It works as advertised, just checkout the tabs on propertynut.com

Categories: css, hacks, html Tags:

MultiThreaded TestNG, Surefire Craziness.

June 30th, 2009 No comments

The Setup

Maven 2.0.9
Surefire 2.4.3
TestNG version 5.8

The Craziness

If you annotate a method to have an invocationCount of 2, and a threadPoolSize of 2, and your test fails in an @BeforeMethod or an @AfterMethod when run by surefire, the test run count reported by surefire will be inaccurate. Significantly innacurate at times.

For example:


package com.infusion.crm.application;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;

/**
* Created by IntelliJ IDEA.
* User: joey
* Date: Jun 29, 2009
* Time: 3:19:13 PM
* To change this template use File | Settings | File Templates.
*/
public class TestThreadPoolSize {
@BeforeMethod
public void before(){
AssertJUnit.assertFalse(true);
}

@Test
public void test1(){
AssertJUnit.assertFalse(true);
}
}

Returns the following from maven test:


-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
Tests run: 8, Failures: 1, Errors: 0, Skipped: 7, Time elapsed: 10.298 sec <<< FAILURE!

Results :

Failed tests:
before(com.infusion.crm.application.TestThreadPoolSize)

Tests run: 8, Failures: 1, Errors: 0, Skipped: 7

This problem is also manifested if you create an AnnotationTransformer that forces the invocationCount and threadPoolSize of a test to both be greater then 1.

This bug is only manifested when multiple threads are used. If you set the invocation count to 20 and the thread pool size to 1, the bug does not appear.

Sometimes it is unpredictable and worse then just 2x the right number of tests. I ran a test, with 9 methods, and 4 threads, and a very exhaustive @BeforeMethod that takes several seconds to run. It failed repeatedly and said: 142 test run, 3 failed, 139 skipped.

Categories: java, maven, testng Tags:

Converting jUnit test to TestNG tests.

June 30th, 2009 No comments

Update

After writing the below script, and submitting my change for a code review, the reviewer pointed me to a TestNG provided tool that will convert them. So here it is:

java org.testng.JUnitConverter -overwrite -annotation -srcdir src

Be sure to include the testng jar, along with the $JAVA_HOME/lib/tools.jar in your classpath when you run that.

Original SED way of converting

Because of the exceptional design of TestNG, this is a pretty simple text replace exercise. Here is a shell script using sed that will do most of the work for you. After it runs, just remove jUnit from your dependency list / classpath and re-compile, hand edit any errors.

There are two steps to running this, first, create a list of all the java files that contain junit references in your src.

find . -name "*.java" | xargs grep -L junit > toconvert

Then, use xargs to run the script on all the files.

cat toconvert | xargs testngify

Here is the contents of the testngify shell script:


#!/bin/bash

IN_FILE=$1
function replace {
OUT_FILE="${IN_FILE}'.tmp'"
#echo executing pattern: $1
sed "$1" < $IN_FILE > $OUT_FILE
rm $IN_FILE
mv $OUT_FILE $IN_FILE
}

replace "s/import org\.junit\.After;/import org\.testng\.annotations\.AfterMethod;/g"
replace "s/import org\.junit\.Assert;/import org\.testng\.AssertJUnit;/g"
replace "s/import org\.junit\.Before;/import org\.testng\.annotations\.BeforeMethod;/g"
replace "s/import org\.junit\.Test;/import org\.testng\.annotations\.Test;/g"
replace "s/import junit\.framework\.Assert;/import org\.testng\.AssertJUnit;/g"
replace "s/import\s*static\s*org\.junit\.Assert\.\*;/import static org\.testng\.AssertJUnit\.\*;/g"
replace "s/import\s*static\s*org\.junit\.Assert\.assertEquals;/import static org\.testng\.AssertJUnit\.assertEquals;/g"
replace "s/\(\s\)Assert\./\1AssertJUnit\./g"
replace "s/import org\.junit\.Test;/import org\.testng\.annotations\.Test;/g"
replace "s/\(@Test\s*\)(expected\s*=/\1(expectedExceptions =/g"
replace "s/\(@Test\.*\)ComparisonFailure\.class/\1AssertionError\.class/g"
replace "s/@After\s*$/@AfterMethod/g"
replace "s/@Before\s*$/@BeforeMethod/g"
replace "s/@Before()\s*$/@BeforeMethod/g"
replace "s/org\.junit\.Assert\./org.testng.AssertJUnit\./g"
replace "s/.*@Ignore(\"\(.*\)\")/\t@Test(enabled=false) \/\/\1/g"
replace "s/\(.*\)@Ignore\(.*\)/\1\2/g"
replace "s/^.*org\.junit.*$//g"

Categories: java Tags: , , , ,