James Andrews

Web Development

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...

How not to attract web developers.

by jandrews on Dec.02, 2009, under Web Development

Today I was browsing Craigslist help wanted section. In the “computer gigs” section I came across this add.

Title: Web Application Developer Wanted.
We are looking to build a 25 to 30 page web application. If you are interested please supply a quote.

25 to30 pages in total
YOU MUST MUST MUST have experience in SOAP API integrations and prove this.
The web application will in turn need to be connected / integrated to other applications such as SFDC (Salesforce.com) or iphone.
Looking for start to finish quote.

Please send resume and contact info.

Thank you!

The title was clear and concise. It was enough to peak my interest, and see what it was about. The first two sentences though sent me turning the other way. They have an idea for a 25 page site. Without any description of what those 25 pages are they expect you to send in a quote? Then they go on to re-itterate how many page there are, like they already hadn’t pointed that out. Then we “MUST MUST MUST” have experience with SOAP API. Looking for a “start to finish” quote. A quote on something with no explanation of what it is, or on what technology; beyond SOAP API.

How on earth do you expect someone to quote a project if they don’t even know what is involved? If you have a project and you need someone to do the work for you. Figure out what tech you want to use, if you have no preference, specify that. Don’t ask for a quote, ask for resumes. Take some resumes first look them over, and find people who seem to have some technical expertise Ask them to sign an NDA, give them some specifics and then ask them for a quote.

With the ad above any quote you get will be wrong, and in the end either you or the developer will lose out because the project wasn’t properly speced.

Leave a Comment more...

php 5 Random String Generator

by jandrews on Aug.05, 2009, under PHP Development

I am sure people have written something similar 1000 times, but with time they fade from the internet. Today I will share with you my Random String generating php class. Are you ready?

<?php

    /**************************************************************************
    Copyright 2009  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 RandomString
    {

        /* static variables needed to create the random string */
        private static $alphas = "abcedfghijklmnopqrstuvwxyz";
        private static $alphasUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private static $numbers = "1234567890";
        private static $specialChars = "!@#$%^&*()-_+=[]{}|:;>.<,/?";

        /*
         * Function to generate a random string of roman characters
         */
        public static function generateRandomAlphaString($stringLength, $type = "lower")
        {
            switch($type)
            {
                case 'upper':
                    $stringBase = self::$alphasUpper;
                    break;
                case 'mixed':
                    $stringBase = self::$alphasUpper . self::$alphas;
                    break;
                default:
                    $stringBase = self::$alphas;
            }

            return self::generateRandomStringWithLengthFromString($stringLength, $stringBase);

        }

        /*
         * Function to generate a random string of numbers 0-9
         */
        public static function generateRandomNumericString($stringLength)
        {
            return self::generateRandomStringWithLengthFromString($stringLength, self::$numbers);
        }

        /*
         * Function to generate a random string of numbers 0-9 and roman characters
         */
        public static function generateRandomAlphaNumericString($stringLength, $type = "lower")
        {
            switch($type)
            {
                case 'upper':
                    $stringBase = self::$alphasUpper . self::$numbers;
                    break;
                case 'mixed':
                    $stringBase = self::$alphasUpper . self::$alphas . self::$numbers;
                    break;
                default:
                    $stringBase = self::$alphas . self::$numbers;
            }
            return self::generateRandomStringWithLengthFromString($stringLength, $stringBase);
        }

        /*
         * Function to generate a random string of numbers 0-9 and roman characters and other special characters
         */
        public static function generateRandomStringWithAll($stringLength)
        {
            $stringBase = self::$alphasUpper . self::$alphas . self::$numbers . self::$specialChars;
            return self::generateRandomStringWithLengthFromString($stringLength, self::$numbers);
        }

        /*
         * Function takes a string length, and a "string from", and generates a random string
         * of stringlength, with characters withing "string from"
         */
        private static function generateRandomStringWithLengthFromString($stringLength = 0, $fromString = "")
        {
            $fromStrLen = strlen($fromString);
            $returnString = "";

            for($count = 0; $count < $stringLength; $count++)
            {
                $random = (rand() % $fromStrLen);
                $random = rand(0,$fromStrLen);
                $returnString .= substr($fromString, $random, 1);
            }

            return $returnString;
        }
    }

This class is useful anytime you need a random string of any length. All the functions are called staticly, so no need to create an object just call the class.

Example 1:
Create a random string of 12 lowercase-alphabet characters

print RandomString::generateRandomAlphaString(12);

Example 2:
Create a random string of 12 uppercase-alphabet characters

print RandomString::generateRandomAlphaString(12, 'upper');

Example 3:
Create a random string of 12 mixed-case-alphabet characters

print RandomString::generateRandomAlphaString(12, 'mixed');

Example 4:
Create a random string of 12 number characters

print RandomString::generateRandomNumericString(12);

Example 5:
Create a random string of 12 number characters or lower-case alphabet characters

print RandomString::generateRandomAlphaNumericString(12);

Example 6:
Create a random string of 12 number characters or upper-case alphabet characters

print RandomString::generateRandomAlphaNumericString(12, 'upper');

Example 7:
Create a random string of 12 number characters or mixed-case alphabet characters

print RandomString::generateRandomAlphaNumericString(12, 'mixed');

Example 8:
Create a random string of 12 number characters, mixed-case alphabet characters, or special characters

print RandomString::generateRandomStringWithAll(12);

This class will pretty much fill all your random string needs. Feel free to use it if you like, or make suggestions to changes, to make it better.

3 Comments 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...