James Andrews

Technology

PHP Class – Calendar Matrix

by jandrews on Mar.08, 2010, under PHP Development

The other night I found myself needing a PHP class file that would give me calendar data. Specifically I needed something that I could build a calendar display with. The problem was I didn’t want it to write the HTML, I just wanted it to give me a multidimesional array of weeks and days. That way I could have whatever content I wanted in it. Not finding anything that didn’t write out HTML I created the CalendarMatrix class.


/**************************************************************************
Copyright 2010 James Andrews (email : contact at jamesmandrews dot com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**************************************************************************/
class CalendarMatrix implements ArrayAccess, Iterator, Countable
{
// Define a list of the days of the week in english.
private $daysOfWeek = array( 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday','Saturday','Sunday');
private $dayCount = 0;
private $matix = array();

public function __construct($year, $month)
{
$this->dayCount = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$this->generateMonthWeeksMatrix();
}

public function calendarDayHeaderArray()
{
return $this->daysOfWeek;
}

public function getMonthName($year=false, $month=false)
{
return date('F', mktime(0, 0, 0, $month, 1, $year));
}

private function firstDayOfMonth() {
return date("l", strtotime(date('m').'/01/'.date('Y').' 00:00:00'));
}

private function primeMatrix($startPos)
{
// Set up the first matrix array
$this->matrix[] = array();

for($count=0; $count < $startPos; $count++)
{
$this->matrix[(count($this->matrix)-1)][$count] = "";
}

return $matrixPos = count($this->matrix[(count($this->matrix)-1)]);
}

private function generateMonthWeeksMatrix()
{
// Get the position for the first day in the week header array.
$startPos = array_keys($this->daysOfWeek, $this->firstDayOfMonth());

// prime the matrix
$matrixPos = $this->primeMatrix($startPos[0]);

// Handle each day of the week
for($day = 0; $day < $this->dayCount; $day++)
{
// Fill in the date into the array value
$this->matrix[(count($this->matrix)-1)][] = ($day+1);

// If the current array hits a length of 7 start a new one.
if(count($this->matrix[(count($this->matrix)-1)]) == 7){
$this->matrix[] = array();
}
}
}

/*
* Below are our "implementataion functions."
*/

// We don't want to be able to change the data, so this
// function though here for compatibility does nothing.
// We'll throw an exception later.
public function offsetSet($offset, $value) {
}

public function offsetExists($offset) {
return isset($this->matrix[$offset]);
}

public function offsetUnset($offset) {
}

public function offsetGet($offset) {
return isset($this->matrix[$offset]) ? $this->matrix[$offset] : null;
}

public function rewind() {
reset($this->matrix);
}

public function current() {
return current($this->matrix);
}

public function key() {
return key($this->matrix);
}

public function next() {
return next($this->matrix);
}

public function valid() {
return $this->current() !== false;
}

public function count() {
return count($this->martrix);
}
}

The class is designed to mostly work like an array. With one exception, you can not modify an indexed value. It does how ever allow you to use for, and foreach statements to iterate through the array.

// Instantiate the matrix using the year and month in the constructor.
$matrix = new CalendarMatrix(2010, 03);

The matrix will now initialize itself with the constructor and you can use it like so.


<table>
<?php foreach($matrix as $week): ?>
<tr>
<?php foreach($week as $day): ?>
<td<>?php echo $day; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>

The code will now have created an calendar with the first row being Monday the last row being Sunday. There is also a function go build the day header at the top.


<table>
<tr>
<?php foreach($matrix->calendarDayHeaderArray() as $dayName): ?>
<th><?php echo $dayName; ?></th>
<?php endforeach; ?>
</tr>
<?php foreach($matrix as $week): ?>
<tr>
<?php foreach($week as $day): ?>
<td<>?php echo $day; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>

It is also flexible enough to be used to build a calendar out of divs simpley use both foreach calls next to each other and then put a div in the middle instead of a <td> tag.

Leave a Comment :, , , , more...

Review – Ibis Reader

by jandrews on Feb.27, 2010, under Programming, Software, Web Development, iPhone Programming

Today I was browsing through my tweeps when I saw that @liza had an announcement. A project she’s been working on for some time has come to life, and is viewable for all too see. This project is Ibis Reader, and HTML5 based ePub reader, which allows you to read ePub applications on any mobile devices who’s browser supports HTML5.

I decided to take it for a test drive. I grabbed my iPhone was went to Ibis Reader where I was redirected to the mobile version, and to where I was given instructions on how to setup Ibis Reader on the iPhone. One of the things it asked me was if it could use 50MB of storage on my phone. I agreed, and next I found myself in an area where I could browse books. I scrolled through a couple pages until I got to one called “Japanese Fairy Tales”. I’m a big Nipponophile (Japanese nut), so I decided to grab that, and start to read.

The interface is very simple. touch the right side of the screen and you advance a page, touch the left side of the page and you go back a page. Very easy from the get go. The text displays at a nice readable font size, with a font that’s easy on the eyes. Flipping the iPhone on it’s side, Ibis Reader knows to change the size of my page. The only thing that doesn’t seem to work here is the cover, which is a graphic, and not a huge concern in my opinion.

Since Ibis Reader takes advantage of local storage space the book reads fast. Page loads take only rendering time, and since it’s text, that’s no time at all.

I am very happy with how simple and easy this application runs. If I didn’t know better I would think it was a native app that ran locally on my iPhone. Bravo @liza, this is work to be very proud of.

Leave a Comment more...

Wordpress is more than a BLOG!

by jandrews on Feb.19, 2010, under Web Development

Since it’s inception Wordpress has been customized and built into probably the most easiest CMS to use for maintaining website. The page administration is real simple to use. They have created a wonderful API that is simple to understand, so that even the most novice developers can jump in and get their hands dirty. I have fallen very much in love with that simplicity.

For the past few days I have thought that I wanted a new look for my website. I am not a designer. I can cobble together something that looks “ok-ish”, but that’s about it. I turned to a website that I have come to rely on when I need designs for personal projects. That site is called Themeforest.net. It allows you to buy designs, prebuilt HTML templates, and even Wordpress themes that you can use on your site un-exclusively. Now I could have gone and found a design, and coded all the HTML/CSS/PHP for myself, but I didn’t want to spend the time. I have other projects on my plate, so I thought I would simply find a Wordpress theme that was appealing purchase it and make a go of it. SIx hours of watching video.

While the theme is artistically pleasing, and full of all sorts of features it fails like so many other themes whose designers still see Wordpress as a blogging tool, and not a content management system. The home page is driven by blog category instead of it being a “page”. The javascript carousel is also driven by a category. While I can’t say that I feel it’s 100% wrong, I have had customers who have bought themes and became frustrated because the theme they bought didn’t support pages in the carousel, and then I had to figure out a way to fix it.

These designer/developers have to figure out that Wordpress is not just a blog, and that their customers do not want to rely on the blog aspect and categories to manage “pages” of a site when “pages” is already part of the Wordpress functionality.

I have spend too much time this evening hoping for a response on their forums, or my comment on Themeforest.net. Then trying to debug the code because I really wanted the new look up tonight. Alas, that is not the case. I have it at 99%, but am missing something stupid I am sure. Tomorrow is a new day, maybe I’ll 1) get a response with a fix, or 2) figure out what I am missing.

Leave a Comment :, , , , more...

Corona – iPhone/iPad Development made easy.

by jandrews on Feb.07, 2010, under General Discussion, iPhone Programming

Last night I was browsing twitter when one of my tweeps mentioned an article about iPhone and Flash. The article basically stated that before the release of the iPhone there were discussions of using flash on the iPhone for application development. That talks went south and since then there was no turning back. Now whether there is any truth to that I don’t know. What made the article interesting was the mention of an SDK called Corona by a company called Ansca Mobile. Essentially it’s a scripting language similar to actionscript/javascript that allows you to build iPhone apps as if you were building a Flash application.

I’ve wanted to write Cocoa apps for a while, but with work, and life I haven’t been able to get enough time to wrap my head around Cocoa and bindings and all the fun that goes with it. I know Javascript very well. Since I am a web developer so I thought I would give Corona a shot.

Corona cost $99 but there’s a 30 day trial version. I downloaded the trial version, and started reading through the docs (ok skimming through the docs) and sample applications. I found the APIDocs to be poor. No real explanation on how to capture events, which on the iPhone is important. Touch events are everything when it comes to the phone. Also, the company seems to be indecisive about some of the touch event naming, like “drag”. They thought it didn’t make sense, but I easily knew what they were talking about. Though I probably would have called it “swipe”. The sample code however was full of useful snippets, and from that I was able to build a 45 line application. That’s right in 45 lines I had the application I have been wanting to write for 5 months. Now I haven’t been able to test it on an iPhone or within Apple’s iPhone simulator. I have tested it in Corona’s iPhone simulator, in order to test it elsewhere I need to pay the $99, so for all I know it could totally break. I will give them the benefit of the doubt that it will work fine on the iPhone, and could revolutionize iPhone development for those who want to quickly get something out without the time consumption of learning Cocoa Touch, or paying an iPhone developer up to $150/hr

I am looking forward to them having a version that supports the iPad, I am sure they are working diligently on it.

2 Comments :, , , , more...

iPad

by jandrews on Jan.28, 2010, under General Discussion, Macintosh

It’s been one day since Steve Jobs announced the iPad to the world. Since then it seems like a world of haters has vomit their hatred of the iPad upon us. Complaining that it’s nothing more than an oversized iPod touch. What I am trying to figure out is this 1) why is that bad? and 2) Did you really expect something more? 3) Why did it have to be more?

In Steve’s keynote he talked about the computer and the smart phone and something in the middle. The iPad is not meant to replace the computer, and it’s not meant to replace the smart phone. It’s meant to compliment them. There are plenty of instances where you may want something portable like a phone, but not as clunky as a laptop, and that is where the iPad comes in. Something that doesn’t need a full computer operating system. Something that can become the device you need at the time.

Let’s say you are in the medical profession, do you really want to have to boot into a complex operating system like windows, linux or OS X that uses up battery life quickly? Or would you rather just turn on a device that allows you to open your application, view and enter patient data quickly and submit it to the servers in the hospital IT department? What if you are a photographer, or a real estate agent, or a business person in general who wants to track their appointments, calculate costs quickly. Applications designed specifically for you, so you don’t need to multi-task into other applications? That is what iPad is about.

Sure there are instances where multitasking may be nice, such as listening to Pandora Streams while doing something else in another application. It’s not a perfect device, but for people to condemn it for it’s faults and not look past them to the devices potential just seems real sad to me.

The iPad will be a great device once you get it in your hands, and see what it can do.

1 Comment :, , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...