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.

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.

So far

Posted February 17th, 2007 in Coding by Azuka

We’re coming along nicely aren’t we?

Authware Screenshot

Hello world!

Posted December 20th, 2006 in Coding by Azuka

At last, I shall be free to rant in peace. I’ll hopefully be moving most of my code section over here.

The old links to my page content will still work. I set up 301 redirects to the new content.

Working

Posted October 22nd, 2006 in Coding by Azuka

I’ve already mocked up a design for Authware and tweaked the database layer — I was able to communicate successfully with PostgreSQL, MySQL and SQL Server. Hopefully, any other developer interested should be able to write a layer for mySQLite or some other obscure database. I used PHP 5′s Interface feature — the classes only have to implement it. I only have to work on the admin area and the different modules — it’s going to be tougher this time because I’m using pluggable functions like WordPress, a detailed permissions system, and perhaps XML-RPC.

I’ve been spending a while working with PHPBB3 Olympus, working on some obscure site for some [rich] clients (you may take this tongue-in-cheek). Without Zend Studio to help me explore the code, I daresay I’d have been truly doomed. What confounds me now is duplicating the registration and login codes to work out of the PHPBB concept without lumping together all the core PHPBB code together. So far, I’ve been chasing my tail running through myriads of functions that each make their contribution the the complexity of the code. The annoying thing is that since it’s in beta, there’s no code documentation — I have to make guesses and keep trying. What’s that joke — the last thing a coder wants to do is code.

Flex looks like one of those necessary evils — I remember PHP was when I had to give up ASP and recode a crazy site entirely in PHP. Ikezi’s been bombarding me with Flex UIs to no end but I guess what finally did the trick was Ms Dewey. Talk about a pretty girlish yet sophisticated bot who pouts and winks to no end at you, or looks out of the corner of her eye while doing something silly. As a search engine, it fails — as a seductive marketing ploy, well…

Designers vs. Developers

Posted October 8th, 2006 in Coding, Design by Azuka

i55 ScreenshotI’m in charge of overhauling a site (http://www.i55.org) and moving it to Joomla. Here’s the test site [before it goes live].

I’ve been slaving away at this site for quite a while, rewriting components to fit the design I was given as much as possible. I proffer my work to the client, and he’s quite satisfied but what happens with the designer?

He’s angry I didn’t give the exact stuff shown in his PSD design. For one, I had considerably slimmed the site down from the former developer’s work, and tried to follow his design as best as I could, rewriting some components in some cases to make them fit. I’d originally been told by my client to ignore the styling if it wasn’t feasible.

Was this my punishment for trying to match the design I was given? Designers can be such a royal pain…

Changing Compilers

Posted September 16th, 2006 in Coding by Azuka

I updated my archaic DevC++ installation today — and holy of holies, I couldn’t compile something I was working on. I decided to try out a “Hello World” program, but I still couldn’t compile. I was beginning to think the problem was from several installations I made recently so I uninstalled them — the errors thrown only changed.

I shutdown XP and booted to Vista, installing the DevC++ compiler again, and facing the same errors. Some simple research on the net told me I wasn’t the only one having DevC+ woes. Thanks to Google, I discovered Codeblocks but the MingW/GCC compiler still threw a lot of nonsense errors. I’m currently using the Digital Mars compiler and it works well.

I know I could use it with DevC++, but talk of falling in love with an IDE — Codeblocks rules for now.

SQLite

Posted September 12th, 2006 in Coding by Azuka

I was just going over something today ans came across SQLite. Nope — it’s not that I just discovered it — I mean, I initially planned to start using it way back since last year, but I obviously needed a book to remind me that it existed.

I’m working on a lightweight database abstraction layer [now you'd ask me 'what for?'] for Authware, and I think I’m going to use SQLite for my local installation — at least just to get a feel of things. Currently, I’m working with PostgreSQL and MySQL without any incidents. PHP5 rocks — I can’t imagine how much code I had to write when PHP 4 was still the ‘in thing.’

Anyway, things are looking good. I see a dangerous, sharp curve ahead but I’ll deal with that when I get to it.

Back to SQLite. I haven’t tested its performance yet, but I’m still wondering if the single-file databases won’t be a problem — we writers sure can come up with a lot of junk when we choose to. Face it, I hate writing functions in MySQL — but I’m already in love with sqlite_create_function() and sqlite_create_aggregate()… even without having ever used them :D .