Friday, December 25, 2015

The way to pass data to external form validation in Codeigniter 2.x.x

External form validation was very important in Codeigniter besides the default form validation since some requirements on programming are above from default settings. Here are the ways on how to set external validation with passing data and return the results. Hope it helps.


Let say I need to pass data from an object retrieved from Codeigniter model as below:

$obj_calendar_data = $this->m_calendar->get_calendar_data_by_calendar_id($calendar_id);
Then I need validate based on value from database as below (in this case value of max_pax):

validation_rules('total_no_of_seat','seat',"required|numeric|callback_check_max_pax[$obj_calendar_data->max_pax]");
 In the same controller, the function for checking max_pax:

function check_max_pax($str,$second_param)
 {
    if( (int) $second_param > (int) $str):
       validation_message('check_max_pax','You can not set the number of pax less than previous number of pax.');
       return FALSE;
        endif;
 }
Note:

check_max_pax function should be the same with the input name in validation_message('check_max_pax');

Thanks to Shahrul for taught me. 

Monday, November 30, 2015

Bootbox

http://bootboxjs.com/documentation.html

Wednesday, November 11, 2015

Thursday, November 5, 2015

After import sample data 1.9.1.0 can't login admin, no admin user in database?

Run this code in query:
LOCK TABLES `admin_role` WRITE , `admin_user` WRITE;

SET @SALT = "rp";
SET @PASS = CONCAT(MD5(CONCAT( @SALT , "password") ), CONCAT(":", @SALT ));
SELECT @EXTRA := MAX(extra) FROM admin_user WHERE extra IS NOT NULL;

INSERT INTO `admin_user` (firstname,lastname,email,username,password,created,lognum,reload_acl_flag,is_active,extra,rp_token_created_at) 
VALUES ('Firstname','Lastname','email@example.com','myuser',@PASS,NOW(),0,0,1,@EXTRA,NOW());

INSERT INTO `admin_role` (parent_id,tree_level,sort_order,role_type,user_id,role_name) 
VALUES (1,2,0,'U',(SELECT user_id FROM admin_user WHERE username = 'myuser'),'Firstname');

UNLOCK TABLES;
In admin site:
username: myuser
password: password 

Magento

https://en.wikipedia.org/wiki/Magento

Tuesday, October 27, 2015

Sunday, October 25, 2015

Codeigniter - The best practice in _remap() function

Instead of using like this:

function _remap($method)
{
         $array = array('method1','method2','method3','method4','method5','method6','method7',...);
         in_array($method, $array) ? $this->method() : $this->index()
}


to


function _remap($method)
    {
        $arr_raw_method_registered = get_class_methods(__CLASS__);
        $arr_remove_element = array('get_instance');
        $arr_method_registered = array_filter($arr_raw_method_registered, function($item)use($arr_remove_element) 
        {
            if (in_array($item, $arr_remove_element) || $item[0] == '_'):
                return false; 
            else:
                return true;
            endif;
        });
        
        in_array($method, $arr_method_registered) ? $this->$method() : $this->index();
    }

The function covers all defined function on that class.



Monday, October 19, 2015

Regex to Catch image in img tag

<img .*?src="[^"]*\/\K([^"]*?\.(?:jpeg|jpg|bmp|gif|png))

My Name in Regex!

^([\+?I?])([\+?n?])([\+?d?])([\+?e?])([\+?r?])([\+?a?])(\d{0,9})$

Useful websites to learn Regex

Basic Tutorial
  1. http://www.macronimous.com/resources/writing-regular-expression-with-php.asp
Regex Website Tester
  1. https://regex101.com/

Regex to Valid Phone Number (Malaysia)

     function valid_phone_num($str)
     {
     if (!preg_match("/^(\+?6?01)[0|1|2|3|4|6|7|8|9]\-*[0-9]{7,8}$/", $str)):
       $this->form_validation->set_message('valid_phone_num', 'Please insert a valid phone number format. Example : 0191234567.');
       return FALSE;
     else:
       return TRUE;
    endif;
    }

Wednesday, October 14, 2015

Codeginter RollBack

https://ellislab.com/codeigniter/user-guide/database/transactions.html

Wednesday, October 7, 2015

WebcamJS

https://github.com/jhuckaby/webcamjs

Monday, September 28, 2015

50+ Best Free Bootstrap Admin Templates

http://www.cssauthor.com/bootstrap-admin-templates/

Keyboard Event Handling: onkeydown

<script type="text/javascript">
 /*
    To check key number for keyevent
    function keydownHandler(e) {
    var evt = e ? e:event;
    var keyCode = evt.keyCode;
    var sModifiers = ''
        +(evt.ctrlKey  ? 'Ctrl ' :'')
        +(evt.shiftKey ? 'Shift ':'')
        +(evt.altKey   ? 'Alt '  :'') ;

    alert('keyCode='+keyCode);
    if (keyCode==16 || keyCode==17 || keyCode==18)
         alert(' key: '+sModifiers);
    else
         alert(' key: '+sModifiers+fromKeyCode(keyCode));
  }

  //Register the keydown event handler:
  document.onkeydown=keydownHandler;
  */
</script>

$(document).bind('keydown', function(e) {
        if (e.ctrlKey && e.shiftKey && e.which === 78) {
            e.preventDefault();
            skip_question(<?= get_session('question_id') ?>)
        }
        if (e.which === 49) { //OPTION A
            e.preventDefault();
            alert(1);
        }
        if (e.which === 50) { //OPTION B
            e.preventDefault();
            alert(2);
        }
        if (e.which === 51) { //OPTION C
            e.preventDefault();
            alert(3);
        }
        if (e.which === 52) { //OPTION D
            e.preventDefault();
            alert(4);
        }
    });

Thursday, August 13, 2015

Save Mysql table -> field, type, comment to csv

Mysql statement to save table field, type, comment to csv
SELECT
 
    column_name,
    column_type,
    column_default,
    column_comment
 
FROM information_schema.columns
WHERE table_name='c_product_detail'
INTO OUTFILE 'C:/Users/Imran/Desktop/work/table.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
;