我需要能够添加到我的$ questions数组中,但代码中没有动态,所以当我添加一些东西时,我必须为每个问题创建一个if语句和一个新的输入字段来存储数据(老师要求以隐藏式存储)。有谁知道我如何才能提高效率?
<?php
date_default_timezone_set('Europe/Oslo');
$questions = array(
array(
'text' => 'Whats your name?',
'type' => 'text',
'name' => 'svar1'
),
array(
'text' => 'Do you live in Oslo?',
'type' => 'checkbox',
'name' => 'checkbox'
),
array(
'text' => 'How old are you?',
'type' => 'text',
'name' => 'svar3'
),
array(
'text' => 'Do you have friends?',
'type' => 'text',
'name' => 'svar4'
),
array(
'text' => 'Do you like going to the movies?',
'type' => 'text',
'name' => 'svar5'
)
);
$step = isset($_POST['step']) ? $_POST['step'] : 0;
if (isset($_POST['prev'])) {
$step --;
}
if (isset($_POST['svar1'])) {
$step++;
}
echo "<p><a href='/webutvikling/oblig2.php?reset=true'>Start på nytt</a></p>\n";
?>
<!DOCTYPE html>
<html>
<head>
<title>Spørreundersøkelse</title>
<meta charset="utf-8">
</head>
<body>
<style type="text/css">
body {
font-family: Helvetica Neue, Arial;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
background-color: #E0E0EB;
}
.wrapper {
border: 1px solid black;
text-indent: 10px;
font-size: 1em;
background: RGBA(255, 255, 255, 0.8);
}
</style>
<h1>Spørreundersøkelse</h1>
<form target="oblig2.php" method="post" >
<input type="hidden" name="step" value="<?php echo $step ?>">
<?php if (isset($questions[$step])) : ?>
<p>Gjeldende steg: <?php echo $step +1 ?></p>
<h3><?php echo $questions[$step]['text'] ?></h3>
<input type="<?=$questions[$step]['type']?>" autofocus name="<?= $questions[$step]['name']?>">
<input type="submit" name="next" value="Send inn">
<?php endif; ?>
<br><br>
<?php if ($step > 0) : ?>
<input type="submit" name="prev" value="Forrige spørsmål">
<?php endif; ?>
<?php if($step > 0):?>
<input type="hidden" name="svar1" value="<?= isset($_POST['svar1']) ? $_POST['svar1'] : false ?>" >
<?php endif;?>
<?php if($step > 1):?>
<input type="hidden" name="checkbox" value="<?= isset($_POST['checkbox']) ? $_POST['checkbox'] : false ?>" >
<?php endif;?>
<?php if($step > 2):?>
<input type="hidden" name="svar3" value="<?= isset($_POST['svar3']) ? $_POST['svar3'] : false ?>" >
<?php endif;?>
<?php if($step > 3):?>
<input type="hidden" name="svar4" value="<?= isset($_POST['svar4']) ? $_POST['svar4'] : false ?>" >
<?php endif;?>
<?php if($step > 4):?>
<input type="hidden" name="svar5" value="<?= isset($_POST['svar5']) ? $_POST['svar5'] : false ?>" >
<?php endif;?>
</form>
<?= isset($_POST['svar1']) ? $_POST['svar1'] : false ?> <br /> <br />
<?= (isset($_POST['checkbox']) && $_POST['checkbox'] == 'on') ? 'Ja' : 'Nei'?> <br /> <br />
<?= isset($_POST['svar3']) ? $_POST['svar3'] : false ?> <br /> <br />
<?= isset($_POST['svar4']) ? $_POST['svar4'] : false ?> <br /> <br />
<?= isset($_POST['svar5']) ? $_POST['svar5'] : false ?> <br /> <br />
</body>
</html>