您最多能選擇 25 個主題
主題必須以字母或數字為開頭,可包含連接號「-」且最長為 35 個字元。
51 行
1.1 KiB
PHP
51 行
1.1 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . "/required.php";
|
|
|
|
$question = $VARS['question'];
|
|
$answers = $VARS['answers'];
|
|
|
|
if (trim($question) == "" || trim($answers) == "") {
|
|
header("Location: ./create/error");
|
|
}
|
|
|
|
/**
|
|
* Generate question access codes.
|
|
* https://stackoverflow.com/a/4356295
|
|
* @param int $length
|
|
* @return string
|
|
*/
|
|
function generateRandomString($length = 4) {
|
|
$characters = 'ABCEFGHJKLMNPQRSTWXYZ';
|
|
$charactersLength = strlen($characters);
|
|
$randomString = '';
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
|
}
|
|
return $randomString;
|
|
}
|
|
|
|
$length = 4;
|
|
$code = generateRandomString($length);
|
|
$tries = 0;
|
|
while ($database->has("questions", ['qcode' => $code])) {
|
|
if ($tries > 10) {
|
|
$length++;
|
|
}
|
|
$code = generateRandomString($length);
|
|
$tries++;
|
|
}
|
|
|
|
$database->insert("questions", ['qtext' => $question, 'qcode' => $code]);
|
|
|
|
$qid = $database->id();
|
|
|
|
$answers = explode("\n", $answers);
|
|
|
|
foreach ($answers as $ans) {
|
|
if (trim($ans) != "") {
|
|
$database->insert("answers", ['atext' => trim($ans), 'qid' => $qid]);
|
|
}
|
|
}
|
|
|
|
header("Location: ./q/$code"); |