Determine Current Page in WordPress

Lately, I’ve been doing a lot of work with WordPress themes. On one particular site, the navigation menu is built dynamically, combining pages and categories in the navigation scheme. I needed to find a way to determine which page or category was currently being viewed so I could apply a special class to the item in the navigation menu, indicating that the link is active.

After a bit of research, I found a couple of useful WordPress functions that helped me determine which page the visitor is currently viewing.

Find an Item in an Array (PHP)

On occasion, I’m sure you’ve found yourself in a position where you need to find an item inside of an array. PHP offers a very simple method to do just that. The array_search function lets you search for the item and then tells you what the index for that item is. This function works with numerically indexed arrays and with associative arrays.

Finding the ID of a Newly Inserted MySQL Row

If you’re anything like me, you generally set up an auto-incrementing integer field in most of your MySQL tables that can be used as the primary key for your records. A lot of times, after inserting new information into the database, you need a way to find out what ID was assigned to your data when you created the new row. With MySQL, it’s really simple to get that information. MySQL offers a function called “LAST_INSERT_ID” that does just that.

As soon as you finish inserting the new row, if you run another SQL query that looks like “SELECT LAST_INSERT_ID()” on your database, the result you get back will be the most recently added auto-increment number in your MySQL installation.

PHP also has a built-in function to perform this operation. Instead of setting up a separate MySQL query, executing it and retrieving the results from it, you can simply use the mysql_insert_id() function, which will automatically perform those steps and simply return the ID number.

Adding and Removing Items in PHP Arrays

When working with arrays, you may often find yourself in a position where you need to add a few extra items to the array or remove some of the items you already added. PHP has a few functions that are designed to help you with this process, allowing you to add new elements to the beginning or end of the array or to remove elements from the beginning or end of the array.

Happy New Year!

I’d like to take this opportunity to wish you all a very happy new year. I won’t look back at the past year or the past decade and try to list any spectacular events or lessons from that time period (I don’t have a good enough memory to do that), nor will I try to look forward and make any predictions about what’s going to happen in the coming decade or even the coming year.

Instead, I’m going to take this opportunity to share a very quick, easy tip for anyone using a PHP template on their Web sites to ensure that your copyright always shows the current year, doing a very small part to keep your site from looking too terribly out of date.

Copying Data from SQL Server to MySQL

In my last post, I showed you a way to copy the structure of an SQL Server table into a new MySQL table. In this post, I will show you how you can fairly easily copy the data from that SQL Server table into your new MySQL table.

Before we start, we should make sure we have a complete list of the columns in our MySQL table (which should, if we did everything right in building the table, be identical to the columns in our MS SQL table).