好的,这是我的功能:
function getNewJobNumber($jobPrefix, $addition = "0") {
$addition = $addition + 1;
//echo $addition . "<br />";
$yearDate = date("Y");
$firstDigit = $yearDate[strlen($yearDate) - 1];
$db = DatabaseHelpers::getDatabaseConnection();
$jobQuery = 'SELECT jobID, jobNumber, jobPrefix FROM tblJobNumbers WHERE jobPrefix = "' . $jobPrefix . '" AND jobNumber LIKE "' . $firstDigit . '___" ORDER BY jobID DESC LIMIT 1';
//echo $jobQuery . "<br />";
$stmt1 = $db->query($jobQuery);
$stmt1->setFetchMode(PDO::FETCH_OBJ);
$firstResult = $stmt1->fetch();
//above should select the latest created job number with selected prefix
//print_r($firstResult);
$jobNumber = $firstResult->jobNumber; //top row, will be last job number
//echo "jobNumberFromDB:" . $jobNumber . "<br />";
if (!$jobNumber) {
//no job number exists yet, create one
//will be last digit of year followed by "000" ie in 2013 first
//new job number is "3000"
$newJobNumber = str_pad($firstDigit, 4, "0");
return $newJobNumber;
} else {
//job number already exists, try next one
$nextJobNumber = $jobNumber + $addition;
$nextJobQuery = 'SELECT jobID, jobNumber, jobPrefix FROM tblJobNumbers WHERE jobPrefix = "' . $jobPrefix . '" AND jobNumber = "' . $nextJobNumber . '" ORDER BY jobID DESC LIMIT 1';
$stmt2 = $db->query($nextJobQuery);
$stmt2->setFetchMode(PDO::FETCH_OBJ);
$nextResult = $stmt2->fetch();
$dbNextJobNumber = $nextResult->jobNumber;
if (!$dbNextJobNumber) {
//new job number is unique, return value
echo "return:nextJobNumber-" . $nextJobNumber . "<br />";
return($nextJobNumber);
} else {
//new job number is not unique, and therefore we need another one
if ($addition <= 99) { //don't let this recurse more than 99 times, it should never need to
//in order to loop this programatically call function again, adding one to addition factor
getNewJobNumber($jobPrefix, $addition+1);
} else {
return;
}
}
}
}
这是我的电话:
$ourNewJobNumber = getNewJobNumber($_POST['txtJobPrefix'], 0);
echo ":}" . $ourNewJobNumber . "{:<br />";
这是我的结果:
return:nextJobNumber-3005
:}{:
代码执行完美,从数据库中提取值并比较它们,并按照我想要的方式做所有事情。在我可以测试的每种情况下,它都会获得正确的值,但它完全拒绝将该值返回给调用脚本。有没有人看到我掩盖的任何愚蠢错误?在我的 return 语句之前立即进行调试回显似乎消除了在 return 语句之前出错的任何可能性,但我现在还不知道。
编辑:为了清楚起见,3005 是我现在期望从我的数据库中得到的值。这是为了在工作中设置工作编号,该编号始终为 Zxxx,其中 Z 是年份的最后一位。这些总是按顺序创建的,但对于跨越一年以上的工作,我们只更改 Z,所以这是我用来解决 3030 可以(并且确实)在创建 3000 之前存在的事实的代码。