php sprintf + sql like

Sometimes I do my best programming when I’m tired. Don’t ask me why, I just do. It’s a skill I picked up in college.

Being tired and producing good code wasn’t the case last night. I was trying to use the sprintf PHP function with a SQL “Like” statement. I made some dumb mistakes that tripped me up for a while. Hopefully someone out there will find this post and help them not make the problems I made, sleepy or not!

Normally the LIKE is used in mysql like this:
SELECT name FROM users WHERE name LIKE 'J%';

That would get all names including: Josh, Jason, Jimi, etc.
In a SQL Like statement the % is a wild card, so the command is to match everything starting with “J”

Now when you use the sprintf() function it looks kind like this:
$query = sprintf("SELECT name FROM users WHERE name='%s'", $searchString);
The %s will be replaced with the value of $searchString

Trying to combined them is where I had some problems….

At first I tried to do something like this:
$query = sprintf("SELECT name FROM users WHERE name LIKE'%s'", $searchString);
didnt return what I was looking for at all, it had no wild cards in it!

Then I tried this:
$query = sprintf("SELECT name FROM users WHERE name LIKE'%s%'", $searchString);
didnt work either, this time it threw errors

But this worked great
$query =
sprintf("SELECT name FROM users WHERE name LIKE '%s'", $searchString . "%");

So the moral of the story is, if you want to use a SQL Like statement, appent the wildcard for the Like statement to the string to be inserted by the sprintf funtion.

Mac on my PC – LEO4ALL

In my last blog post I talked about how my computer had a system drive failure. I am waiting for Western Digital to send me a new 10,000 rpm drive to replace the broken one, so in the meantime, I thought I would screw around with trying to put Mac OS X on my desktop.

My friend Luis Majano is a great software developer and swears by his Mac Book Pro. At work I run Windows XP, at home it Windows Vista. I have Ubuntu on my laptop and run CentOS on my web servers, so I’m not a die hard about one OS or another, they all have their place.

I love Linux operating systems, so learning from Luis that Mac OS X sits on top of BSD made me more interested in switching (Apple don’t tell you that in their cute commercials). The price of Mac computers is insane though, and not something I’m blindly going to jump into.

So to the point… a broken PC a spare harddrive, and the want to try Mac OS X, whats a geek to do? A few google searches, and a torrent download later, I had in hand, Leo4All.

Leo4All is an awesome distribution of the hacked apple OS to run on none genuine apple hardware. They even have a great wiki (http://osx86leo4all.wikidot.com)

I dropped the DVD into my drive, booted up and a few minutes later I was in the OS X installer. Formatted the drive into an apple format, clicked install and 10 minutes later I was working inside of OS X! everything was there, even time machine! check out the screen shot below…

I had trouble with my network card, as OS X doesnt seem to like a lot of on-mother-board devices. I fixed that by powering down, and installing an old pci NIC. Booted back up and it was there!

I had no audio, but after a few minutes of googeling around, and following likes from the Leo4All wiki, I had it going.

I still havent had any luck getting my dual monitors to work. OS X doesnt seem to like nVidia cards with 512 megs of ram. Oh well, one monitor is fine with me for now.

The USB ports work, and recognize my iPod and iPhone just fine.

So it looks like I’m set. If the experience goes well, who knows, I just might become a switcher! If you know of some sweet mac software I need to try out, let me know.

Improving CodeIgniters View Handling

codeigniter logo

Please be aware that I no longer use this method. Please follow this link to see how I handle layout with Codeigniter

If you have ever worked with an MVC framework for web developent, you know that the “V” stands for “view”.

At work, I program in coldfusion, and use the coldbox framework, and I love it. At home, I write PHP code, with the codeigniter framework.

I love codeigniter, but I think that its biggest weakness is the view handeling, all thought codeigniter had taken strides to improve it. At the time of writing this, codeigniter is at version 1.6.1, and allows multiple views to be loaded at one time.

example:

<?php
 
class Page extends Controller {
 
function index()
 {
 $data['page_title'] = 'Your title';
 $this->load->view('header');
 $this->load->view('menu');
 $this->load->view('content', $data);
 $this->load->view('footer');
 }
 
}
 ?>

This is great with one exception, codeigniter simple builds a stack of the view results, appending them to each other.

This means that in header.php, loaded at the start of the view sequence, we would have to opening tags, such as <body>, and then in the last view, footer, we have to close the tags we opened in header.php, in this case, </body>

I don’t know about you, but opening tags in open file, and depending on another file to close them is not a good practice. Using a MVC setup and then doing something like this is very counter intuitive.

To fix this, codeigniter needs to support layouts, as well as views.

A layout is a file that contains the framework for a page, and the views are included and rendered inside the layout. basically filling out the content of the page. This also leaves your code the ability to be more flexible. Your views are pluggable components that don’t care about the layout at all.

One of the reasons I love open source software is the fact that the community will fix weaknesses is the software. Looking at the codeigniter wiki, I came across the “view object” (http://codeigniter.com/wiki/View_Object/)

The view object is a great solution for adding layouts to the codeigniter framework.
Here is a code sample of how to use the view object in a controller:

$this->load->library('view');         // or autoload
 
$this->view->layout = 'admin/layout';
 
$this->view->data(array(              // set the view data
 'privileges' => $privileges,
 'catcode'    => -1,
 'page'       => $page,
 ));
 
$this->view->load(array(             // load the page partials
 'header'     => 'header',
 'menu'       => 'menu',
 'content'    => 'admin/'.$page,
 'footer'     => 'footer',
 ));
 
$this->view->render();               // create the view or

inside the layout file (admin/layout.php)

<? $header->render(); ?>
 
<body>
 <? $menu->render(); ?>
 <div id="mainContent">
 <? $content->render(); ?>
 </div>
 </body>
 
<? $footer->render(); ?>

You can see that the layout file contains the framework of the page, freeing up the views to be individual pluggable items that can be used across your codeigniter application.

I hope that the codeigniter team takes note of the view object and adds it to the core for codeignier 1.7 or maybe even sooner!

LifeStreaming Is Simple As Pie

Its not secret, I love social networking, I cant get enough of it. I also love programming and anything internet related.

I’m not sure how I came across it, but a PHP based, Object Oriented RSS caching tool named SimplePie caught my attention.

Previously I had been using a tool called Last RSS, but I found that Last RSS could not handle ATOM feeds.

Digging into how SimplePie works, I found that I could merge several feeds together, sorted by post time, my first thought was to use this to aggregate and cache all the different music related news feeds that and going to add content to notpopular.com v2.5 (when ever I finish it). I started thinking about it a bit more and thought it would be cool to use SimplePie to mash together all of the different RSS feeds from the different social networks I am on.

It wasn’t that hard to use SimplePie and the various social networks to make something really cool.


//establish the feeds
$pownce = new SimplePie("$pownceRSS_url");
$flickr = new SimplePie("$flickrRSS_url");
$twitter = new SimplePie("$twitterRSS_url");
$digg = new SimplePie("$diggRSS_url");
$youtube = new SimplePie("$youtubeRSS_url");


//merge them all together!
$merged = SimplePie::merge_items(array($pownce, $flickr, $twitter, $digg, $youtube));

Then all you have to do is look over each item in $merged, and output the appropriate info you want.

You can see what I built over at www.JoshHighland.com

One thing I really found great was the API that was on the SimplePie site.

After I put this all together, I found out that there is a term for what I had just built. People are calling them “LifeStreams“, which is a very appropriate term. I thought I had invented something, but guess LifeStreaming is like fire or a spear, given enough time people all over the place will discover it on their own.

I encourage you do set up a LifeStream for yourself and post the URL in the comments below. It’s fun!

My LifeStream: www.JoshHighland.com

UPDATE (10/29/08) :
I was having trouble with my feed from twitter dying after a while. It would only fetch it once, then nothing.I found the answer to my problem on the simplePie blog, http://simplepie.org/blog/2008/08/16/twitter-bug/. I followed the instructions and commented out the If statement on lines 1583-1586, and the twitter feed started to work again. I hope that twitter updates their RSS service so hacks like this aren’t needed.

WordPress 2.5 image upload problem : SOLVED

This afternoon I upgraded my blog to the latest version of wordpress, 2.5. The install went great and it was simple to do. I didn’t realize that I had any problems until I went to make a blog post and upload an image. When I tried to upload, I was greeted with the following:

Something was for sure messed it. I tried it in several browsers, but had the same results. no luck. I started to go some searching online but since wordpress 2.5 is so new, there was little info to be found. After about an hour of stumbeling around I came across a post on a forum where someone said they got the image upload to work by adjusting their .htaccess file on thier site with the following code.

<IfModule mod_security.c>
<Files async-upload.php>
SecFilterEngine Off
SecFilterScanPOST Off
</Files>
</IfModule>

I made the change to my .htaccess file, and like magic the image upload feature started to work again!

I am making this post in the hopes that other people with similar problems will stumble across this post and be able to fix their problem with out having to wade through all the pages of people trying to diagnose what the problem is when the solution is super easy.