Skip to content

Instantly share code, notes, and snippets.

@ngaranko
Created December 6, 2012 21:10
Show Gist options
  • Save ngaranko/4228441 to your computer and use it in GitHub Desktop.
Save ngaranko/4228441 to your computer and use it in GitHub Desktop.
<?php
// page.php
// Question model
class Question
{
private $id;
private $name;
}
// Option model
class Option
{
private $id;
private $question_id;
private $name;
private $next_question_id;
}
$question_id = (!empty($_GET['id'])) ? $_GET['id'] : 1; // Select first Question if it's first
// Process POST if any
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$option = "select * from options_table where id = " . $_POST['option'];
// Save temp result to session
if (empty($_SESSION['results']))
$_SESSION['results'] = array();
$_SESSION['results'][] = array($question_id, $option->id);
if (is_null($option->next_question_id)) {
// Save end result to db
"insert into results_table values("user_id", ". json_encode($_SESSION['results']
print 'the end';
die();
} else {
redirect('/page.php?id=' . $option->next_question_id);
}
}
// View
$question = "select * from questions_table where id = $question_id";
$options = sprintf("SELECT * FROM options_table WHERE question_id = %s", $question_id);
?>
<html>
<form action="." method="POST">
<h1><?php print $question->name; ?></h1>
<?php
foreach ($options as $option) {
?>
<input type="radio" name="option" value="<?php print $option->id; ?>" /> <?php print $option->name; ?>
<?php
}
?>
<input type="submit" value="GO" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment