The ramblings of web developer Beau Brownlee

I recently had the need for printing a Google maps static image map in a Silverlight application. It needed to actually be embedded in the Silverlight app to take advantage of the Silverlight 4 printing library (a topic I will touch on later). I wrote an API to make it easy to do pretty much every thing that the Google static API allows you to do. Here’s a quick demo of what the results look like in silverlight:


Read the rest of this entry »

 
June 21st, 2010
 

    date("Y-m-d H:g:s", strtotime($data))

If that needs explanation then I cannot help you… ;)

 
June 18th, 2010
 

This is a great article: http://virtualrealityblog.com/random/dont-be-a-perfectionist/. The author is NOT saying to not strive for excellence rather do not try and be something that you inherently are not and will never be. If you really try to reach out for perfection you will always be disappointed. The same goes for your expectations of others. If you expect perfection from others you will forever be disappointed. Impossible expectations of others is the definition of oppression. No one can accomplish perfection and to expect it of yourself or others makes you the oppressor of yourself or another. I’ve seen this all too often in the development world. The expectations almost always come from an individual who thinks he/she understands development but they actually don’t. This is one reason why I am a very strong advocate for development managers actually having a background of development. You would think that this is a ‘no-brainer’ but unfortunately it is not the case. Many times developers are placed under the heavy hand of managers who have never actually held the title ‘developer’ and as Zed Shaw says they think of development as ‘manufacturing’ when the reality is that programming is a very messy art, full of twists and unexpected turns no matter how much experience you have. So always strive for the best, but never strive for perfection or require perfection from others. If you do, you will never reach a goal.

 
June 15th, 2010
 

Something that can become annoying when working on a php project is all of the includes you have to do. More to the point is all of the path changes you have to do. Depending on how the project was built it could be an easy task such as ‘include(”../file.php”)’. However in the real world sometimes your working on a project that you’ve inherited and the folder structure can be much messier than that. A handy tool in some circumstances is a recursive function that requires the name of the file and the function finds the file recursively through your folder structure:

static function include_file($name, $include_type = 'include', $relative_uri = "..")
	{
		$ignore_files = array( 'cgi-bin', '.', '..' );
	    $dir = opendir($relative_uri);
 
	    while(false !== ($file = readdir($dir)))
	    {
	    	if (! in_array($file, $ignore_files))
	    	{
				if(is_dir("$relative_uri/$file"))
				{
					self::include_file($name, "$relative_uri/$file");
				}
				elseif (!is_dir("$relative_uri/$file"))
				{
					if ($file == $name)
					{
						if ($include_type == 'include')
						{
							include("$relative_uri/$file");
						}
						else if ($include_type == 'include_once')
						{
							include_once("$relative_uri/$file");
						}
						else if ($include_type == 'require')
						{
							require("$relative_uri/$file");
						}
						else if ($include_type == 'require_once')
						{
							require_once("$relative_uri/$file");
						}
						closedir($dir);
						return true;
					}
				}
	    	}
		}
		@closedir( $dir );
		return false;
	}

Here’s an example of how to use the function:

// Basic usage
include_file('test.php');
 
// Require instead of include
include_file('test.php', 'require');
 
//Recursive search in a particular folder
include_file('test.php', 'include', 'app_lib');

Something to note in the $relative_uri parameter we have a default string of ‘..’. This could be the path to the root of the project such as ‘c:\…’ or ‘/etc/…’ (depending on what operating system you are using). That will force the default of that parameter to recursively search your entire application structure.

Use this function with care as it does cost some overhead scanning for files. However I’ve found this to work perfectly for smaller applications that don’t have millions of users connecting to it.

PC World put out an article talking about the new Motorola Android phone touting a 2.4 GHZ processor. That is more than double the speed of any smart phone on the market to date. This opens the door for many possibilities of creating programs that were impossible to build up to the standard of a regular PC. Take for instance Microsoft Office. Sure there are office programs on iPhone and Android but really none of them even remotely compare to the capabilities of the full fledged Microsoft Office suite. Current smart phones just don’t have the multithreading capabilities or the speed needed to really accomplish this and still have a good user experience. Most of these applications have to be ‘dumbed down’ for it to be able to run on a smart phone. With these new found power there are many more possibilities for creating more powerful rich applications

Yay, Utopia is here!

Well, not really. One of the things that is still in its infancy is human interaction. Sure we’ve come a very long way with our touch capabilities and how we interact with interfaces, but there are a couple limitations. Number one is size. If you want a small pocket size device, as of right now you get a small pocket size screen. We don’t have the futuristic holographic imaging bursting forth from the tiny little device yet. Right now, if the device is small, the screen is small. There’s a lot that can be viewed small and a lot that may not be as desirable to be viewed as small. Number two would be that human interaction isn’t always the same with touch screens. I have friends who have smart devices and they have ‘mastered’ the touch screen and type very fluently, however, not as fast as on a regular old keyboard. It amazes me that after decades of computer evolution the one thing that hasn’t changed is the basic qwerty keyboard. It’s been around since before electric typewriters. Sure you may make them more ergonomic etc, but the foundation of the basic qwerty keyboard remains the same to this day as it always has and it doesn’t look like it’s going to go away anytime soon. So to be productive on these devices as we’ve been on desktops or laptops we will need to continue to innovate on the human interaction aspect.

PHP has typically been partnered along side of MySQL as a DBMS for a long time now. While MySQL is very powerful PHP can utilize many other DBMS’s as well such as PostgresSQL, Oracle, and even Microsoft SQL. The problem is that connecting to and using the different databases means you have to load different drivers with different coding conventions. For a long time you couldn’t use the same code with Microsoft SQL as you did with MySQL. Not until PDO.

********************UPDATE***************************
I’ve changed a couple things in the class. First I’ve changed the get rowcount and get columncount to functions so they are run on demand instead of every time you execute a query. I’ve also added in a function to get the last inserted ID from an insert query which is very handy. I’ve also added in a function to close the opened cursor to free up memory and the function runs in the destruct magic method.
*****************************************************

Read the rest of this entry »

 
June 10th, 2010
 

I was recently watching a show on my netflix ‘watch instantly’ called Fight Quest. While I’m no fighter myself, I appreciate the time, effort and dedication it takes to become an expert in a fighting style. If you’ve never seen the show before it’s about 2 American expert fighters traveling all over the world learning the history and fighting styles of all kinds of different cultures. Everything from French kickboxing to Brazilian knife and stick fighting to Israeli Crav Maga. The 2 men have 1 week to study with the best masters of the individual fighting style and then they are pitted against the best fighters for a 3 round fight. Something I noticed, was the incredible emphasis on mutual respect for each others fighting styles. While fighting is the most raw competition, there was always a sense of respect for the others abilities. It made me think of how nice it would be if that’s the way it was in the world of technology. Oh sure, we have great respect for those programmers who share our love for one particular language but how many programmers have you met that show open disdain for other programmers who may not do things the same way? Take for instance C# vs Java. Man, you want to have a knock-down-drag-em-out fight amongst nerds that’s a great way to start. I happen to be a programmer who uses many languages including C#. Does that mean I hate Java and Java developers? Not at all. Because I use a PC and Ubuntu does that mean I think that no one can possibly get any work done on a Mac? No, that would be asinine. And the same holds true the other way around. The problem is that instead of transcending beyond the technology and appreciating each one for it’s own merits we get caught up in the immature fighting of the corporate realm. Even though I may not know Java or Haskell or Ruby that well does not mean I don’t appreciate the technologies and respect the intelligence of the individual developers. And while I may have my own preferences I understand that they are just that ‘preferences’. Much of what I can do in the languages that I am familiar with other developers can do in languages they are familiar with. I love seeing the differences and learning about how other developers are doing things. Learning other languages opens your mind and grows you into a more well rounded programmer. So while yes, there is competition, lets not forget a key ingredient… respect.

Dr. Peter Grogono – Living with Concurrency from CUSEC on Vimeo.

I really enjoyed this lesson given by Dr. Grogono. Concurrency is very quickly becoming a bigger and bigger problem that must be solved as computers are being distributed with more and more cores. While at this stage of the game it is typically a problem for very large companies such as Google to solve because their the ones working with powerful $30,000 servers with 64+ cores, at the same time it won’t be long before those servers will come down in price and the computing demands placed upon ‘the rest of us’ will grow. No worries though, with brilliant minds like Dr. Grogono’s we are in good hands.

 
June 9th, 2010
 

One of the most common things any programmer will have to do is work with strings. Whether it be validating, checking value or sanitizing. Something I find helpful is to wrap functionality in classes so I can easily reuse them later. Here are a few string functions that I’ve found helpful. Don’t hesitate to comment with string functions you’ve found helpful!

class string
{
	/**
	 * Checks to see if string is only alphabetic
	 *
	 * @param string $value
	 * @param boolean $ignore_spaces
	 * @return boolean
	 */
 
	static function isAlpha($value, $ignore_spaces = false)
	{
		if (!isset($value))
		{
			return false;
		}
 
		if ($ignore_spaces)
		{
			$value = str_replace(" ", "", $value);
		}
 
		return ctype_alpha($value);
	}
 
	/**
	 * Checks to see if string only contains letters and numbers
	 *
	 * @param string $value
	 * @param boolean $ignore_spaces
	 * @return boolean
	 */
 
	static function isAlphaNum($value, $ignore_spaces = false)
	{
		if (!isset($value))
		{
			return false;
		}
 
		if ($ignore_spaces)
		{
			$value = str_replace(" ", "", $value);
		}
 
		return ctype_alnum($value);
	}
 
 
 
	/**
	 * Checks to see if a string is numeric
	 *
	 * @param string $value
	 * @param boolean $ignore_spaces
	 * @return boolean
	 */
 
	static function isNumeric($value, $ignore_spaces = false)
	{
		if (!isset($value))
		{
			return false;
		}
 
		if ($ignore_spaces)
		{
			$value = str_replace(" ", "", $value);
		}
 
		return ctype_digit($value);
	}
 
	/**
	 * Checks to see if string is a valid phone number
	 *
	 * @param string $value
	 * @return boolean
	 */
 
	static function isPhone($value)
	{
		if (!isset($value))
		{
			return false;
		}
 
		if(ereg("^[0-9]{3}-[0-9]{3}-[0-9]{4}$", $value) || 
		ereg("^\([0-9]{3}\)-[0-9]{3}-[0-9]{4}$", $value) ||
		ereg("^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$", $value) ||
		ereg("^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$", $value) ||
		ereg("^[0-9]{3}[0-9]{3}[0-9]{4}$", $value) ||
		ereg("^[0-9]{3} [0-9]{3} [0-9]{4}$", $value)
		) 
 
		{
			return true;
		}
		else 
		{
			return false;
		}
	}
 
	/**
	 * Checks to see if a string is an email
	 *
	 * @param string $value
	 * @param boolean $check_domain
	 * @return boolean
	 */
 
	public static function isEmail($value, $check_domain = false)
	{
		if (!isset($value))
		{
			return false;
		}
 
		if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $value)) 
		{
			if ($check_domain == true)
			{
				list($userName, $mailDomain) = split("@", $value);
 
				if (checkdnsrr($mailDomain, "MX")) {
				 	return true;
				}
				else 
				{
					return false;
				}
			}
		  return true;
		}
		else 
		{
		  return false;
		}
	}
 
	/**
	 * Checks to see if a string is a valid zip code
	 *
	 * @param string $value
	 * @param boolean $extended
	 * @return boolean
	 */
 
	static function isZip($value, $extended = false)
	{
		if (!isset($value))
		{
			return false;
		}
 
 
		if (!$extended)
		{
		  if(ereg("^[0-9]{5}$", $value))
		  {  
		  	return true;
		  }
		  else
		  {
		    return false;
		  }
		}
		else
		{
			if(ereg("^[0-9]{5}$", $value) || ereg("^[0-9]{5}-[0-9]{4}$", $value))
			  {  
			  	return true;
			  }
			  else
			  {
			    return false;
			  }
		}
	}
 
	/**
	 * Validates credit card number
	 *
	 * @param string $value
	 * @return boolean
	 */
 
	static function isCreditCard($value)
	{
		if (!isset($value))
		{
			return false;
		}
		if (ereg("(^(4|5)\d{3}-?\d{4}-?\d{4}-?\d{4}|(4|5)\d{15})|(^(6011)-?\d{4}-?\d{4}-?\d{4}|(6011)-?\d{12})|(^((3\d{3}))-\d{6}-\d{5}|^((3\d{14})))", $value))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
 
	/**
	 * Strips html out of a string
	 *
	 * @param string $value
	 */
 
	static function stripHTML(&$value)
	{
		if (!isset($value))
		{
			exit;
		}
 
		$breaks[] = "<br>";
		$breaks[] = "<br />";
		$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
               '@<style[^>]*?>.*?</style>@siU'    // Strip style tags properly
				);
 
		$value = preg_replace($search, '', $value);
		$value = str_ireplace($breaks, "\r\n", $value);
		$value = trim($value);
		$value = strip_tags($value);
		$value = html_entity_decode($value, ENT_QUOTES);
		$value = addslashes($value);
	}
 
	static function cleanHTML(&$value)
	{
		$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
               '@<style[^>]*?>.*?</style>@siU'    // Strip style tags properly
				);
		$value = preg_replace($search, '', $value);
		$value = htmlentities($value);
	}
}
 
June 9th, 2010
 

Most PHP developers will check to see if a page request method is ‘POST’ by checking to see if an input exists by isset() function and if the value of that input is null or not. While this works a better method of checking the method is:

// you can check: 'GET', 'HEAD', 'POST', 'PUT'
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    //do something
}

This requires less overhead, is a bit more elegant and more reliable.

« Previous Entries
cheap software