I had an issue with a MySQL query containing a sub-query recently where it worked fine when done as a SELECT query, but gave an error when switching it to a DELETE query. The error given was something like 'You can't specify target table "items" for update in FROM clause'. The sub-query was referencing the same table as the main query, which apparently can't be done directly in MySQL because the table will be modified during deletion. But there is a sort of a hack I found in this StackOverflow answer, among others, to force it to create a temp table and allow it to work.
query posts
jQuery AJAX and multipart form handling
I recently had need to submit a web form with file fields via AJAX. The application uses jQuery and was already submitting forms just fine without file fields using the .serialize() method to pass data to a jQuery.ajax() call. That didn't seem to handle the file fields, though. Searching the internet, I found a solution using the browser built in FormData object.
Sorting with `FIELD()` function in Doctrine
I recently needed to sort query results to match the order of IDs passed to it for use in a WHERE … IN() clause. In MySQL, this can be done using the FIELD() function in the ORDERY BY clause. For Doctrine, which doesn't have the FIELD() function and doesn't allow functions in the ORDER BY clause, there's a little more work needed to make use of it.
JQuery UI Droppable and Handling Multiple Draggable Types per Droppable
JQuery UI Draggable and Droppable make it fairly easy to implement dragondrop on a web page. There are some things that are not easy to do with it though. One example is having a droppable accept multiple types of draggables with different responses depending on type, especially when added at different times (for instance, being attached by separate objects/scripts). The way JQuery UI is set up, only one droppable behavior set can be attached to an element, so doing
element.droppable({accept: ".type1",...});
element.droppable({accept: ".type2",...});
simply replaces the ".type1" options with the ".type2" options.
In a recent project, I needed multiple draggable types per droppable, so I created an object class to handle adding a new "accept" type and associated events to an element that is already a droppable. I do this using duck punching to overwrite the original event callbacks. The wrapper callback checks the draggable element to see if it matches the new "accept" value. If so, it runs the new callback, otherwise it runs the original callback. Every time a new set of droppable options is applied, a new wrapper callback is created that calls the previous, so that no functionality is lost. Perhaps not as efficient as a single function with an if/switch tree, but that would not be feasible for this use case.
Continue reading post "JQuery UI Droppable and Handling Multiple Draggable Types per Droppable"Animation queue management for jQuery
jQuery makes it fairly easy to animate DOM elements. Animating a single-step animation on one or more elements is simple with the call of the animate method. Multi-step animations can be more complex because animations are run asynchronously, meaning that they will start running when called but the script will continue onto the next step before the animation is done. For these, jQuery has the ability to queue steps. jQuery automatically queues multiple steps on a single object and dequeues as each completes, so you don't have to worry about managing things and setting up callbacks. But for more complex animations where multiple elements are animated at different times or other functionality must be performed after an animation step, there is no automatic queuing.
A common practice for simple queuing is to use the "complete" parameter of the animate method or of other similar asynchronous methods that is a callback to be run when the animation is finished. This works nicely when there are a few steps. It becomes more unwieldy though the more steps you add. That is where queue comes in, allowing for adding of as many steps as you want without having to nest in callback after callback.
David Hawkins: JQuery Image Reflections
The design of the gallery portion of the David Hawkins site I'm working on at Cogneato called for a reflection of the current image below that image. This could have been done by making reflections for each image in an image editor and then adding them to a separate field in the CMS. That would have been a pain and would require (most likely) us to be involved for each image added.
Luckily, since this site was already going to be using jQuery, I was able to find a jQuery plugin that handles the reflections automatically on page load. People without javascript just won't get reflections. It works in modern browsers and IE 6-8. It is less than 2kB, which would be much smaller than even a single separate reflection image, though the now-more-bloated 72kB jQuery might ruin size benefits if we weren't using it already. And as far as adding images to the site, the reflection is added automatically, well worth it.
Because of the design of the site, I had to modify the script somewhat to make it work properly. One issue was that I had a border around my images. Continue reading post "David Hawkins: JQuery Image Reflections"
Stearns: Wordpress Custom Queries
For the Stearns site, we need to list upcoming events on the home page. Using Flutter, I created a custom write panel for the events (and other items). The events are simply posts that have a custom date field attached to them.
I was attempting to use the "query_posts()" function to get the posts I need. I discovered that it is possible to use this function multiple times on a single page. I previously thought you were unable to because of "the loop", but you only have to make a few accommodations for the page name and other such Wordpress variables getting changed. I was able to use this to output the page data on our home page plus two categories of posts.
Unfortunately, "query_posts()" allows limiting by category and sorting by a custom key, but no less than, greater than, or other such comparisons with the meta key [wrong, see end of post]. So I decided to make my own SQL query, to be run with the "$wp_db->get_results()" function. The function allows a straight SQL query to be run. Then some other functions are used to put the result set into "the loop". So, the code to run my custom query looks like the following:
Lynda JQuery Certificate
We had the day off for Veteran's Day, so I've had some extra free time. I spe…
Continue reading post "Lynda JQuery Certificate"