Tuesday, April 8, 2008

Real Cool Perl

A friend on mine posted this Perl tidbit in IRC, so I had to pass it along:

(9:23:00 AM) ghost: $name =~ s/([\W\-])?([^\s\-]+)/join('', $1||'', ucfirst(lc($2)))/ge
(9:25:02 AM) ghost: that takes a sentence in all caps.. "SUCH AS THIS" and turns it into "Such As This"

NICE... for sure.

Monday, October 15, 2007

Google Maps

Having written my first application with the Google Maps API, I must say that it went fairly smoothly, using PHP as my database interface and Ajax calls to update data within the page works nicely. If you have not tried this yet, there are a zillion reasons to aggregate data available on the web and put it on a map. My project is a nationwide map of CCH-unfriendly stores.

Labels: , ,

Tuesday, July 17, 2007

Open Laszlo

I have really fallen in LOVE with Open Laszlo recently. It is a fantastic language that will interface nicely with php server requests to provide an outstanding user experience. In 85 lines of code I wrote a small website package with content tabs, animation, database interaction, and form interaction! Open Laszlo applications compile to Flash 7 or 8 and you just toss them into a HTML wrapper and you are good to go.

For anyone who wants to provide a much better user experience that AJAX alone can provide, taking a look at Open Laszlo very attractive.

Tuesday, March 27, 2007

WAP for BlackBerry on Apache

This is slightly off the topic, but I will figure out a way to tie it in.

You have a webhost with Apache and allowing .htaccess files... how can I make WAP friendly pages to display?

1. Create a .htaccess file and upload it to your WAP subdomain or directory!


Options +Indexes
AddType audio/adpcm adp
AddType text/vnd.sun.j2me.app-descriptor jad
AddType application/java-archive jar
AddType text/vnd.wap.wml wml
AddType application/vnd.wap.wmlc wmlc
AddType image/vnd.wap.wbmp wbmp
AddType text/vnd.wap.wmlscript wmls
AddType application/vnd.wap.wmlscriptc wmlsc
AddType application/vnd.wap.xhtml+xml xhtml


This should handle all the file types listed. The adp will handle ringtones converted from mp3 to adp for blackberry. Download and save as a ringtone!

jar and jad will handle install java apps. Be sure to upload the .cod file also.

wml, wmlc, wbmp, wmls, wmlsc, xhtml are the pages for your WAP browser.

2. I want to redirect depending on if it is a WAP browser or a "real" browser.

Good News, here is some PHP code that does it written by Anurag Singh:


//Description
//Do not remove the below code. This will lead to violation of Terms Of Use
/****************************************************************************
This script is written by Anurag Singh
Title: Wap Redirect
Version: 1.0
Email: tom.anu007@gmail.com,
Homepage: http://tom.anu007.googlepages.com/wapredirect
Special thanks to Harald Hope, Website: http://techpatterns.com/
And also to Joseph George Jacobs , Email joe@spitzbucket.com
****************************************************************************/
//Configuration
global $userBrowser;

// Your wapsite
//enter the link you want user to be redirected in case of WML browser
$wmllink = "http://backalley.net/wap/wap.xhtml";

// Your website
//enter the link you want user to be redirected in case of PC browser
$htmllink = "http://backalley.net/wap/pc.html";

//There is no need to edit any information from this point on
//however in case if you want to contribute please do so
//please be sure to let me know about the changes
//Detect the browser
$userBrowser = $_SERVER['HTTP_USER_AGENT'];

//Remove the below // qutoes.
//It helps to check if there is any error when editing
//If everything is all right it will display the browser information
//please remember to put // back after testing or no redirection will occur
//echo $userBrowser;

//Check for Windows CE
if(stristr($userBrowser, 'Windows CE'))
{
$ub = "WML";
}
//Check for Mozilla
elseif(stristr($userBrowser, 'Mozilla'))
{
$ub="PC";
}
//Check for Mozilla
elseif(stristr($userBrowser, 'gecko'))
{
$ub="PC";
}
//Check for opera
elseif(stristr($userBrowser, 'opera'))
{
$ub="PC";
}
//Check for omniweb
elseif(stristr($userBrowser, 'omniweb'))
{
$ub="PC";
}
//Check for msie
elseif(stristr($userBrowser, 'msie'))
{
$ub="PC";
}
//Check for konqueror
elseif(stristr($userBrowser, 'konqueror'))
{
$ub="PC";
}
//Check for safari
elseif(stristr($userBrowser, 'safari'))
{
$ub="PC";
}
//Check for netpositive
elseif(stristr($userBrowser, 'netpositive'))
{
$ub="PC";
}
//Check for lynx
elseif(stristr($userBrowser, 'lynx'))
{
$ub="PC";
}
//Check for elinks
elseif(stristr($userBrowser, 'elinks'))
{
$ub="PC";
}
//Check for Mozilla
elseif(stristr($userBrowser, 'Mozilla'))
{
$ub="PC";
}
//Check for links
elseif(stristr($userBrowser, 'links'))
{
$ub="PC";
}
//Check for w3m
elseif(stristr($userBrowser, 'w3m'))
{
$ub="PC";
}
//Check for webtv
elseif(stristr($userBrowser, 'webtv'))
{
$ub="PC";
}
//Check for amaya
elseif(stristr($userBrowser, 'amaya'))
{
$ub="PC";
}
//Check for dillo
elseif(stristr($userBrowser, 'dillo'))
{
$ub="PC";
}
//Check for ibrowse
elseif(stristr($userBrowser, 'ibrowse'))
{
$ub="PC";
}
//Check for icab
elseif(stristr($userBrowser, 'icab'))
{
$ub="PC";
}
//Check for crazy browser
elseif(stristr($userBrowser, 'crazy browser'))
{
$ub="PC";
}
//Check for IE
elseif(stristr($userBrowser, 'internet explorer'))
{
$ub="PC";
}
//If it's not any of the above browsers
//Then it gotta be a WML or very unpopular browser
else
{
$ub="WML";
}
//Code for redirecting based upon the results
if($ub == "PC") {
header("Location: ".$htmllink);
exit;
}
else
{
header("Location: ".$wmllink);
exit;
}

?>


Now you can host your WAP site on the same server as your normal website with no problems! (As long as your webhost allows .htaccess files of this nature)

Labels: , , ,

Recursion

Several people have asked me for reasons to use recursion:

bengalpedigrees.com is a project I volunteer server and programming on. It is pretty standard family tree record keeping and outputting of pedigrees...

why use recursion on this?

For one, you write one display function, then a pedigree of any depth can be called by the same function with a little math to determine which row you are currently displaying to place table cells properly.

I five generation pedigree has 2+4+8+16+32 for a total of 62 cells. Who the heck wants to hand code a page of that mess?

This means that the website is capable of outputting 1 row, or ALL the rows, based on a single function and parameter! A tiny amount of code that provides quite a bit of functionality and makes for easy to maintain.

So, anyone else have another example for a good use of recursion?

Labels: , ,

Wednesday, January 17, 2007

Recursive Programming

Recursion is the nemesis of many new programmers, and done improperly, the result is a slow and incorrect outcome. Done properly, very few lines of code can solve huge recursive problems. Let's take an example.

A classic example is computing factorials: 3! = 3*2*1

Lets write a function:



function factorial($number) {
if ($number == 1) return 1;
else return $number * factorial($number-1);
}

echo "Factorial 5 = " . factorial(5);




Example: http://backalley.net/factorial.php

The most important part of this function is the return 1 statement. Without it, you would have endless loop. This is the most common mistake for new programmers.

To create a recursive function we MUST do the following:

1. Initialize! This is often simply checking for a valid parameter. Modify this function to check for $number > 0 or return an error (-1).

2. Check for the base case. Here, if the number is 1, we return 1.

3. Redefine the answer as a set of smaller problems.

4. Run algorithm on sub-problems.

5. Combine and return the results.

Recursion is useful for mathematical functions, linked lists, array traversals, string searching, and many other more complex operations such as trees.

When you are ready to move beyond the world of simple loops, think of a recursive project and give it a shot!

Labels: , , ,

New Blog

Time to give back something to the community and cover some PHP topics which I do not see very many tutorials for. Check back, they are coming soon.