function add_code($field_id, $description, $code = '') { // if no code is passed, use first 3 letters of $name for code if (trim($description) == '') { return false; } // cannot create a code without a description if ($code == '') { $str = $description; $i = 0; // get first three letters preg_match('/^[a-z0-9]{3}/iD', $str, $m[0]); $code = strtoupper($m[0][0]); // validate the code $sql = "SELECT * from codes where field_id='" . jb_escape_sql($field_id) . "' AND code like '%" . jb_escape_sql($code) . "%' LIMIT 1 "; $result = JB_mysql_query($sql) or die(mysql_error()); while (mysql_num_rows($result) == true) { $i++; if ($i > 3) { return false; // too many attempts } // add a random char to the string $str = $str . chr(rand(ord('A'), ord('Z'))); $sql = "SELECT * from codes where field_id='" . jb_escape_sql($field_id) . "' AND code like '%" . jb_escape_sql($code) . "%' "; $result = JB_mysql_query($sql) or die(mysql_error()); preg_match('/^[a-z0-9]{3}/iD', $str, $m[0]); $code = strtoupper($m[0][0]); $count = mysql_num_rows($result); } } JB_insert_code($field_id, $code, $description); return $code; }
echo $line[0] . ", " . $line[1] . "<br>"; $error = validate_code($field_id, trim($line[0]), trim($line[1])); if ($error == '') { JB_insert_code($field_id, trim($line[0]), trim($line[1])); } else { $JBMarkup->error_msg('<b>ERROR!</b> Cannot save ' . $line[0] . ' code because:'); echo $error; } $error = ''; } $JBMarkup->ok_msg('Bulk import complted'); } if ($_REQUEST['new_code'] != '') { $error = validate_code($field_id, $_REQUEST['new_code'], $_REQUEST['new_description']); if ($error == '') { JB_insert_code($field_id, trim($_REQUEST['new_code']), trim($_REQUEST['new_description'])); $_REQUEST['new_code'] = ''; $_REQUEST['new_description'] = ''; $_REQUEST['code'] = ''; $_REQUEST['action'] = ''; $JBMarkup->ok_msg('Changes Saved'); ?> <?php } else { $JBMarkup->error_msg('<b>ERROR!</b> Cannot save new code because:'); echo $error; } } if ($_REQUEST['bulk']) {