SQL

Posted April 26th, 2010 in Coding by Azuka

My ugliest SQL query — for posterity.

SELECT `wp_category`.`id` AS `category:id`, `wp_category`.`name` AS `category:name`, `wp_category`.`created` AS `category:created`, `wp_category`.`modified` AS `category:modified`, `wp_lib_items`.* FROM `wp_lib_items` LEFT OUTER JOIN `wp_lib_borrowers` ON (`wp_lib_items`.`id` = (SELECT `wp_b`.`id` FROM `wp_lib_borrowers` AS `wp_b` WHERE `wp_b`.`item_id` = wp_lib_items.id ORDER BY `wp_b`.`status` ASC LIMIT 1)) LEFT OUTER JOIN `wp_congregate_members` ON (`wp_lib_borrowers`.`member_id` = `wp_congregate_members`.`id`) LEFT JOIN `wp_lib_categories` AS `wp_category` ON (`wp_category`.`id` = `wp_lib_items`.`category_id`) ORDER BY IFNULL(NULLIF(`wp_lib_borrowers`.`due_date`, '0000-00-00'), '3333-33-33') DESC, `wp_lib_items`.`title` ASC LIMIT 10 OFFSET 0

Now I’m off to tango with PHP’s SplPriorityQueue.

Thanksgiving code

Posted November 28th, 2008 in Coding, Rants by Azuka

I needed a script to loop through the letters of the alphabet and put this in an array: a-z, aa-zz, aaa-zzz, aaaa-zzzz, etc. Through a very rough approach, I’ve satisfied conditions 1-3, but not anything after (source code below).

[syntax,alg.phps,php]

So, all my problems are from the function get_items(). Basically, I don’t want to loop through the $used array to find out what values have a particular length. So based on my analysis, we have a theoretical function f(x, y) which gives the following when run:

f(1, 26) = 0
f(2, 26) = 26
f(3, 26) = 702
f(4, 26) = 18278

ie, the set {0, 26, 702, 18278, …}

f(2, 26) - f(1, 26) = 26      = 26 ^ 1
f(3, 26) - f(2, 26) = 676     = 26 ^ 2
f(4, 26) - f(3, 26) = 17576   = 26 ^ 3

Okay, now I see it clearly. Here goes:

[syntax,alg2.phps,php]

So, why do I need this? I have to do a test on an PHP-Ajax newsletter WordPress plugin. On a site I’m working on, we need to send emails to 70000 subscribers, and the current plugin loops through all the records in the database in one go. Of course the max execution time passes before then. I needed to generate a test database of 70000 emails @localhost.com to help me test while I rewrite the plugin.

Well, onto the actual rewrite.

Algorithms for Dummies

Posted November 7th, 2008 in Coding, Rants by Azuka

I’m taking my second algorithm class this semester. So far, I think I understand a lot of the algorithms discussed in theory. The problem is translating them to code. Most of the algorithm textbooks I’ve come across are so technical, I haven’t the foggiest idea how to actually go on.

The biggest problem, I think is that I cannot even implement the most basic structure needed — a binary tree — not to talk of directed and undirected graphs. Not knowing doesn’t necessarily translate to not wanting to know, and the fact that I can’t write a simple Insertion-Sort algorithm kind of makes me feel down.

I’m dusting off my old C++ book and starting over again from arrays and linked lists. :-(

If you happen to know where a poor coder-wannabe can get a simple algorithms book — with code examples — I’d be very grateful.

CakePHP

Posted April 23rd, 2008 in Journal by Azuka

I’m rewriting Authware at the moment using CakePHP.

I figured I had to learn something about MVC sometime, but here I am doing so — and it isn’t funny.

Ruby (on Rails)

Posted February 3rd, 2008 in Rants by Azuka

Back when I still had a print subscription to Linux Journal, I read an interview of David Hansson, creator of rails and thought, ‘wow, this is cool!’

My friend Kwame had a book on Ruby which I perused and found the language was relatively simple. Now for such a simple, amazing language — and framework — one would think the install process was just as easy. Well, it isn’t 00 at least not to me.

I read tutorials on the arcane of the arcane, built it myself and whatnot and in the end couldn’t get it to run on my Windows machine — not that it was present in the online repositories to get it for my Fedora machine.

My answer? Screw rails. If I need a framework I’ll go for Cake — but we all know I won’t. After all, I’m very egotistical and want to try writing everything out myself.

Open Link in New Window

Posted June 15th, 2007 in Rants by Azuka

Isn’t there some way to disable this or move it further down in Firefox? Why does it have to be at the top? Everytime I righ-click a link, I intend to open it in a new tab. I hate new windows!

The crazy context menu item just keeps getting in my way…

SELECT * EXCEPT(…)

Posted June 3rd, 2007 in Journal by Azuka

This would have been one of the coolest additions to SQL. Programmers are lazy, always looking for a way to avoid writing more code and I suppose this is one area I fit the norm. I need to select all the fields from a 20-column table… except one.

Doing a Google search, it’s apparent I’m not the only one who’s looking for such a feature. I know, I know. I could have written the column names manually during the time I typed this.

What’s with PHP Sessions?

Posted June 1st, 2007 in Rants by Azuka

I’ve been tearing out my hair trying to figure out how to have persistent sessions on the client. Okay, I’ve done it before on several projects but those ones didn’t use a database-based session handler.

I’ve called session_set_cookie_params to no avail everywhere in my code — the time in the db’s correct, but the session always expires when the browser is closed. Setting the session cookie myself gives a different problem — it gets invalidated on the next page load.

Arrrrrrrgh! I hope it really is because I’m running it on a local domain. If it isn’t, I just might get angry, download the PHP source, learn C and rewrite PHP in no particular order (I wish!).

Code Profiling

Posted April 27th, 2007 in Coding by Azuka

While running some tests on my local machine, I discovered a certain content item on Authware took between 2.5 and 2.9 seconds to load — when nearly every other one got loaded in less than 0.7 seconds. There were some basic references to clone — especially when determining the book name for a certain content item and discovering the book was it’s own parent — and I naturally erroneously assumed the overhead arose in those sections. Doing a manual copy of all the elements I needed instead of cloning changed nothing.

Something about code profiling from George Schlossnagle’s Advanced PHP Programming came back to me, and the hunt began. I must have been typing in the wrong stuff because only commercial products kept coming back until I got a Sitepoint article on XDebug.

Downloaded it, got WinCacheGrind and spotted the problem in less than a minute. That ‘nifty’ html_2_xhtml function I wrote sometime before when I was just starting out with regular expressions did a lot of arcane stuff with the e modifier of preg_replace.

Solution: swipe the wpautop and wptexturize functions from WordPress. The slow content item runs in less than 0.6 seconds now, and the other items take between 0.1 and 0.3 seconds to process. And er, the PHP website warns about using strstr just to determine if a needle exists in the haystack. Replacing it with strpos in my own version of wptexturize saw an increase in response times (by about 17 milliseconds).

Let’s hope I don’t keep profiling away instead of doing some real coding in the next few weeks.

SpawEdit

Posted March 6th, 2007 in Rants by Azuka

When I first wrote Authware I needed a WYSIWYG editor. I wanted one written in PHP that was basically drag and drop.

I couldn’t find a basic TinyMCE-PHP implementation back then. FCKEditor (it’s come a long way in the last two years) gave me a lot of bad code. By chance I came across Innovastudio‘s editor. It did everything it said it would — the problem was, it wasn’t free.

After some more scouring I came across Solemtra’s Spaw Editor. It gave me some pretty bad HTML code but I wrote a simple regexp matching function that converted all the uppercase tags (a la IE) to lowercase and did some font to span tag conversion.

Looking back now, am I glad I made the decision? You bet!

For one, it gives very very XHTML-compliant code now (no FONT tags + lowercase characters), works in Opera 9 (surprise!) and still has a nifty PHP class I can instantly plug in.

I’m off to look for a PHP Word to HTML parser. I need one badly :( .