這很簡單:
首先你想做的事就是在數據庫中查詢以前的註冊號/ ID。
是這樣的:
$query = mysqli_query($connect,'SELECT regCode From data ORDER BY regCode DESC LIMIT 1');
那麼你檢查是否有任何這樣的結果:
$results = count(mysqli_num_rows($query));
如果$結果返回0(這是一個全新的數據庫)這樣做: $regCode = 'A0001';
其他:
CRE吃了一連串的字母。
$alphabet = ['B','C','D','E']; #from B to Z (A is omitted because it is the default prefix)
恢復之前的註冊碼是這樣的:
list($regCode) = mysqli_fetch_row($query);
從這裏單獨從數字
$prefix = substr($regCode,0,1); //get prefix/alphabet
$storage_id = (int) substr($regCode,1,4) +1; //get code and add 1
現在測試在我們的例子是1000的極限字母(或任何你想要的號碼):
$limit = 1000
if($storage_id>$limit){
$storage_id= '0001'; //if limit exceeded reset storage id
$index = array_search($prefix,$alphabet); //get alphabet/prefix index in the $alphabet array
if($index>=0){
$prefix = $alphabet[$index+1]; //Get next alphabet
}
}else{
//when limit is not exceeded do this
if(strlen($store)!=4){ //check the length of our regCode (should be 4 letters in this case if not add some zeros)
$storage_id = str_pad($storage_id,4,'0',STR_PAD_LEFT);
}
}
$regCode = $prefix.$store; //combine alphabet to new storage_id
echo $regCode;
就是這樣!
這是完整的代碼。只需複製這個:
<?php
mysql_connect('localhost','root','');
mysql_select_db('test');
if(isset($_REQUEST['submit']))
{
$query = mysql_query('SELECT regno from details ORDER BY regno DESC LIMIT 1');
$results = mysql_num_rows($query);
if($results==0){
$regCode = 'A0001';
mysql_query("INSERT INTO details (regno) VALUES('{$regCode}')");
}else{
list($regCode) = mysql_fetch_row($query);
$alphabet = array('B','C','D','E');
$prefix = substr($regCode,0,1); //get prefix/alphabet
$storage_id = (int) substr($regCode,1,4) +1;
$limit = 1000;
if($storage_id>$limit){
$storage_id= '0001'; //if limit exceeded reset storage id
$index = array_search($prefix,$alphabet); //get alphabet/prefix index in the $alphabet array
if($index>=0){
$prefix = $alphabet[$index+1]; //Get next alphabet
}
}else{
//when limit is not exceeded do this
if(strlen($storage_id)!=4){ //check the length of our regCode (should be 4 letters in this case if not add some zeros)
$storage_id = str_pad($storage_id,4,'0',STR_PAD_LEFT);
}
}
$regCode = $prefix.$storage_id; //combine alphabet to new storage_id
mysql_query("INSERT INTO details (regno) VALUES('{$regCode}')");
}
echo $regCode;
}
?>
<form name="form" id="form" method="post">
<input type="submit" name="submit">
</form>
你至少應該嘗試寫一些代碼,看看你能想出什麼,但是這是可能的。 – andyroo 2014-09-29 10:41:53
是的,它可能:) – Josnidhin 2014-09-29 10:41:59
你可以請縮小這個問題嗎?它太寬泛了,目前顯示你沒有努力。 – Fluffeh 2014-09-29 10:42:12