Tag Archives : PHP

Love Ruby, Hate Rails, but still use it — or Ruby vs PHP 11

Okay, that title was a bit extreme, I don’t really hate Rails. I don’t get why people feel the need to be so completely polarized about a framework, or a language. It’s either the best thing since sliced bread, or the worst, and nowhere in between. Still, most developers who aren’t Ruby purists, can tend to have a love hate relationship with Rails. Rails is easy to develop with, full stop. It’s practically impossible for you not to push out an app in AT LEAST 1/4 of the time it would take in straight up PHP. The problem is, PHP and Rails are apples and oranges, they aren’t the same thing, Rails is a framework built with the Ruby language, PHP is just a language. If you want a comparison, try CakePHP and Rails. Saying that Rails isn’t as fast as PHP is kind of mind boggling. Rails is slow, and so is ruby, and it will never be as fast as PHP or faster, period. Unless PHP becomes abandoned, which doesn’t seem likely. PHP is 100% geared towards a Server Side Scripting language, from the ground up, it’s written directly into the PHP parser to handle HTML with embedded PHP tags. Ruby is a general purpose scripting language with addon modules for parsing ruby code in HTML, like ERB. Regardless of this, you’d barely notice how much slower it is in reality because at this point, it’s very much a matter of hundredths of a second difference (probably). That barely even matters in large […]

Spree Commerce, eBay Trading API, and the eBay Accelerator Toolkit from Intradesys EbatNs

In this bit, I talk about using the EbatNs SDK for the eBay Trading API, to list items from your Spree Commerce store, or really any rails application. Filled with production code examples from an honest to god real live and working spree site that interfaces with the eBay trading API to Add, Revise, and End items, on ebay when they sell in your store, or on your store when they sell on eBay.

HowTo Knock it off with crazy MYSQL queries and the qTranslate plugin unistall

We’ve all done it. Either we learn some tricks in MYSQL, or read a cool post about some obscure programming trick you can do with SQL and decide to try it in a pinch on a live database. Just don’t do it. 9 times out of 10 you have access to SSH, and PHP, and you can write a script that will do what you want. You can also “test” it on a small set of inputs and have it echo out the necessary queries to the console instead of sending them to MYSQL. This is called the poor man’s unit test, or poor man’s BDD. Now, on one word press site, before the whole wordpress MU thing, we made the mistake of using qTranslate, well, it’s not evil, it’s just not good, so we wanted to stop using it, however, there’s no real way out of it. Search around and you find this post on their boards DON’T YOU EVEN DARE DO THIS. It’s just bad programming. You will probably hose your database doing this. Instead, let’s look at a more sensible solution, using your favorite scripting language, Ruby or PHP will do fine, I’ll choose PHP. $connection = mysql_connect(‘mysql.yoursite.com’,’user’,’pass’); mysql_select_db(‘yourdb’); $prefix = ‘your_table_prefix’; $res = mysql_query(“select * from {$prefix}posts limit 5″); //limit for testing $wanted_language = ‘en’; function split_text($text,$quicktags = true) { //The following code is kluged from the actual qTranslate Plugin $split_regex = “#(<!–[^-]*–>|\[:[a-z]{2}\])#ism”; $current_language = “”; $result = array(); $blocks = preg_split($split_regex, $text, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE); foreach($blocks as $block) { if(preg_match(“#^<!–:([a-z]{2})–>$#ism”, $block, […]

Firefox Content Encoding Error, PHP Website

I looked around online, and couldn’t find any solutions to this problem, so here is one possibility for you. I have a client with an intranet website with a custom MVC framework in PHP. In one of the controller actions, there was an echo statement. Normally, this shouldn’t really affect it, i.e. when you start echoing things, PHP normally sends the start of output headers and just plugs along. Everything worked fine awhile around, but then someone upgraded the server, and put the latest PHP there. Now those pages were broken, giving the Content Encoding Error. Removing the echo statements that were coming before the start of the page got rid of the error.

Simple PHP closure/lamda/anonymous function usage.

This is a fun little example of the new Closure features in php. class TArray { public $pMembers = array(); public $pCurrentIndex = 0; public function __construct() { $args = func_get_args(); if (empty($args)) return; if (count($args) == 1) $args = $args[0]; foreach ($args as $arg) array_push($this->pMembers,$arg); } public function push() { $args = func_get_args(); foreach ($args as $arg) { array_push($this->pMembers,$arg); } } public function pop() { return array_pop($this->pMembers); } public function each() { $args = func_get_args(); if (empty($args)) { if ($this->pCurrentIndex > count($this->pMembers)) { $this->pCurrentIndex = 0; return null; } $ret = $this->pMembers[$this->pCurrentIndex]; $this->pCurrentIndex++; return $ret; } else if (is_callable($args[0])) { array_walk($this->pMembers,$args[0]); } else { throw new Exception(“Array->each does not understand that…”); } } public function resetIndex() { $this->pCurrentIndex = 0; } } $array = new TArray(1,2,3,4,5,6); while ($i = $array->each()) { echo “$i\n”; } $array->each(function ($elem) { echo “$elem\n”;}); We can go a little further, and do like this. class Person { public $name; public function __construct($name) { $this->name = $name; } } $array = new TArray(new Person(‘John’), new Person(‘Jane’)); $array->each(function ($person) { echo “{$person->name}\n”;}); An issue that comes up is, how do I get the $this variable in there. The short answer is, you don’t. You can finaigle it after a fashion, but it’s just ugly. The issue is, people want the $this var, because they want to be able to manipulate the values, in a permanent fashion, or do something else. How you accomplish it, in a pretty way is: 1. Give up the ghost, you can’t have $this, it’s just a […]