使用php,jquery基于条件自动生成字符串变量

时间:2015-07-04 07:32:53

标签: php jquery

我正在研究学生用户注册系统,为新学生分配一个 注册号码取决于他的部门。

以下学生正在学习机械工程(每行一名学生证):

BSME/010J/2012
BSME/011J/2012
BSME/012J/2012

只有中间区域发生变化(010,011,012,013 ......)。最后一部分是今年。

每个部门都有一种独特的注册号码方式。 有没有办法PHP代码可以生成这些注册号码 一个值选择选项?以下是我的表格:

<form method="post" action="" id="myForm"
    <select>
    <option>Mechanical</option>
    <option>Bcom</option>
    <button id="submit">Generate</button>
</form>

1 个答案:

答案 0 :(得分:0)

假设一个字符串总是以相同的序列开始或结束(在这个例子中:j)并且有一个递增的数字,那么你可以请求一个自定义序列,其中一个单独的序列可以区分不同类型的学生这种情况(例如:F为新生,SM为大二,J为Junior,SR为高级,C为继续学生)。只要您有一个表示整数的单独变量,就可以增加该数字并连接字符串以生成最终的唯一字符串:

<强> HTML

<!-- Form -->
<form action="process.php" method="GET" \>
<!-- Select Department -->
<select name="department">
<option value="BSME">What Ever this stands for</option>
<option value="nextD">Next Department</option>
<option value="anotherD">Another Department</option>
</select>

<!-- Enter Year (Not sure how this is determine, assuming input) -->
<input type="text" name="year" />

<!-- Apply Custom Sequence -->
<input type="text" name="customSequence" /> 

<!-- SUBMITION - Call Processing -->
<button type="submit" value="Generate" />
</form>

** PHP - process.php **

// Initialize Values
$departmentFromSelect = $_GET['department'];
$yearFromForm = $_GET['year'];
$valueFromSequenceInput = $_GET['customSequence'];
// INTEGER TO INCREMENT WOULD HAVE TO BE STATIC OR GLOBAL
public static $integerToIncrement = 0;

// Call the function passing to generate next reg number
function generateRegistrationNumber() {
    // Increment integer for next value
    $integerToIncrement++;
    // Concatenate String
    $finalRegNum = $departmentFromSelect . "/" . $integerToIncrement . $valueFromSequenceInput . "/" . $year;
    return $finalRegNum;
}
相关问题