What are you doing manually that could be automated with php?
One of the things I realized when I was updating my diary here yesterday for the first time since April of 2001 is that there were a lot of manual steps required to update a page once the contents of the diary had been written. Too many manual steps, in fact. One of the many benefits of the new rage bloggers is how easy it is to update one's diary and I think that in addition to the fact that people shared blogs is some of the reason for its growing popularity. If you are interested in adding a blogger to your website I show you how to do so at Script School in the surfer interaction course week #1. I actually started my own blogger some time ago on my personal site tdavids.com, but that was more of an experiment than something I intended to update with anything real useful. So if I am to update this diary with any frequency I simply had to make it more automated -- and thus less time consuming -- to add new diary entries.
I realized as I started to go through this that this is something I've done time and time again using scripting: taken a series of manual steps and reduced them into scripts. So I will explain the steps to do this here and maybe you can include in automating some of your tasks.
STEP 1. List all the steps required to finish a single project.
In my case a single project is defined as "adding a new diary entry" -- below are the steps it takes and in parenthesis I've documented what is manual or currently automated. In the case of adding these diary entries there are currently no steps currently automated.
(manual)
create/write diary
entry, with example code as necessary
(manual) change body tag to include script
(manual) change navigation pointers, last/next diary arrows and links
(manual) change date of file for voting script on diary page
(manual) login to admin area and update search script
(manual) update main diary page
to include new diary entry
(manual) update home page with link to new diary entry
STEP 2. Define what could be automated in the list above, if any.
(manual)
write diary entry, with example code as necessary
Automate: no
Though it would be nice to be able to have someone else come in and write the diary entry for me, that is not reality, lol, so this will remain a necessary manual step but actually this is the one part of this process that I enjoy most. Obviously this is the most time consuming part of the process, though.
(manual)
change body tag to include script
Automate: yes
The WYSIWYG editor I use doesn't allow for the code to change the background colors based on the day of the week (described in diary entry 121999), so I must convert this out with every diary entry using a standard text editor upon each diary entry. This can and will be automated by using a simple replacement from the basic <body> to the correct format including the script.
(manual) change navigation pointers, last/next diary arrows and links
Automate: yes
This one lended itself to a fascinating discussion on using time/date functions in PHP that I haven't really discussed here in my php diary before. When building these diary entries I have stayed consistend with a file format of MMDDYY. Obviously this isn't year 2100 compliant, but then chances are I'll be long dead by then and someday if I get real bored I might be compelled to make the files compatible for the year 2100 and beyond.
However would it be possible to sort a bunch of these files to determine what the most recent diary entry was? With the exception of the first 3 files (121799, 121899 and 121999 all file extensions are the same). So I set about doing a simple sort to see what I could come up with on the data:
<?
$diary_entries = array('121799','121899','121999','122199','122099','122399','122299',
'122699','122499','122799','122899','123099','122999','010800','123199','010100',
'010200','010500','010300','010700','010600','010900','011000','012100','011400',
'070700','012200','021500','071400','070800','071200','071300','073000','072000',
'072900','072800','073100','021801','021901','122802','122902','030301','030401',
'042701');
sort($diary_entries);
foreach ($diary_entries as $s) {
print("$s<br>");
}
?>
Example 33: Problem with sorting MMDDYY dates as string using sort function
The result of this unfortunately was not a true chronological order, but an ASCII sort. This wouldn't work for what I was trying to do. Because it would be difficult to determine what was the most recent. I then thought about using the last modified date, but then that wouldn't work because if I modified files out of order (which I might do someday in the future) that would screw up the logical order of the files. The next thought I had was converting the MMDDYY into a unix timestamp and sorting that way. So how to take MMDDYY format and convert to unix timestamp?
Answer: use the built-in php function: mktime() http://www.php.net/manual/en/function.mktime.php
How to convert MMDDYY date to unix timestamp using PHP
So now I've altered my code for example #34 to retry sorting. The hours, minutes, seconds part of the argument is not important. Here is the mktime format:
mktime ( int hour, int minute, int second, int month, int day, int year [, int is_dst])
Note: mktime() does accept 2 digit years.
My code alteration inside the foreach loop to convert the date would look like this
$month = substr($s, 0, 2);
$day = substr($s, 2, 2);
$year = substr($s, 4, 2);
$unix_timestamp = mktime(0,0,0, $month, $day, $year);
print("$s = $unix_timestamp<br>");
Example 34: How to convert MMDDYY to unix timestamp - source
Bingo! That does the trick. Now it is possible for me to sort the files by the unix timestamp which is the correct diary entry date. So now it is just a matter of building a new timestamp array and converting back from that date. I could do an associative array and use the built-in asort() function, but since I will only need to know the most recent date, I would just use the second to last index in the array. Why couldn't I use the very last index? Because that is the placeholder diary page that says: "this index hasn't been created yet" The code to switch back from unix timestamp to date is teh built-in date() function. Here's the code to do this:
sort($as_timestamps);
print("<br>Now sorted by timestamp, oldest to newest<br>");
foreach ($as_timestamps as $s) {
$diary_entry = date("mdy", $s);
print("$s = $diary_entry<br>");
}
Example 35. How to convert unix timestamp to MMDDYY - source
So this takes care of determining the previous and next arrow buttons automatically. One might look at all this and say, heck it's easier to just manually set them! But the beauty of this is that now replacing at the script level is a piece of cake so I never have to worry about these navigational buttons and link code again. I just have to do replacements in the template I work from like this:
%122802.php3% -- %123002.php3%
Anywhere that text appears, I'll have the script replace with the appropriate link. The 123002.php3 will be determined by taking the current date and adding one day (see next item below). Next on the list:
(manual) change date of file for voting script on diary page
Automate: yes
This is easily done by taking the current diary entry date. This will be the real time date that I add the diary entry. The diary entry I'll be creating with my new automated diary entry script and adding one day to it. This will be as easy as calling the date and mktime functions again:
$today = date("mdy");
$tomorrow = date("mdy", mktime(date("d")+1));
The %123002.php3% above is created by examining the last array index and changing this filename to the day immediately after the current diary entry that I'm creating. My diary system doesn't really allow for creating multiple diary entries within the same day, but I've always just figured I'd add to each diary day rather than create all new diary entries. I can add a timestamp to each diary day and since this diary entry is one of my longest ever, size isn't really a concern.
(manual) login to admin area and update search script
Automate: yes
This is the easiest one to automate. After the diary entry page is created I just need to include the search script so it is effectively called at build time. Next.
(manual) update main diary page
to include new diary entry
Automate: *yes, sort of
Now that I created the as_timestamps array which orders all the diary entries it certainly makes it easier to break out items by the date, but it doesn't really describe what is in the diary page. This makes it a bit problematic to automatically add to the page unless I include some sort of data about what the diary entry is about or change the format. If you notice the current format of the diary page it doesn't really lend itself to convenient inserting as a template because the location changes. I will likely change this format and make it more template friendly, which will make the future of updating this page automatically easier. What I can do, however, is add a "new" section to the top of this page which lists say the 3 newest diary entries and a description -- which can be entered into the diary entry update form (this will be described shortly). I can generate this list of 3 diary entries from the $as_timestamps array and use my built-in meta tag search engine display described in diary entry 010600 for the descriptions of the pages. Since I've remained consistent with using meta tags which describe the diary entries themselves, this has treated me very well in both local and remote searching of these diary entry pages.
(manual) update home page with link to new diary entry
Automate: yes
No need to setup a separate template for this page at this time, as I decided to manually scroll old diary entries off the main page at this time. The reason being that because over the last 3 years I've not had a great many diary entries (less than 40, actually, and that includes this diary entry). Hopefully with this new automation this will increase the number of these diary entries. So the easiest way to do this is to add a comment tag and then just have the script insert the diary entry in the format on the main page BELOW that comment tag. If I add manual date updates on the home page, they will be scrolled as well.
Now let's review the revised manual to automation list:
(manual) write diary
entry, with example code as necessary
*(automate) change body tag to include script
*(automate) change navigation pointers, last/next diary arrows and links
*(automate) change date of file for voting script on diary page
*(automate) login to admin area and update search script
*(automate) update main diary page
to include new diary entry
*(automate) update home page with link to new diary entry
*changed to automate
This means I've taken essentially 7 steps and made it one: create the diary entry.
STEP 3. Implement the automation solution
Now I need to add one new final step to what was once my 7 actions above to create a new diary entry, and that is to cut and paste the diary entry into the automation script that I've described above to handle all the automation tasks. I'll add this to my admin area script for this site. Basically this is a form with a textarea field for me to cut and paste the diary entry I've created locally with my WYSIWYG editor and which contains the template replacements I described above.
This form looks something like this (it doesn't work here, it's just for display purposes):
New
Diary Entry:
Brief description:
diary HTML:
Brief
description - this will be the description on the home page.
diary HTML - this is a cut and paste of the HTML code that I've written locally.
So with my new format all I need to do is write the diary entry and then cut and
paste the HTML source code to this form and press the "Add Diary
Entry" button and the 7 steps I outlined above will be done automatically.
Of course there is code necessary in the admin script to do those 7 steps including building the pages, but I've outlined the basic framework of this script in this diary entry.
These are the steps you would take to automating manual tasks for any project. When you find that you are doing something repetitively, or that you shy away from doing something because it requires too much manual HTML work, consider automating the work as outlined above. If you lay out your pages in some sort of logical order as I did when I originally created these pages over 3 years ago then you can come back and automate them fairly easily. Planning and design considerations in website construction should be examined carefully.
Happy coding to you!
==============
New forum for discussion of diary entries
Finally a place to discuss these diary entries, php and other scripting-related stuff. The homeroom at Script School is available to discuss this diary entry. You'll find some recent comments included in these diary entries. You must be an enrolled student at Script School to add comments to these diary entries. Please, no spam, and stay on topic for each diary entry. With this in mind, I'm going to try and start updating these diary entries again more frequently and sharing some useful things I've learned about PHP.
Please vote on the usefulness of this diary entry so other people will know if it is worth their time to read :)
[back]
12/29/02 "How to automate
& reduce
manual tasks using PHP"
[next]
PHP Diary | Script School | PHP Scripts | TD Scripts.com
Copyright 1999-2003 php-scripts Last Modified 12/29/02 03:51