[How to] Global progress indicator for ajax requests in jQuery

Standard

Showing a loading icon (like the one below) when an ajax request is running is a common practise to ensure end-user is aware that page/information is being updated in background.

ajax loader icon

While developing large scale web applications where one might have numerous ajax requests in the code, it might result in duplication of code to show and hide the loader for each and every ajax request individually.

Instead of writing the code to display and hide the loader for every ajax request, you can add the code below to your layout (common file included in all pages, e.g. footer). But if you do not have a common layout then the code above won’t help you since you’ll end up copy pasting the same code in all files individually which would result in same issue (code duplication) that we are trying to overcome here 🙂

Assuming “loader” is the ID of icon/image (or parent element), you can use the code below.

This would not only save some amount of time which could be utilised for other productive things, it would also help in avoiding unnecessary code duplication. You can use these handlers for anything other than showing/hiding loader as well.

Note that if you are making the ajax request with global option set to false then ajaxStart and ajaxStop events wouldn’t fire. By default global is set to true so you can actually use the global option to control for which requests you would like to show the loader icon. If you do not want to show the loader for a specific ajax request then set global to false. For example,

There are some other global ajax event handlers in jQuery you can refer to see if they suit your use case(s) better.

Happy coding!!!

Moving MySQL InnoDB data files

Standard

If you ever need to move around MySQL data files for InnoDB tables or getting the error mentioned below then follow the instructions in this post.

Note that this post might be helpful only if you have a backup of complete data files directory from your source MySQL installation. Otherwise please refer to the links at the bottom of this post.

Take backup of your data files (source)

Copy your data files from /usr/local/mysql/data/ on Mac or /var/lib/mysql/ on Ubuntu/Linux to a safe location, e.g. inside your home directory. Location of data files can be different so refer to your MySQL configuration file (my.cnf) for correct path.

mkdir ~/db-files/

On Mac

On Ubuntu/Linux

Do not forget to copy the period (.) at the end of commands above.

Restore files (destination)

Stop the MySQL server on target system, if running.

On the new/target installation, replacing the target data directory contents with source data directory may not work, specifically if you have InnoDB tables.

Take a backup of data files on the destination and keep them safe somewhere in case anything goes wrong. Assuming that target data directory location is /usr/local/newmysql/data/

Extract the compressed archive of source data files.

From the extracted contents, manually copy the database folders under destination data files directory. Do not copy/overwrite any files or folders inside destination data directory, for example sys and mysql folders. For example, if one of the database name is mydb and destination data directory path is /usr/local/newmysql/data/ then execute the following command.

cp -R mydb /usr/local/newmysql/data/

This needs to be done for all your databases one by one. Any files or folders which pre-exist in the target data directory must not be overwritten. If you do then there is a fair probability that you are doomed.

Once you have copied all your databases manually (without overwriting any existing folder or file) to the target data directory, copy/replace ibdata1 file from your source directory to target data directory

Finally, ensure that ownership and permissions of folders and files inside destination data directory are correct. Data directory itself and it’s content must be owned by mysql user, _mysql  in Mac and mysql in Ubuntu/Linux.

On Mac

On Ubuntu/Linux

I am not 100% sure but permissions should be as mentioned below. These permissions work on my Mac correctly.

755 (rwxr-xr-x) for mysql top data directory

700 (rwx——) for each database directory

660 (rw-rw—-) for database files inside database directory

Lastly, restart the MySQL server and verify if everything is as expected.

Summary

The most important part is the one related to ibdata1 file. This file stores lot of information and data about your InnoDB tables along with an internal index to all the InnoDB tables. If you don’t replace the ibdata1 file related to your original/source data files then that index would be lost and MySQL would fail to understand the schema and data too would be lost despite having ibd and frm files in place.

Here are links to some useful discussions/posts related to the topic for reference.

What is the ibdata1 file in my /var/lib/mysql directory?

How to restore table stored in a .frm and a .ibd file?

Recovering an InnoDB table from only an .ibd file.

InnoDB Corruption Repair Guide

How to Use Third Party Autoloaders in Yii

Standard

Using third party libraries in Yii is super easy. Just put the library, e.g. Google, under vendors directory and add the following lines at the beginning in your controller where you wish to use it.

Alternatively, you can include the first line mentioned above in your Yii application’s main.php imports section as shown below (line 4).

And then include the desired file in your controller (2nd line) before using it. That’s all.

However, you might run into issues if you are integrating a third party library with it’s own autoloader. Yii’s guide has some documentation on how to tackle the situation but it doesn’t work out of the box for most of the people. The reason why integrating third party libraries with autoloaders in Yii isn’t as simple as integrating a non-autoloader library is that third party autoloaders conflict with Yii’s own autoloader.

In such a situation, you can use the code mentioned below and change the path/library as per your requirements in second line. You can use it anywhere in your code (most likely in controller) as needed.

Trick here is to temporarily unregister Yii’s autoload functionality before adding third party autoloader and then re-enabling Yii’s autoloader. You still need to include the vendors directory in your application’s main.php as shown in second code snippet above.

I am not sure if this works in Yii 2 as well, most likely it won’t since Yii 2 is a complete rewrite, but this certainly works in Yii 1.x releases.

Active Menu Highlighting for External Link Type in Joomla

Standard

While working on a Joomla website, I realized that if you have an external URL type menu item in your Joomla site then most likely active menu highlighting won’t work for it. Active menu highlighting basically means that the current menu selection is highlighted as a visual clue to show the website user which menu is currently selected. In the example below, currently menu item 2 is shown as selected. Continue reading

Tentative delivery date calculation in PHP

Standard

Here is a simple function you can use if you need your PHP application to display a tentative delivery/completion date to your customer. It excludes the weekends in calculation so only business days are considered. For example, if your e-commerce site ships a product in three business days and customer orders on Thursday then this function will return tentative delivery day as Tuesday, not Sunday. Continue reading