James Andrews

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

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

Full Circle v0.5.1 Release

by jandrews on Mar.31, 2009, under PHP Development

I am happy to announce the release of Full Circle v0.5.1 This is a new feature release. I asked for feature requests, and Gary mentioned he would like to be able to enable disable posting to facebook and only enable it on certain posts. It was a relatively easy feature to add, so I did.

At the bottom of the advanced edit page. You will find 2 new checkbox fields at the bottom of the page. One for twitter one for Facebook. You can disable one or both. In your Full Circle settings page, you can enable Facebook and twitter, but set their default to be disabled posting. If this is the case then the 2 new checkbox fields on the advanced edit page will be unchecked. Checking them pushes the post to which ever ones you have activated.

Download Full Circle Today

33 Comments :, , , , , , more...

Full Circle V0.42

by jandrews on Mar.30, 2009, under PHP Development

OK new version of Full Circle.  This one includes the ability to include links at the bottom of your blog posts.  Just another step in making Full Circle the only plugin you need to advertise your blog posts on your social networks.  I’d be interested in what other networks people think they need to have included.  Friend of mine mentioned linked in, so I am going to go look at linkedin’s api and see what I come up with.  Who knows that may be v0.5

The new version is available here.  http://wordpress.org/extend/plugins/full-circle/

8 Comments :, , more...

Full Circle v0.3

by jandrews on Mar.30, 2009, under PHP Development

New Version of Full Circle is now available at the WordPress plugin archive.  Plugin now offers the ability to connect to your Facebook feed and your Twitter feed.  All done in one handy dandy little plugin instead of 2.

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