Category Archives : PHP/MySQL


Rails 3 – PHP Style Global Variables, $_SESSION, $_GET, $_POST in the models, and everywhere.

PHP Globals in Rails 3 So, as part of my slow descent into hell for even showing this stuff, I thought I would make a quick and dirty post about getting PHP like globals into your models in Rails. I wrote a longer article about globals in the model in rails which you can check out for some other options. def some_before_filter $_SESSION = session $_GET = request.GET $_POST = request.POST $GLOBALS = {} end Yeah, that simple. When it comes to globals, the ruby and rails community is kind of like the child of a former Nazi sympathizer who got off the hook at Nuremburg. Yeah, they’re related, but they just don’t want to have anything to do with them, and if you remind them of the connection they get really angry.


Apache2/MySQL on Ubuntu Configuration

Once you’ve installed apache2 and mysql on your server, you might want to try these configuration changes to make it perform a bit better: In /etc/mysql/my.cnf key_buffer = 16K max_allowed_packet = 1M thread_stack = 64K table_cache = 4 sort_buffer = 64K net_buffer_length = 2K And in Apache2′s config, /etc/apache2/apache2.conf, find the section for MPM prefork if yo have it and: StartServers 1 MinSpareServers 3 MaxSpareServers 6 ServerLimit 24 MaxClients 24 MaxRequestsPerChild 3000 The above are just some things to try if you encounter a situation where apache is using too much memory, or the same for mysqld.


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.


SOAP APIs R Klunky or How to waste bandwidth for no good reason

The fact that SOAP is still around is, to be honest, absolutely beyond my comprehension. No serious effort seems to have been put into something like JSONRPC. Still, when it comes to some API calls, SOAP is pretty darn good, but this here is one example of it being used as just a waste. I recently setup a client, who has a Spree Commerce Ruby on Rails store, with the Ebay Trading API. Basically, he lists items on his store, sends them to ebay, and his store is notified when they have sold on ebay, and are thus removed from his store, or when they sell on his store, they are ended on ebay. Pretty standard setup, nothing special. The app subscribes to ebay trading api platform notifications which are sent to the server in a SOAP format. The SOAP notification that an item has sold is 11kb. Yeah, I know what you are thinking, that’s small. No, it’s not. When you are talking millions of users, it’s not small, it’s huge. The information that I actually need from that is 1.1k. To be quite honest, I need even less. Apparently the client notifications api sends out notifications in as REST kind of way, i.e. as a url with key=>value pairs. Really, all I need is: EventName=TheEvent&ItemID=2312398081 That’s all I use. I understand that other people might need more information, and may be handling all kinds of specific situations, but that’s part of the point. Different people need different info, so just dumping all available […]


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, […]


HowTo html input find/replace regex

Whenever I make a form, it’s a bit of a hassle to add in the php support. That is, if the page gets reloaded of sent back, we want to insure that all the valid data that was entered before is maintained. And easy way to do this is: Here is an easy way, tested in jedit, but should work with little to no modification in other regex capable editors: Find: \<input name=”([\w]+)” value=”([\w]+)” /> Replace: <input name=”$1″ value=”<?= isset(\$_POST['$1']) ? \$_POST['$1'] : ‘$3′ ?>” />


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 […]