<?php
include('includes/config.php');

$sql = "SELECT CategoryCode FROM tblcategory ORDER BY id DESC LIMIT 1";
$query = $dbh->prepare($sql);
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);

if($row) {
  $lastCode = $row['CategoryCode'];
  $num = intval(substr($lastCode, 3)) + 1;
  echo "CAT" . str_pad($num, 3, "0", STR_PAD_LEFT);
} else {
  echo "CAT001";
}
