Tuesday, November 20, 2012

WordPress As a Framework for Web Apps - Building a Simple Recipe Database

In the past WordPress has grown from a powerful blog-system to a powerful CMS and, more recently, to a powerful framework for web applications. In fact, using WordPress customization features and the WordPress API, there are not many applications I can think of, you couldn't build. However, there are a lot of applications you shouldn't build with WordPress. Choosing which framework to use (or choosing not to use one) is balancing between many pros and cons affecting architecture, performance, security, scalability and so on.
I found WordPress extremely useful as base for medium sized web apps, without too much traffic. Building a small application, for example a restaurant table booking system, from scratch or even with a framework like Rails or CakePHP, would involve thinking about database scheme, controller structure, authentication, user interfaces etc. A lot of this stuff WordPress is already doing: you already have rough user management, a working admin interface and you only have to think about how to map your data-model to the already existing WordPress database structure.
Our use case: A recipe database
Now, I want to show you how to implement a simple recipe database with WordPress. Requirements are really basic:
  1. Allows adding recipes and editing them just like ordinary posts or pages
  2. Allows categorizing recipes in hierarchical categories like Healthy -> Chicken -> Marinated Chicken Breasts
  3. Allows adding ingredients to a recipe and finding recipes by ingredients
  4. Allows adding quantities to ingredients of a recipe, e.g. 500ml milk, 20g sugar, 3 tablespoons olive oil

Using VBA To Search For The Latest Version Of A Text File

If you're using VBA to import data from a text file, you might need to make sure you're using the latest version of the file. This article will show you how to use VBA to search a folder and identify the correct document based on the date and time the file was created.
One example could be that you receive daily files which are the basis for a regular report you compile in Excel.


Listing Files With The File System Object
The Microsoft Scripting Runtime library must be selected under references in the tools tab before any code you write can access the file system.
Our short piece of VBA code will select all the files in the appropriate folder and find the file with the most recent file creation date.
First, we'll create the file system object and select the folder, which in this example is called "files" and is under the folder of the active workbook.
Dim fso As Scripting.FileSystemObject
Dim SourceFolder As Scripting.FolderSet fso = New Scripting.FileSystemObject
Set SourceFolder = fso.GetFolder(ActiveWorkbook.path & "\files")
Next, we can loop through the folder and determine the most recent file. We do this by creating a date object, which by default defines the year as 1899 - depending on the version of Excel you have.
To begin, we set the newest date as "30/12/1899" so every file will have been created after that date. As we loop through the code we redefine the newest date and file depending on the file creation date.

Using VBA To Search And Retrieve Data From A Text File Containing A Code Library

Sometimes it's easier to save information in a text file, rather than adding another sheet to a possibly already cluttered Excel application. In some instances, the use of a text file might be the only option in a particular situation.
This article will explain how you can organize a text file to make it easy to search for, and retrieve information.
Organizing The File
We'll use the example of using a text file containing a code library, where you can store individual procedures for later use.
One way to search the file is to place your own tags around the code, so that your own VBA code can find the block of text and do something with it. In this case, the "tags" are already in place with the sub and end sub lines.
Perhaps your text file looks something like the text below and you want to retrieve the changeMe procedure which changes the color, bold and font type of the selected cell.
Sub firstCodeSnippet()

' other subs

end subSub changeMe(cellAdress)


With Selection.Font

.Name = "Tahoma"

.Bold = True

.ColorIndex = 3

End With

End sub
Sub otherCodeSnippets() ' other subs end sub

Now, you just need to write some VBA code to retrieve the data.