1) 是的,您可以使用会话。您可以将每个评分存储在一个数组中。在输出任何内容之前,请确保在页面的第一行调用session_start()。
对于每个页面,您可以执行以下操作:
//start using sessions
session_start();
//if the session variable is empty, create a new array
$votesArray = empty($_SESSION['votesArray']) ? array() : $_SESSION['votesArray'];
//push the submitted vote value
array_push($votesArray, $_POST['voteValue']);
//store the vote array in the session
$_SESSION['votesArray'] = $votesArray;
然后在最后一页,你可以使用$_SESSION['votesArray'] 获取整个投票数组,然后用它做你想做的事。
2) 这有点复杂。就我个人而言,当我遇到这样的情况时,我会使用索引来构建我的表单。示例:
<form method="post" action="">
<?php
//for each car, generate the form
for($index=0;$index<$numberOfCars;$index++) {
?>
<input name="vote<?php echo $index;?>" type="hidden" value="5">
<?php
}
?>
<input type="submit">
<!-- stores the number of votes that will be submitted -->
<input name="numberOfVotes" type="hidden" value="<?php echo $numberOfCars; ?>">
</form>
这将为每辆车输出一个隐藏的输入,其中包含一个投票值。当表单被提交时,你将使用这个逻辑来获得每一个投票:
<?php
for($i=0;$i<$_POST['numberOfVotes'];$i++) {
$vote = $_POST['vote'.$i]; //this is the i'th car's vote
}
?>