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!