JoeDag32

PHP multi word search

I was building a search for a site I was working on and needed the search to work on the words a user might type in separately not as one string. For instance, if they typed: “wiffle ball in Louisville”, it would search: wiffle, ball, and Louisville.

Building my SELECT statement was a little tricky at first.

First I had to take my string that’s being passed and store each word in an array. The below is a very simple example, I highly recommend that you clean up the string first!

$searchString = $_POST['who-search'];
$searchArray = explode(" ", $searchString);

This takes our string and uses the explode function to break up our string into an array whenever whitespace is found. Again, this should be refined a bit more in a production level product.

Now that we have an array of the words that were entered, a way to work this array into the SQL statement had to be figured out. Using a couple of loops to go through the array and create OR’s did the job.

$sql = "SELECT DISTINCT * FROM table WHERE ("; 
 
while (list($key,$val) = each($searchArray)) {
	$val = addslashes($val);
	if ($val<>" " and strlen($val) > 0) {
		$sql .= "column1 LIKE '%$val%' OR column2 LIKE '%$val%'  OR column3 LIKE '%$val%') OR";
	}
}
$sql = substr($sql,0,(strLen($sql)-3));//this will eat the last OR
$sql .= ") ORDER BY column1 DESC";

The while loop goes through our array the addslashes function is called to the current value. This escapes apostrophes from throwing off the SQL statement.

If the current value isn’t empty then it’s inserted in the SQL and it’s value compared to the columns you want to compare it against in your SQL. In my example above I’ve used LIKE against 3 columns.

Finally you have to remove the final OR from the SQL statement. A simple substr function is used to remove it from the end.

LOST Promo Contest

At work every morning after a new episode of LOST airs we all gather near my desk and discuss what we thought of the previous nights episode of LOST. It’s actually pretty fun, and quite funny just how involved we have all gotten with the show.

Recently we heard of a fan promo contest to create a 35 second commercial for the upcoming finale. So a group of us decided why not?

We reviewed the official rules, and viewed what had already been submitted. I then printed out some storyboards for us to fill out with our ideas. We all had some great ideas, but they all seemed to have one connection, and that was how this show has brought so many different people like our office together.

Working on this was a lot of fun. I got to work with people I never work directly with, but happen to work a few feet from. We spent just a little bit of time on it, but think it turned out pretty nice.

Eating with half a numb face

I had an dental appointment to replace a filling for a crack I had in a molar that was done when I was in high school. Going to the dentist was never too bad for me, and I was pretty much in and out in under 45 minutes (way to go Dr. Josh).

However, what I wasn’t expecting was for half of my face to be numb for 4 hours. I half to admit, it’s a very strange feeling. Eating pizza a little bit afterwards turned into quite the epic adventure.

I’ve been known to be a little too rough at times, so I was extremely cautious to make sure I wasn’t accidentally biting into the right side of my cheek while eating. Small bites that were only being chewed on the left side of my mouth was the recipe for success.

2 slices felt like 4, with only half of my jaw doing any substantial work. All in all the whole numbing thing has turned out to be a pretty interesting experience.

Copyright © 2007 - 2010 joedag32.com