I'm writing a flatfile polling system. To add a poll, what I have is a form that asks how many options you want, and then displays a form for poll name, poll question, and then loops until it's got enough option input boxes.
Now, I add each option to an array like so:
$poll = array(
"question" => "$question",
"options" => array($one, $two, $three, $four, $five),
"votes" => array(0, 0, 0),
);
But how can I use the number of options I got in the form to know how many to put in the options sub-array? Because otherwise I'll have a bunch of empty (or just weird) options that don't exist, and I can't know how many someone wants (10, 15, 100, etc.)
So how can I grab the options from the form and put them in the array? Here's the whole code:
if(!isset($_GET['num'])) {
?>
<p><form method="get" action="index.php?id=new">
<input type="text" name="num" value="Number of Options" />
<input type="hidden" name="id" value="new" />
<input type="submit" value="Go to Step 2" />
</form></p>
<?php
}
else {
?>
<p><form method="post" action="index.php?id=new">
<input type="text" name="name" value="Poll Name" /><br />
<input type="text" name="question" value="Poll Question" /><br />
<?php
$n = $_GET['num'];
for($t=1; $t<=$n; $t++) {
?>
<input type="text" name="<?php echo $t; ?>" value="Option <?php echo $t; ?>" /><br />
<?php
}
?>
<input type="hidden" name="addpoll" /><br />
<input type="submit" value="Add Poll!" />
</form></p>
<?php
$question = $_POST['question'];
$name = $_POST['name'];
$options = str_replace( "\\", "", $options);
$poll = array(
"question" => "$question",
"options" => array($one, $two, $three, $four, $five),
"votes" => array(0, 0, 0),
);
$dat_file = "$name.dat";
$poll_data = serialize($poll);
$handle = fopen($dat_file,'w+');
fwrite($handle,$poll_data);
fclose($handle);
}
So I need a way to get the options, and put them into the array. . .