PHP

That’s how easy it is to create Twitter with Kohana 3

Posted by on Aug 25, 2011 in Coding, PHP | 0 comments

As Steve Krug would call it, some "happy talk"

Kohana has become my PHP framework of choice ever since I was forced to start using it about 9 months ago. Derived from CodeIgniter (interestingly enough I can't confirm that since, once again, Kohana's Wikipedia page has been deleted), Kohana has made several advancements and is now a fully capable framework I've come to love.

This article shows one of the reasons for that, namely how quick, easy and (arguably) elegant it is to create a web app from scratch using Kohana. The app we'll build is an extremely basic one, but coding it from scratch, without a framework… I don't even want to imagine.

The post is in the form of a tutorial and I'll write as if giving instructions to You.

Project Summary

Image representing Twitter as depicted in Crun...
Image via CrunchBase

We'll be building a very basic Twitter-type microblog application. The main functionalities of the site include:

  • Login
  • Making a micro-post (ie tweeting)
  • Viewing a list of past micro-posts
  • Deleting a post
  • Logging out

We'll use Twitter's own Bootstrap toolkit (hey, what the heck, we're stealing their bread anyhow, why not in guns blazing?) that does all the heavy lifting on the CSS / design part. We won't be using any Javascript since that would be a post on it's own.

Setting it all up

[To be written, as soon as I have the time]

Coding

[To be written, as soon as I have the time]

Conclusion

Now that you've spent some hours (it took me 3 hours to write the fully-working code you see @ github) building the app, all you need to do is hire some marketing people and travel back in time 8-or-so years – the riches await.

snapshot

 

View Project Source @ GitHub
Read More

PHP algorithm for consistent random colors

Posted by on Aug 5, 2011 in PHP | 0 comments

Here's a PHP function that returns 2 random, but (mostly) constant HEX color codes.

The function takes a single parameter – a string – and returns an array with 2 colors.

   /**
     * A function that returns a random background and font color.
     * The output is constant as long as the input string remains unchanged.
     * @author Ando Roots
     * @date August 2011
     * @copyright Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
     * @link http://sqroot.eu
     * @static
     * @param string $string A random input string that determines the output colors.
     * @return array 2 colors, index 0 being random background color, index 1 gray font color,
     * it's darkness dependant on the first color.
     * Example: array(2) { [0]=> string(7) "#62FFD5" [1]=> string(6) "#9D02A" }
     */
    public static function color($string = 'test') {
        $colors = array();
        $color = array(0, 0, 0);

        // Prepare the input string.
        $string = preg_replace('/[^0-9]/', '', md5($string));

        // Determine it's length
        if (strlen($string) > 8) {
            $length = 3;
        } elseif (strlen($string) > 5) {
            $length = 2;
        } elseif (strlen($string) > 2) {
            $length = 1;
        } else {
            // Use time if all else fails
            $length = 3;
            $string = strrev(time());
        }

        // Generate background color
        $hex = '#';
        foreach ($color as $i => $c) {
            $color[$i] = (int)substr($string, $i * $length, $length);
            if ($color[$i] > 255) {
                $color[$i] = 255;
            }
            $hex .= dechex($color[$i]);
        }
        $colors[] = strtoupper($hex);

        // Generate font color
        $hex = '#';
        $darkness = abs(255- round(array_sum($color) / 3));
        $hex .= dechex($darkness).dechex($darkness).dechex($darkness);
        $colors[] = strtoupper($hex);

        return $colors;
    }
Read More

Kohana 3.2 Validation Estonian translation

Posted by on Aug 2, 2011 in PHP | 0 comments

Allpool olev on Kohana 3.2 Validation üldiste veateadete tõlge eesti keelde.

Fail tuleb paigutada application/messages/validation.php

 

 ':field tohib sisaldada ainult tähti',
	'alpha_dash'    => ':field tohib sisaldada ainult tähti, numbreid ja sidekriipse',
	'alpha_numeric' => ':field tohib sisaldada ainult tähti ja numbreid',
	'color'         => ':field peab olema värv',
	'credit_card'   => ':field peab olema krediitkaardi number',
	'date'          => ':field peab olema kuupäev',
	'decimal'       => ':field peab olema komakohaga arv täpselt :param2 komakohaga',
	'digit'         => ':field peab olema naturaalarv',
	'email'         => ':field peab olema e-mail',
	'email_domain'  => ':field peab sisaldama kehtivat e-maili domeeninime',
	'equals'        => ':field peab olema võrdne :param2 -ga',
	'exact_length'  => ':field peab olema täpselt :param2 märgi pikkune',
	'in_array'      => ':field peab olema antud valimis',
	'ip'            => ':field peab olema ip aadress',
	'matches'       => ':field peab olema võrdne :param2 -ga',
	'min_length'    => ':field pikkus peab olema vähemalt :param2 märgi pikkune',
	'max_length'    => ':field pikkus ei tohi ületada :param2 märki',
	'not_empty'     => ':field peab olema täidetud',
	'numeric'       => ':field peab olema number',
	'phone'         => ':field peab olema telefoninumber',
	'range'         => ':field peab olema vahemikus :param2 kuni :param3',
	'regex'         => ':field ei ole nõutud formaadis',
	'url'           => ':field peab olema url',
);
Read More

T-shirt idea?

Posted by on Mar 22, 2011 in PHP | 0 comments

< ?
include('World.php');
$world = new World();
$world->me->profession = 'PROGRAMMER';

foreach ($world->day as $day) {
    $world->me->write_code($day);
    $world->me->drink_coffee(4);
    if ($world->me->had_fun) {
        $world->me->have_sex(rand(1, 6));
    } else {
        $world->me->profession = 'WAITER';
    }
}
?>
< ?

class World {
    public $day = array();
    public $me;

    public function World() {
        for ($i=1; $i <= 365; $i++) {
            $this->day[] = $i;
        }
        $this->me = new Me();
    }

}

class Me {

    public $had_fun = TRUE;
    public $profession;

    public function drink_coffee($number) {

    }

    public function have_sex($times) {
        return FALSE;
    }

    public function write_code($day) {
        echo "Day #$day of a ".$this->profession.": Wrote code, had coffe, had fun AND some sex.
";
    }
}

?>
Read More