public function save($POST)
{
$db = new DB();
$ref = "TF" . date("dmyhis");
// for($i = 0; $i < count($POST); $i++){
// if(count($POST[$i]) > 1)
// $type = "radio";
// else
// $type = "text";
// $sql = "INSERT INTO forms (question, ref, user_id, type) VALUES ('". $POST[$i][0] ."','". $ref ."', '1','". $type ."')";
// $last_id = $db->insert($sql);
// for($j = 0; $j < count($POST[$i]); $j++){
// $sql = "INSERT INTO choices (form_id, choice) VALUES ('". $last_id . "', '". $POST[$i][$j] . "')";
// $db->insert($sql);
// }
// }
$sql = "INSERT INTO topics (topic, create_dt) VALUES ('" . $POST["topic"] . "', now())";
$last_topic_id = $db->insert($sql);
for ($i = 0; $i < count($POST["data"]); $i++) {
$last_from_id = 0;
if (count($POST["data"][$i]) > 1) {
$sql = "INSERT INTO froms (question) VALUES ('" . $POST["data"][$i][0] . "')";
$last_from_id = $db->insert($sql);
}
$start = $last_from_id == 0 ? 0 : 1;
for ($j = $start; $j < count($POST["data"][$i]); $j++) {
$sql = "INSERT INTO questions (question, from_id, topic_id) VALUES ('" . $POST["data"][$i][$j] . "', '" . $last_from_id . "', '" . $last_topic_id . "')";
$db->insert($sql);
}
}
$response["status"] = 0;
$response["msg"] = "Success";
echo json_encode($response);
}