Okay, so here’s my function:
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;
}
}
}
}
here’s my call:
$ourNewJobNumber = getNewJobNumber($_POST['txtJobPrefix'], 0);
echo ":}" . $ourNewJobNumber . "{:<br />";
and here’s my result:
return:nextJobNumber-3005
:}{:
The code is executing perfectly, pulling values out of the database and comparing them and doing everything just the way I want it to. It is getting the correct value in every circumstance I can test, but it just outright refuses to return that value back to the calling script. Does anyone see any stupid errors that I have glossed over? Having my debug echo immediately before my return statement seems as if it eliminates any possibility of it going wrong before the return statement but I just don’t know at this point.
Edit: Just to be clear, 3005 is the value I am expecting out of my database at this point. This is to set up job numbers at work which are always Zxxx where Z is the last digit of the year. These are always created sequentially, but for jobs that span more than one year we only change Z so this is the code I use to work around the fact that 3030 can (and does) exist before 3000 is ever created.
You are calling your function recursively but you are not doing anything with the return value:
Should be something like: