Re: Generate serialize number
Here is some code I used to generate card numbers for an app… perhaps it may help.
Regards,
Scott
$idBatch = date('YmdHis');
$nowStr = date('Y-m-d');
$DefaultLevel = 0; // not programmed
$DefaultActivityType = 1; // in stock
$Building = {input_building};
$StartCard = {input_startcard};
$CardCount = {input_count};
$Comment = {input_comment};
$CurrentUserID = $_SESSION['g_idUser'];
if (is_numeric($StartCard)) {
if ($StartCard > 0) {
$_duplicates_found = 0;
// check database for no duplicates, cancel batch if any present
for ($newCardno = $StartCard; $newCardno < ($StartCard+$CardCount); $newCardno += 1) {
$padCardno = str_pad($newCardno, 5, '0', STR_PAD_LEFT);
$sql = 'SELECT COUNT(*) FROM AccessCards WHERE cardno = "'.$padCardno.'"';
sc_lookup(db_cards,$sql);
if ({db_cards[0][0]} > 0) {
$_duplicates_found++;
}
}
if ($_duplicates_found > 0) {
sc_error_message('There were '.$_duplicates_found.' duplicate cards found, please verify batch entry');
} else {
// add to database, need update VALUES code to insert once instead of 1 by 1 as SC5 does not allow ';' to script multiple sql statements.
for ($newCardno = $StartCard; $newCardno < ($StartCard+$CardCount); $newCardno += 1) {
$padCardno = str_pad($newCardno, 5, '0', STR_PAD_LEFT);
$sql = 'INSERT INTO `AccessCards` ';
$sql .= '(idBatch,cardno,idLevel,dateOrdered,dateReceived,dateUpdated,idActivityType,idBuilding,idUser)';
$sql .= 'VALUES';
$sql .= '("'.$idBatch.'","'.$padCardno.'",'.$DefaultLevel.',"'.$nowStr.'","'.$nowStr.'","'.$nowStr.'",'.$DefaultActivityType.','.$Building.','.$CurrentUserID.')';
sc_exec_sql($sql); // or die("Invalid Query: ".$sql);
}
?>
<script language="JavaScript">
alert("Cards have been generated, click OK to continue");
</script>
<?php
sc_redir(grid_accesscards.php);
}
}
}