Nasrul Hazim Bin Mohamad



Year 2016, InsyaAllah, next year to be a year of sharing knowledge of what I’ve learnt so far. Following are the trainings I would like to conduct:

1. Mobile Apps Development with jQuery Mobile
2. Mobile Apps Development with Intel XDK (incoming)
3. API Development with Slim Framework (incoming)
4. PHP Basic / Advanced
5. Web Development with CakePHP 3 (incoming)
6. Web Development with Laravel 5 (incoming)
7. Web Development with Yii Framework 2 (incoming)
8. WordPress for Developers – Beginner Level
9. WordPress Theme Development
10. WordPress Plugin Development

Those status incoming, it’s the one I’m preparing the syllabus & materials, summarizing the process, and so on. Hope more things can be share other than mentioned above such as the tools I’ve used along the process of the development – Sublime Text 3, SQLYog, Putty, Git and so on

CakePHP 2.x: Pagination Element


Being working on Bootstrap Pagination with CakePHP. Here the final one. Use it as you needed.


  1. CakePHP 2.x Pagination
  2. Bootstrap Pagination
  3. CakeDC Users
  	<ul class="pagination">	
		echo $this->Paginator->prev('< ' . __d('users', 'previous'), array('tag' => 'li'), null, array('disabledTag' => 'span', 'class' => 'disabled'));
		echo $this->Paginator->numbers(array(
			'separator' => '',
			'tag' => 'li',
			'currentTag' => 'span',
			'currentClass' => 'active'
		echo $this->Paginator->next(__d('users', 'next') . ' >', array('tag' => 'li'), null, array('disabledTag' => 'span', 'class' => 'disabled'));

Training – CakePHP Basic


MySQL: Save Arabic Characters / Words


Database / Table for Arabic Usage

  1. Charset – UTF-8
  2. Collation – utf8_unicode_ci


# Database
CREATE DATABASE `db_arabic`CHARACTER SET utf8 COLLATE utf8_unicode_ci;

# Table
CREATE TABLE `db_arabic`.`users`( `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(255), `created` DATETIME, `modified` DATETIME, PRIMARY KEY (`id`) ) CHARSET=utf8 COLLATE=utf8_unicode_ci; 

In CakePHP, make sure to set 'encoding' => 'utf8' in database.php.

You can have Arabic Lorem Ipsum at Multilanguage Lorem Ipsum Generator

For web page, please ensure to add meta tag, charset `utf-8′, as following:

&lt;meta charset=&quot;utf-8&quot; /&gt;

Reference: How to save Arabic Words Into MySQL Table

CakePHP: Running Shell Script with Cronjob


Create a Shell Script

Create a ScheduleShell.php in cakephp/app/Console/Command.

class ScheduleShell extends Shell {
    function main() {
    	$this-&gt;out('Hello World');

Add a cronjob

sudo crontab -e

I have a cronjob running every 5 minute for ScheduleShell.php

# cronjob run every 5 minute
*/5  *    *    *    * cd /var/www/html/cakephp/app &amp;&amp; Console/cake schedule

HTML: Editable Elements


I’m using the following:

  1. X-editable
  2. Bootstrap 2
  3. Cakephp / Any PHP Framework

Include the jQuery, Bootstrap 2 in your HTML head

    &lt;!-- jQuery --&gt;
    &lt;script src=&quot;//;&gt;&lt;/script&gt;
    &lt;script src=&quot;//;&gt;&lt;/script&gt;

    &lt;!-- Bootstrap --&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;link href=&quot;css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; media=&quot;screen&quot;&gt;
    &lt;script src=&quot;js/bootstrap.min.js&quot;&gt;&lt;/script&gt;

    &lt;!-- X-Editable --&gt;
    &lt;link href=&quot;//; rel=&quot;stylesheet&quot;/&gt;
    &lt;script src=&quot;//;&gt;&lt;/script&gt;

The View

&lt;a href=&quot;#&quot; class=&quot;x-editable&quot; data-type=&quot;text&quot; data-pk=&quot;1&quot;&gt;Some value 1&lt;/a&gt;
&lt;a href=&quot;#&quot; class=&quot;x-editable&quot; data-type=&quot;text&quot; data-pk=&quot;2&quot;&gt;Some value 2&lt;/a&gt;

The JavaScript

$(document).ready(function() {
    $.fn.editable.defaults.mode = 'inline';

        url: 'something/update'

The Controller

App::uses('AppController', 'Controller');

class Something extends AppController {

    public function update()
        /* Data structure we received from x-editable
                [name] =&gt; 
                [value] =&gt; Some value 1
                [pk] =&gt; 1
        $data = $this-&gt;request-&gt;data;
        $this-&gt;Some-&gt;read(null, $data['pk']);
        $this-&gt;Some-&gt;set('value', $data['value']);
        // return JSON if u need it or render a HTML (need to handle on success event)


  1. X-editable

Angular X-editable –

CakePHP: Upload File


The Model


App::uses('AppModel', 'Model');

class Picture extends AppModel {
	public $useTable = 'picture';
	public $primaryKey = 'id';
	public $name = 'Picture';
	public $order = ' ASC';

	protected $_displayFields = array(

The Database

CREATE TABLE `picture` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `link` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)

The View

	echo $this-&gt;Form-&gt;create('Upload',array(
		'enctype' =&gt; 'multipart/form-data',
		'url' =&gt; array(
			'controller' =&gt; 'upload', 
			'action' =&gt; 'index'
	echo $this-&gt;Form-&gt;file('file');
	echo $this-&gt;Form-&gt;end(array(
		'label' =&gt; 'Submit',
		'class' =&gt; 'btn btn-success'

The Controller


App::uses('AppController', 'Controller');

class UploadController extends AppController { 
	public function index() {
                // in this example, we're trying to accept gif,jpeg and png

                $image = $this-&gt;request-&gt;data['Upload']['file'];

                $imageTypes = array(&quot;image/gif&quot;, &quot;image/jpeg&quot;, &quot;image/png&quot;);

                $uploadFolder = &quot;img&quot; . DS . &quot;photos&quot; . DS;

                $uploadPath = WWW_ROOT . $uploadFolder;

                $id = null;

                        $ext = substr(strtolower(strrchr($image['name'], '.')), 1); //get the extension

                        if(in_array($image['type'], $imageTypes))
                                $date = date_create();
                                $rand = rand(100000,999999);
                                $link = 'pic_' . date_format($date, 'U') . '_' . $rand . '.' . $ext;

                                move_uploaded_file($image['tmp_name'], $uploadPath . $link);

                                $this-&gt;Picture-&gt;set(array('name' =&gt; $image['name'], 'link' =&gt; $link));
                                $id = $this-&gt;Picture-&gt;id;
                                $this-&gt;Session-&gt;setFlash('Picture uploaded!');
                                $this-&gt;Session-&gt;setFlash('Only files with extension .gif, .png and .jpeg are allowed to be upload.');


CakePHP: PHPMailer Component – Using GMail SMTP



  1. CakePHP 2.x
  2. PHPMailer

Download PHPMailer & extract it to APP/vendors.

Add following lines in APP/vendors/phpmailer/class.phpmail.php line 542, in ELSE statement. This to ensure we have a secured domain through SSL / TLS.

// add a variable named SMTPSecure - SSL / TLS
var $SMTPSecure

// line 542 
    $secure = $this-&gt;SMTPSecure . '://';

Create a component called EmailComponent in APP/app/Controller/Component


App::uses('Component', 'Controller');
App::import('Vendor', 'phpmailer', array('file' =&gt; 'phpmailer'.DS.'class.phpmailer.php'));

class EmailComponent extends Component {

  public function send($to, $subject, $message) {
    $sender = &quot;;; // this will be overwritten by GMail

    $header = &quot;X-Mailer: PHP/&quot;.phpversion() . &quot;Return-Path: $sender&quot;;

    $mail = new PHPMailer();

    $mail-&gt;Host = &quot;;; 
    $mail-&gt;SMTPAuth = true;
    $mail-&gt;SMTPSecure = &quot;ssl&quot;;
    $mail-&gt;Port = 465;
    $mail-&gt;SMTPDebug  = 2; // turn it off in production
    $mail-&gt;Username   = &quot;[yourGMailAccount];;  
    $mail-&gt;Password   = &quot;password&quot;;
    $mail-&gt;From = $sender;
    $mail-&gt;FromName = &quot;From Me&quot;;



    $mail-&gt;Subject = $subject;
    $mail-&gt;Body    = nl2br($message);
    $mail-&gt;AltBody = nl2br($message);

    // return an array with two keys: error &amp; message
    if(!$mail-&gt;Send()) {
      return array('error' =&gt; true, 'message' =&gt; 'Mailer Error: ' . $mail-&gt;ErrorInfo);
    } else {
      return array('error' =&gt; false, 'message' =&gt;  &quot;Message sent!&quot;);


Create a controller, ExampleController.php to test out our EmailComponent


App::uses('AppController', 'Controller');

class ExampleController extends AppController {
	public $components = array('Email');
	public function index() {
		// just to test out the sending email using SMTP is OK, create a method that will be able to access from public
		$to = '[]';
		$subject = 'Hi buddy, i got a message for you.';
		$message = 'Nothing much. Just test out my Email Component using PHPMailer.'
		$mail = $this-&gt;Email-&gt;send($to, $subject, $message);

When you navigate to http://localhost/APP/Example, the email should send to []. Please do check the mailbox. 🙂