Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

51 lignes
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");