Cheers mate. I had a play around with these global variable things, and I have solved the problem!
Here is the final code which writes the text file required for the poll. Thanks for your help!
Ive even got it archiving existing polls! cheers again,
Ben
echo ("<h1>Poll Creator</h1>");
if (!isset($step)) {
$step = 1;
}
if ($step == 1) {
echo ("<form action='index.php' method='post'>\nEnter number of options: <input type='text' name='number' size='2'>\n<input type='hidden' name='step' value='2'>\n<br><br><input type='submit' value='Next...'>\n</form><br>");
}
if ($step == 2) {
echo ("<form action='index.php' method='post'>\n\nEnter question: <input type='text' name='question' size='50'><br><br>\n");
for ($i=0; $i<$number; $i++) {
$j = $i + 1;
echo ("Enter option <b>" . $j . "</b> <input type='text' name='" . $j . "' size='30'><br>\n");
}
echo ("<br><input type='checkbox' name='archive' value='yes' checked> <font size='1'>Check this box to archive the existing poll</font>\n<br><input type='hidden' name='number' value='" . $number . "'>\n<input type='hidden' name='step' value='3'>\n<br><input type='submit' value='Submit'>\n</form><br>");
}
if ($step == 3) {
if ($archive == yes) {
$directory = opendir('archive/');
$counter = -1; //accounts for current and parent directories ('.' and '..' respectively) AND adds one for new filename
while (false !== ($file = readdir($directory))) {
$counter++;
}
closedir($directory);
$archivename = ("archive/" . $counter . ".txt");
$olddata = file("poll.txt");
$olddata = implode("", $olddata);
$openfile = fopen($archivename, "w");
fwrite($openfile, $olddata);
fclose($openfile);
echo ("New poll added <b>&</b> old poll successfully archived.<br><br>");
} else {
echo ("New poll added.<br><br>");
}
$newpolldata[] = $question;
$number = $number + 1;
for ($i=1; $i<$number; $i++) {
$newpolldata[] = $_POST[$i];
$newpolldata[] = 0;
}
$newpolldata = implode("\n", $newpolldata);
$filename = fopen('poll.txt', 'w');
fwrite ($filename, $newpolldata);
fclose ($filename);
}