Monday, May 25, 2009

Moved to wordpress



Sorry can't deal with blogger any more.


Hashmap based on values of a key based array

I recently had to create a function that would take the values in a key based array and create a hashmap based off of the values. Basically, I had to switch the keys and values. (Remember there are no duplicate keys and I was going to need multiple objects associated with 1 key..this was all part of my free php job search script ) . Anyway, the goal was to take an array like this


$before = array(
"White Plains" => "NY",
"New York City" => "NY",
"Philadelphia" =>"PA",
"Tampa" =>"FL",
"Miami" =>"FL"
);

and turn it into an array like this

$after = array('NY' => White Plains,New York City,
'PA' => Philadelphia,
'FL' => Tampa,Miami
)

Here's the function...enjoy
before = array( "White Plains" => "NY", "New York City" => "NY", "Philadelphia" =>"PA", "Tampa"   =>"FL", "Miami"   =>"FL" );  function createHashMap(&$mc){ $hashMap = array(); while ($name = current($mc)) {   $key = key($mc);   //echo "key: $key , current:  ". $name . '
'; if(isset($name,$hashMap)){ $hashMap["'". $name ."'"] .= $key .',' ; } else{ $hashMap["'". $name ."'"] = "'". trim($key) ."'" ; } next($mc); } //print em out foreach($hashMap as $key => $value) echo $key . ' => ' . $value . '
'; } createHashMap($before);



Sunday, May 24, 2009

Php Job Script : Free Job Aggregator

I have moved my blog to wordpress...can't deal with blogger's limitations anymore.

The new URL for my Free PHP Job Search script

One of my clients wanted to me to create a php job script--not a job portal but rather a script that would allow people to search for all of the jobs that you find on monster.com, indeed.com and various other sources.

So I made up this script (I only encrypted a few lines...that make sure that credit). Otherwise it is highly customizable and is SEO friendly as the entire script employs mod_rewrite.

Here's an example of a site that is using this free job search engine script at find tutoring jobs.

Saturday, April 18, 2009

PHP Sorting Parallel Arrays

This weekend I put the finishing touches on a zip code search program for Hubalub private tutors. The goal was for parents to be able to find all tutors within a given radius of a given zip code. Well, after brushing up on some of the fun non-Euclidean geometry necessary to calculate distance on a sphere (check out the Haversine formula at wikepedia), I found myself dealing with the following dilemna. I had about 8 parallel arrays representing the username, cityname and other personal details of the members who were within the radius of a given zip code.

Below is simplified snapshot of the arrays in question.
$userNames =array();
$userAvatars = array();
$distances = array();

Naturally, I wanted to display the tutors by distance in descending order so that you'd see the closest tutor first. No problem right, just use one of PHP's handy sorting functions like ksort() or krsort(), but if you ksort() an array, you've lost the whole 'parallelism' of the arrays! After toying with different possible solutions, the easiest one seemed to be to create a hashmap in which the distances served as keys and then concatenated all the user details into 1 long comma separated string (which could be exploded later). This allowed me to ksort() the array based on the key which was the distance! And Bam! user data is now in descending order based on distance. The only thing to be careful of is that if two distances were the same (ie duplicate keys) you'd end up with an array of buckets that you'd to loop over.

Saturday, March 7, 2009

Hashing Passwords: Md5 or SHA1

There was recently thread at sitepoint.com about the securing website passwords with 1 hashing (md5 or sha1). Someone was worried about the fact that rainbow tables like programs existed and could often get the original password from the md5. In the end, the person who started the thread and was worried that his 1 way md5 hashing was ineffective did not take into account the fact the rainbow tables are not considering the 'salt' that the said webmaster was (hopefully) with his password hashing.

Even more interesting and actually educational for me personally was the fact that combining hashing such as sha1(md5($password)) actually apparently increases the chances for collisions. I didn't realize this and had in fact gotten that very idea from some open source code I had been using (some forum software)...

Thursday, January 1, 2009

How to import CSV into MySQL table: Step by Step Instructions with Pictures

Just show me the steps dude

I recently had to create a zip code locator program for someone's website that I was designing. This site was for parents/guardians/students to be able to find a tutor. Basically, hubalub.com is a tutoring service, and one that is, I believe, unique from most of the other tutoring services out there. That aside, never having written a script like that before, I soon found out that you get the latitude and longitude of a given zip code and find all zip codes with a given radius. In another post, I'll describe some optimizations that you definitely want to pursue to implement this--let's put it this way: the database of zip codes has about 42,000 rows! And you don't want to query 42,000 rows ! Anyway, to get the zip codes and their associated data like county name etc.. you 're going to need to download a zip code database. There are several free and commercial ones available. Just do a google search for them. If like me, you get a humongous CSV text file with all of this data, you'll want to know how to get all the data from this CSV into a MySQL table. And PhpMyAdmin comes to the rescue:

Steps to Import CSV into MySQL table


  • Step 1) Create a table that has the necessary number of columns. If the CSV has 10 types of data, you'll need 10 columns.
  • Step 2) After having created you MySQL table, log into phpMyAdmin and click on this table.I had a table called `morris_zipcodes`.
  • Step 3) Click on the 'import ' (For this step you can refer to the picture below






  • Step 4) You should now be looking at something like the next picture. I circled in red the important parts of the form.



  • Step 5) Click on the 'csv' radio button. You will then see a fields that have to do with your CSV is formatted. Since my CSV's fields were not separated by tabs I entered '\t' as you can see above (no quotes). My fields were not enclosed by quotes so I removed the default quotation marks for 'Fields enclosed by'. I left the fields escaped by in its default form (didn't need to worry about escaping). And lastly, since my lines were terminated by a new line I entered the '\n' for the 'lines terminated by' field.
  • Step 6) I browsed to the massive text file, uploaded it and was soon happily greated by this message : "Import has been successfully finished, 79948 queries executed." WOw that's a lot of queries. I think it took about a minute or two, for MySQL to create all of those rows, not bad!

Saturday, December 20, 2008

Searching for Social Network Script: Part I

I was recently asked to develop a custom script for a private tutoring company. I decided that the requirements would best be suited by first obtaining a social networking engine and then doing some heavy modding. So the first question was: what's the best social networking script out there: a seemingly simple choice. I mean I've shopped around for scripts before--it wasn't too hard, for instance, for me to decide that I like Simple Machines Forum much more than phpbb.

However, it took me literally about 15 to 20 hours of research to come to a decision on which social networking script was right for me. There are several scripts to choose from: Boonex, Dzoic, phpFox, abledating (aparrently a scam),abk-soft(also apparently a scam) and others.(screenshots of abledating from a disgruntled purchaser)

After a few hours of reading social network script reviews in various forums and on many sites, I found myself utterly confused. How could the same script (be it phpFox or abledating) get such positive reviews by one website and such horribly negative reviews by another? As it turns out, there is cut throat competition among the scripts, some have referred to this as the 'script wars' or the 'dating script wars' since these social network engines often are used for the dating sites that seem to be ubiquitous on the internet. It soon became obvious to me (and others from the comments I read on webmaster forums: 1 , 2 ), that the big players in the social network script wars are spamming the internet with positive reviews about themselves and negative reviews about their competition.

If, like me, you find yourself reading forum posts by people who either absolutely love or absolutely loathe a particular script. You have to ask yourself are these people spammers who wrote the script that they love?

Suggestions reading reviews about any social networking script





  • Does the review have any affiliation with the script?
  1. For instance The site :http://www.abledatingreview.com provides very positive reviews for the able dating software (aka abk software). My hunch is that http://www.abledatingreview.com is just a fake site created by the very people who make the abledating software suite.
  • Regarding posts made in forums like DigitalPoint or SitePoint, you have to try to become a Private Investigator of sorts. Your goal is to determine how credible the post is becuase you'll read lots of questionable reviews. : If you read a forum post, pay close attention to
  1. Is the user who wrote the post banned? (sounds obvious, but I had never trained myself to look at whether the forum poster was banned, and several of the reviews were written by people who were banned)
  2. How many posts has the user made? And what do the other posts of this user say? If someone on a forum, has only made 4 posts which are positive about 1 particular script and negative about all the others--and this sort of spam is exactly what you'll find on the forums-- well, I think you can connect the dots about the credibility of that user's reviews!


Other factors you might want to consider before purchasing a social network/dating script


  1. Real world example sites: Can you find real examples of sites that use the software? One of the reasons I went with phpfox is that I found several sites that use the software, and those sites loaded quickly and looked good. (Just do a search 'powered by phpfox') I couldn't find any sites powered by abledating, abk-soft, another strike against that company. I did find some sites that use boonex such as http://www.4ppl.com/ .
  2. Is the source code encoded? Phpfox is not. Boonex is not. I can't remember about Dzoic but I read that abledating is encoded. (again, research on your own about the last two, as I can't remember at this point) If the source coded is encoded, you better like the way the script works out of the box or be willing to pay the script creators to do any mods.
  3. Mod Community: If you are not a programmer, you probably want to research whether or not there's an active community of people who you can pay to mod the script. I think Boonex has that. PhpFox also has an active community of modders
  4. Quality of Code: If you are a programmer (and even if you're not), you want to have well designed code and a well-architectured script. Being a programmer and the guy who was going to go under the hood and customize whatever script we went with, my biggest fear was that I would lay out $400 bucks or so and then get completely crappy code. On this front, I can only speak to phpfox's code: it is indeed very well written with a sophisticated architecture. It uses OOP, smarty templates and a highly modularized, template-based system that, at first, might seem overwhelming, but soon enough is obviously powerful in the flexibility that it gives programmers. They've done a great job separating code from design. Everything is object oriented! (yea!). If you're thinking about using boonex, you should be able to see its source code since you can get a free version. If it's not a free script like Boonex you, unfortunately, probably have to cross your fingers and wait to see what the code that you purchased looks like!

After I spent a good deal of time trying to find the best social network script, I decided to go with PhPfox a company that I have no other affiliation with (other than being a satisfied customer). That said, please remember that PhpFox is the only social networking script that I have athorough and direct knowledge of. For all that I know, Dzoic or SocialEngine might be great. What I would urge you is to take the advice in this post. If you don't, you may end up like some of the people on digitalpoint who were very angry that they laid out several hundred dollars for a partially functional and encoded script from a company that did not offer refunds or technical support.