2013-01-14 121 views
2

好了,這裏是我的功能:爲什麼我的函數不返回返回值?

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語句之前立即使用我的調試回聲,好像它消除了在返回語句之前出錯的任何可能性,但我現在不知道。

編輯:只是要清楚,3005是我期望從我的數據庫在這一點上的價值。這是爲了在工作中設置工作號碼,其始終是Zxxx,其中Z是一年中的最後一位數字。這些都是按順序創建的,但對於超過一年的作業,我們只更改Z,所以這是我用來解決3030可以(而且確實)在創建3000之前存在的代碼的代碼。

回答

4

你是遞歸調用的功能,但你不與返回值做任何事情:

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; 
    } 

應該是這樣的:

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 
     return getNewJobNumber($jobPrefix, $addition+1); 
     ^^^^^^ 
    } else { 
     return -1; // some kind of error message? 
    } 
+1

另一個小提示:你在開始時增加加法變量,然後傳遞(再次增加)值給自己,看起來可能是一個錯誤。 – jeveloper

+1

謝謝你,這是積極的問題,我明白爲什麼現在也有道理。你也是對的增量。爲了測試它可以跳過多於一個,我讓數據庫設置爲跳過兩個,但沒有意識到它是兩個跳過的巧合。現在也是如此,非常感謝你的幫助。 –

4

當你調用

getNewJobNumber($jobPrefix, $addition+1);

您實際上並不返回值。

更改它

return getNewJobNumber($jobPrefix, $addition+1);

+0

謝謝,這個解決方案是絕對正確的。只是因爲更詳細的信息給對方支票,並指出我的其他問題。非常感謝你的回覆,我真的很感激。 –