Archive

Archive for April, 2012

Startups Find Talent with Games and Contests | Dice Blog Network

Startups Find Talent with Games and Contests | Dice Blog Network.

 

Advertisements

JFA Report (Just Freekin Awesome!): Custom R2-D2 Robot at Auto Show

April 21, 2012 3 comments

Bumblebee Cake

2012 Lone Star Round Up – RD-D2 just chillin
This one is for the Star-Wars officionados.  I’m not sure if this is a custom built or this, if it’s stock or from a kit.  Couldn’t find any information through google either.  I’d love to hear your thoughts on this little guy and would you buy one if they were for sale?!

Drawing Shapes in Android

April 19, 2012 1 comment

Android Research Blog

In this post I would like to share my findings about defining and using shapes.  A shape is an XML file that defines a geometric shape, including strokes, colors and gradients

Android Shapes

To define a shape:

1. Create a new Android XML file in the folder res/drawable

2. Make sure the root element of the file is <shape >. (If it’s not, then change it manually)

3. Inside the <shape> element, press CTRL + Space to reveal all the available elements you can use to define a shape:

android shapes options

As you can see, the elements are pretty self explanatory. To reveal the attributes of an element, put the cursor inside that element and press CTRL + Space:

android shape corner

4. Once the shape is defined, you can specify it as a background resource to any view: android:background=”@drawable/myshape”

Example:

res/drawable/boxbg.xml:

 

res/drawable/box2bg.xml:

 

res/layout/main.xml:

 

The final output should look like this:

android shape

View original post

Categories: .Of Interest

Facebook Obsoletes Less Than Year-Old Facebook Application Development For Dummies Book!

Facebook Obsoletes Less Than Year-Old Facebook Application Development For Dummies Book!

2012.04.17

Just a quick rant here.  In what must be some kind of record, Facebook has pretty much made obsolete the book Facebook Application Development For Dummies (Wiley, by Jesse Stay), which I purchased about 7 months ago (August 2011 — only 3 months after it was released!).

Most of my other programming books (mostly O’Reilly, Head First, Head Rush, etc.) are good for at least 5 years before they are useless.  But in this case, Facebook has redesigned their developer portal so much that where once there was a canvas page and canvas url, now only the canvas url is present.  Not only that, when you hover over secure canvas url it says that a secure canvas url is required by October 2011 (Oops! — That deadline has passed).

Such is the developer’s roadmap of progress.  I want to look at this with an open mind, and as such I’ll take this as an opportunity to look for more recent online Facebook Application Development tutorials.  I found one on Facebook’s developer site here.  I’ll try to follow that but right now my ass hurts from hemorrhoids and sitting in an uncomfortable chair too long (an upcoming article will aggregate reviews of several task chairs, ergo and otherwise — WATCH FOR IT!) so I’m retiring for the moment.

Until next time …

– CodeSlayer2010

Zemanta Puts Unwanted HTML Comments In Pre-Formatted PHP Code (Sourcecode) In WordPress Posts

April 9, 2012 4 comments

PROBLEM:

I  have recently determined through trial and error and process of elimination that Zemanta is responsible for adding extraneous HTML comments to my pre-formatted (with “pre” tags) PHP code that I have placed between “sourcecode” tags to activate WordPress.com’s built in programming code syntax highlighting.

Here is a screenshot (fig 1.) that demonstrates the problem:

codeslayer2010 blog with zemanta extraneous html comment errors

fig. 1

See, all that green is what commented my code out and made it so it doesn’t display correctly.

I am not totally sure Zemanta is to blame, but it seems the most likely culprit.  Here’s why:

  1. Identified 3 possible suspects: Zemanta, WordPress.com proofreading, and pasting pre-formatted code in Visual view instead of HTML view.
  2. Disabled Zemanta and retested –  problem remained.
  3. With Zemanta disabled, disabled proofreading as well.  No change.
  4. With Zemanta and proofreading disabled, went Edit>HTML View.  In each instance where I had posted code, deleted all the code from in between “sourcecode” tags.
  5. Re-copied and pasted code from each original PHP file.  Re-tested: Worked!  Syntax highlighting works properly now.  Made multiple edits to both code and text, updated and viewed the post — Good to go every time.
  6. Re-enabled Zemanta, made a test edit, saved and previewed the new post.  Everything is messed up again!  This is why I think Zemanta is the issue.

SOLUTION:

The solution I have come up with is this.

  1. If you want to use Zemanta, do so before adding any preformatted code or sourcecode tags.  Always turn Zemanta off when you are done using it.
  2. DO NOT, (under any circumstances) have Zemanta enabled if you have preformatted PHP code in your blog.  Chances are, Zemanta will mess your code up and you will have to re-paste your code again.  During my tests, I have had to paste all my sourcecode from scratch 3 times and let me tell you — it gets very tedious and wastes valueable time.
  3. Always paste sourcecode in HTML view only.

These are my recommendations based on my personal experience with this issue.  I’d love to hear from anyone else who is having similar issues.  Please let me know if this solution worked for you!

– CodeSlayer2010

Building A PHP Database Connection Class From Scratch (using Singleton & ActiveRecord patterns)

April 8, 2012 11 comments

EH_DESIGN_2012.04.03_PhpMySQLLogo_07Greetings folks.

Tonight I’m studying on how to create a PHP Object-Oriented MySQL database connection class.  This will be reusable and able to be implemented in any database driven web application built with PHP & MySQL.  It uses the ActiveRecord and Singleton design patterns.

I’m currently following this YouTube tutorial: Using OOP PHP to select, update, and insert data in a mysql database. Nick Huot really does a superb job of explaining step-by-step, not only what he is doing, but also why.

(24 hrs later …)

So I am pleased to tell you, dear reader, about the successful PHP OOP Database Connection class I built last night, based on Huot’s tutorials — they really are the best I’ve seen.

I had a lot of debugging to do, but that was simple.  Nick had not provided all the code for PHP validating of variables and such.  I’m sure he would have but he probably ran short on time, so no worries.  The validations I came up with (making sure rows had values and were of type array, for instance), I have provided for you below in hopes it will save some of you some time.  If you choose to implement my code examples you may find that there are still validations I had not accounted for.  I’d love to hear of any bugs that you find, or improvements that you make on this example, or on your own projects derived from this code.

COMPLETE WORKING EXAMPLE:

config.php

<?php  
   // Database Constants
   define("DB_HOST", "localhost");
   define("DB_USER", "root");
   define("DB_PASS", "[your_db_pass]");
   define("DB_NAME", "[your_db_name]");
?>
database.class.php

<?php
require_once('../../viewutilities.php');

class database
{
   private $host;
   private $user;
   private $pass;
   private $dbName;
   
   private static $instance;
   
   private $connection;
   private $results;
   private $numRows; // optional
   
   private function __construct()
   {      
   }
   
   // singleton pattern
   static function getInstance()
   {
      if(!self::$instance)
      {
         self::$instance = new self();
      }
      return self::$instance;
   }
   
   function connect($host, $user, $pass, $dbName)
   {
      $this->host = $host;
      $this->user = $user;
      $this->pass = $pass;
      $this->dbName = $dbName;
      
      $this->connection = mysqli_connect($this->host,
                                         $this->user,
                                         $this->pass,
                                         $this->dbName);
   }
   
   public function doQuery($sql)
   {      
      $this->results = mysqli_query($this->connection, $sql);
      
      if ($this->results && $this->results->num_rows > 0) {
                  
         // NOTE: The line below was giving me this error:
         // "Trying to get property of non-object"
         //$this->numRows = $this->results->num_rows;
         
         $this->numRows = $this->results->num_rows;

      } // end if
   }
   
   public function loadObjectList()
   {
      $obj = "No Results";
            
      if ($this->results)
      {
         $obj = mysqli_fetch_assoc($this->results);
      }
      return $obj;
   }
   
} // end class

?>
table.class.php

<?php
require_once('../../viewutilities.php');

/**
 * Uses Active Record design pattern to implement a class that
 *  closely matches a given database table.  This class contains
 *  methods for binding the class members and methods to CRUD
 *  database management principles.  This type of class is sometimes
 *  referred to a "Table Model", and in many designs it is implemented
 *  as an abstract base class, which is then extended and specialized
 *  as necessary.
 */
class table
{
   protected $id = null;
   protected $table = null;
   
   function __construct()
   {
      
   }   
   
   /**
    * Takes assoc array as param $data.  Dynamically creates class properties
    *  with key as property name and value as its value.
    *
    * @param array $data key value pairs
    * @return
    * @access
    */
   function bind($data)
   {
      //print_r($data);
      foreach ($data as $key=>$value)
      {
         $this->$key = $value;
         //echo $key."--".$value;
      }
   }
   
   function load($id)
   {
      $this->id = $id;
      $dbo = database::getInstance();
      $sql = $this->buildQuery('load');
      
      $dbo->doQuery($sql);

      $row = $dbo->loadObjectList();
      
      if (isset($row) && is_array($row))
      {
         foreach ($row as $key=>$value)
         {
            if ($key == "id")
            {
               continue;
            }
            $this->$key = $value;            
         }               
      }

   }
   
   // write database object contents to database
   function store()
   {
      $dbo = database::getInstance();
      $sql = $this->buildQuery('store');
      $dbo->doQuery($sql);
   }
   
   protected function buildQuery($task)
   {
      $sql = "";
      if ($task == "store")
      {
         // if no id value has been store in this object yet,
         //  add new record to the database, else just update the record.
         if ($this->id == "")
         {
            $keys = "";
            $values = "";
            $classVars = get_class_vars(get_class($this));
            $sql .= "INSERT INTO {$this->table}";
            
            foreach ($classVars as $key=>$value)
            {
               if ($key == "id" || $key == "table")
               {
                  continue;
               }
               
               $keys .= "{$key},";
               $values .= "{$value},";
            }
            
            // NOTE: all substr($keys, 0, -1) does is gets rid of the comma
            // at the on the last array element.
            $sql .= "(".substr($keys, 0, -1).") Values (".substr($values, 0, -1).")";
            
         }else{
            
            $classVars = get_class_vars(get_class($this));
            $sql .= "UPDATE {$this->table} SET ";
            foreach ($classVars as $key=>$value)
            {
               if ($key == "id" || $key == "table")
               {
                  continue;
               }
               
               $sql .= "{$key} = '{$this->$key}'";
               
            }
            $sql .= substr($sql, 0, -2)." WHERE id = {$this->id}";
            
         }         
      }
      elseif ($task == "load")
      {
         $sql = "SELECT * FROM {$this->table} WHERE id = '{$this->id}'";
      }
      return $sql;      
   }
   
   
} // end class

?>
user.class.php

<?php
class user extends table
{
   var $fname = null;
   var $lname = null;
   var $email = null;
   var $table = "user";
}
?>
index.php

<?php
include('./database.class.php');
include('./table.class.php');
include('./user.class.php');

require_once('../../viewutilities.php');

// this has my connection constants
include('./config.php');

$dbo = database::getInstance();
$dbo->connect(DB_HOST, DB_USER, DB_PASS, 'testpatterns');
//$dbo->connect('localhost', 'root', '', 'testPatterns');

$user = new user();
$user->load('1');

echo "Name of user 1: {$user->fname} {$user->lname}<br />";
?>

Now in the interest of time, I’m publishing this post.  Soon I will add commentary and explanations and such, however, experienced Object-Oriented PHP developers will understand how to code fits together without explantion.  The best I can do for the newbies for now is to say if your database exists, has data matching the format the class is looking for , and is running you should be able to put all these files in your PHP Apache web directory (I’m using WAMP and on my system it is in C:\wamp\www; on yours it may be different), and launch index.php to see a first and last name pulled from one row of data, specified by id number, in your MySQL database.

Good luck, and I’d love to hear from you if you have questions, comments, or issues that you ran into.

– CodeSlayer2010

P.S.: Go ahead and comment out the requires and includes for “viewutilities.php”. If you don’t, the code will not work because it will complain that it can’t find this file.

I left it in there because I will be publishing it later on and you may find it useful debugging tool for your PHP scripts. This class is a wrapper for print_r() function and there are all kinds of advantages, the least of which is one line of code vs three with all your pre-tag. But this class and it’s static “debug” method streamline and simplify debugging complex AMP-stack PHP-based web applications. Watch for it soon!

Facebook and the Creep Factor

Social Media App - Girls Around Me

Here’s an interesting article from Kevin Drum over at MotherJones.com that may motivate some users of Facebook and other Social Network apps to monitor their security & privacy settings more closely:

Facebook and the Creep Factor.