Author Archives: Robert

Easily convert MySQL database collations and alter character sets

I can’t remember what all I looked at for references, but this completed tool will generate the SQL  commands necessary to convert your database, tables and fields from any collation and character set into another.

It goes without saying to make a backup of your database before running such commands. If you don’t make a backup first, shame on you 😛 Continue reading

Simple PHP function to convert hexadecimal colors to RGB (red, green, blue) decimal values

This function will convert hexadecimal colors with a length of 6, 3, 2, or 1 into their RGB decimal equivalents. If a value cannot be converted, it defaults to black…

function hex2rgb($hex = NULL) {
	preg_match('/^#{0,1}([0-9a-f]{1,6})$/i', $hex, $matches);
	if ($hex[0] == '#') $hex = substr($hex, 1);
	$length = @strlen($matches[1]);
	switch ($length) {
		case 6:
			$rgb = array($hex[0] . $hex[1], $hex[2] . $hex[3], $hex[4] . $hex[5]);
			break;
		case 3:
			$rgb = array($hex[0] . $hex[0], $hex[1] . $hex[1], $hex[2] . $hex[2]);
			break;
		case 2:
			$rgb = array($hex[0] . $hex[1], $hex[0] . $hex[1], $hex[0] . $hex[1]);
			break;
		default:
			$hex = ($length == 1) ? $hex . $hex : 0;
			$rgb = array($hex, $hex, $hex);
			break;
	}
	return array(
		'r' => hexdec($rgb[0]),
		'g' => hexdec($rgb[1]),
		'b' => hexdec($rgb[2])
	);
}

Short, simple and sweet 🙂

How-To: Reload verification image using Codeigniter 2 CAPTCHA helper

Out of the box, CodeIgniter has a good CAPTCHA helper to use in your web applications. Here is a simple example of how to get a new CAPTCHA image without having to reload the page 😉

Basic Controller

class Welcome extends CI_Controller {

    private $captcha_path = 'assets/img/captcha/';

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->helper(array('captcha'));
        $captcha = create_captcha(array(
            'word'        => strtoupper(substr(md5(time()), 0, 6)),
            'img_path'    => $this->captcha_path,
            'img_url'    => $this->captcha_path
        ));
        $data = array(
            'captcha'        => $captcha
        );
        $this->session->set_userdata('captcha', $captcha['word']);
        $this->load->view('welcome', $data);
    }

}

The View (jQuery used for UI buttons and altering CAPTCHA image ‘src’ attribute)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/blitzer/jquery-ui.css">
<script>
$(function(){
    $('#new_captcha').button({
        text: false,
        icons: {
            primary: 'ui-icon-refresh'
        }
    }).click(function(event){
        event.preventDefault();
        $(this).prev().attr('src', 'welcome/new_captcha?'+Math.random());
    });
});
</script>
<?php echo $captcha['image']; ?>
<a href="#" id="new_captcha">Reload CAPTCHA</a>

Modified Controller Continue reading

Extending the CodeIgniter URI class to simplify pagination base_url option

In a lot of my controllers, I use pagination along with the HTML <base> tag’s href attribute for relative link URLs.

Example URL: http://www.domain.com/admin/users/index/pagination_offset

HTML Base tag: <base href=”http://www.domain.com/admin/”>

What this means, is I usually have to build pagination’s base_url option based on where I am and what information is being passed in the URI. A lot of times it ends up looking something like…

$this->load->library('pagination', array(
    'base_url'       => $this->uri->segment(2) . '/' . $this->uri->segment(3) . '/' . $this->uri->segment(4),
    'total_rows'     => $total_rows,
    'per_page'       => 20,
    'uri_segment'    => 4
));

What I decided to do what extend the core CodeIgniter URI class to provide a basic slice method to grab a chunk of the URI by applying PHP’s array_slice function to the segments property of the URI class. Continue reading

Use JavaScript (using jQuery) to add support for HTML5 placeholder attribute in form fields

Some browsers do not support the placeholder attribute added in HTML5. This attribute is useful for field hinting such as input formats for dates, phone numbers, and the like. If you see text in the field below, your browser supports placeholder, if not, add the code below to mimic the behavior 😉

Example field:

JavaScript (jQuery) workaround: Continue reading