Skip to content

Instantly share code, notes, and snippets.

@hendrasyp
Last active November 11, 2015 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hendrasyp/b60477755dbef5acae25 to your computer and use it in GitHub Desktop.
Save hendrasyp/b60477755dbef5acae25 to your computer and use it in GitHub Desktop.
Function to generate Format autonumber using CodeIgniter and YII Frameworks
<?php
function _autonumber($field, $table, $Parse, $Digit_Count) {
$NOL = "0";
$query = "Select " . $field . " from " . $table . " where " . $field . " like '" . $Parse . "%' order by " . $field . " DESC LIMIT 0,1";
// CODEIGNITER
$identifier = &get_instance();
$data = $identifier->db->query($query)->result_array();
// YII Way
// $identifier = \Yii::$app->db;
// $data = $identifier->createCommand($query)->queryOne();
$counter = 2;
if (sizeof($data) == 0)
{
while ($counter < $Digit_Count):
$NOL = "0" . $NOL;
$counter++;
endwhile;
return $Parse . $NOL . "1";
}
else
{
//CODE IGNITER
$R = $data[0][$field];
// YII
// $R = $data[$field];
$K = sprintf("%d", substr($R, -$Digit_Count));
$K = $K + 1;
$L = $K;
while (strlen($L) != $Digit_Count)
{
$L = $NOL . $L;
}
return $Parse . $L;
}
} // end of function
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment