最近我们有一个项目,因为我没有做得很好。我知道它不会帮助我目前的成绩,但我认为从长远来看,理解这些概念对我来说非常有益。任务是创建一个电影调查。
这是我目前为我的formprocessor.php
所拥有的<body>
<H1><u>The Vermont Web Designers Movie Survey</u></h1>
<?php
//I want to hold the users information that they entered, so they know that this is their survey
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
print "<p>$firstName $lastName's Movie Review - Spring 2018</p><hr>";
//I have values from 1 to 5 (ratings), I want to create a variable for each of them
$great = 0;
$good = 0;
$ok = 0;
$soso= 0;
$terrible = 0;
//I create an array to hold my selections. One for the movie ratings, another for the actor ratings.
$movieChoice = array($_POST['movie1'], $_POST['movie2'], $_POST['movie3']);
$actorChoice = array($_POST['actors1'], $_POST['actors2'], $_POST['actors3']);
//I use a loop to collect my movie choices.
for($i = 0; $i < 3; $i++){
if($movieChoice[$i] == 1){
//I increase the variable by 1 if it was selected.
$great++;
}
else if($movieChoice[$i] == 2){
$good++;
}
else if($movieChoice[$i] == 3){
$ok++;
}
else if($movieChoice[$i] == 4){
$soso++;
}
else{
$terrible++;
}
}
//I use another loop to hold my actor selections.
for($i = 0; $i < 3; $i++){
if($actorChoice[$i] == 1){
$great++;
}
else if($actorChoice[$i] == 2){
$good++;
}
else if($actorChoice[$i] == 3){
$ok++;
}
else if($actorChoice[$i] == 4){
$soso++;
}
else{
$terrible++;
}
}
print ('<table align= "center">
<tr><th colspan= "3">Movie</th>
<th>Question</th>
<th>Great</th>
<th>Good</th>
<th>Ok</th>
<th>So-So</th>
<th>Terrible</th>
</tr>
<tr><td colspan= "3">1: The Godfather Part 1</td>
<td>Quality of Movie</td>
<td>'.$movieChoice[$great].'</td>
<td>'.$movieChoice[$good].'</td>
<td>'.$movieChoice[$ok].' </td>
<td>'.$movieChoice[$soso].'</td>
<td>'.$movieChoice[$terrible].'</td>
</tr>
<tr><td colspan= "3"> </td>
<td>Quality of Actors</td>)
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr><td colspan= "3">2: Men of Honor</td>
<td>Quality of Movie</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr><td colspan= "3"> </td>
<td>Quality of Actors</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr><td colspan= "3">3: Three Days of the Condor</td>
<td>Quality of Movie</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr><td colspan= "3"> </td>
<td>Quality of Actors</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>');
&GT?;
这是我的表单
<H1><u>The Vermont Web Designers Movie Survey</u></h1>
First Name: <input type= "text" size= "10" maxlength= "25" name= "firstName">
Last Name: <input type= "text" size= "10" maxlength= "25" name= "lastName"><br />
<hr>
<p>Please use a scale of 1 (first-rate; great; awesome) through 5 (really terrible) to answer each of these questions</p><br />
<hr>
<table align= "center">
<tr><th colspan= "3">Movie </th>
<th>Question</th>
<th>Great</th>
<th>Good</th>
<th>Ok</th>
<th>So-So</th>
<th>Terrible</th>
</tr>
<tr><td colspan= "3">1: The Godfather Part 1</td>
<td>Quality of Movie</td>
<td><input type= "radio" name= "movie1" value="1">1</td>
<td><input type= "radio" name= "movie1" value="2">2</td>
<td><input type= "radio" name= "movie1" value="3">3</td>
<td><input type= "radio" name= "movie1" value="4">4</td>
<td><input type= "radio" name= "movie1" value="5">5</td>
</tr>
<tr><td colspan= "3"> </td>
<td>Quality of Actors</td>
<td><input type= "radio" name= "actors1" value="1">1</td>
<td><input type= "radio" name= "actors1" value="2">2</td>
<td><input type= "radio" name= "actors1" value="3">3</td>
<td><input type= "radio" name= "actors1" value="4">4</td>
<td><input type= "radio" name= "actors1" value="5">5</td>
</tr>
<tr><td colspan= "3">2: Men of Honor</td>
<td>Quality of Movie</td>
<td><input type= "radio" name= "movie2" value="1">1</td>
<td><input type= "radio" name= "movie2" value="2">2</td>
<td><input type= "radio" name= "movie2" value="3">3</td>
<td><input type= "radio" name= "movie2" value="4">4</td>
<td><input type= "radio" name= "movie2" value="5">5</td>
</tr>
<tr><td colspan= "3"> </td>
<td>Quality of Actors</td>
<td><input type= "radio" name= "actors2" value="1">1</td>
<td><input type= "radio" name= "actors2" value="2">2</td>
<td><input type= "radio" name= "actors2" value="3">3</td>
<td><input type= "radio" name= "actors2" value="4">4</td>
<td><input type= "radio" name= "actors2" value="5">5</td>
</tr>
<tr><td colspan= "3">3: Three Days of the Condor</td>
<td>Quality of Movie</td>
<td><input type= "radio" name= "movie3" value="1">1</td>
<td><input type= "radio" name= "movie3" value="2">2</td>
<td><input type= "radio" name= "movie3" value="3">3</td>
<td><input type= "radio" name= "movie3" value="4">4</td>
<td><input type= "radio" name= "movie3" value="5">5</td>
</tr>
<tr><td colspan= "3"> </td>
<td>Quality of Actors</td>
<td><input type= "radio" name= "actors3" value="1">1</td>
<td><input type= "radio" name= "actors3" value="2">2</td>
<td><input type= "radio" name= "actors3" value="3">3</td>
<td><input type= "radio" name= "actors3" value="4">4</td>
<td><input type= "radio" name= "actors3" value="5">5</td>
</tr>
</table>
<hr>
<input type= "submit" value= "submit">
</form>
我遇到麻烦的真正问题是填充我的电影选择和演员选择,然后为用户打印数据。
答案 0 :(得分:0)
数据库设置:
电影表 字段:movie_id,movie_title
movie_ratings表 字段:movie_ratings_id,movie_id,user_id,rating
actor_ratings表 字段:actor_ratings_id,movie_id,user_id,rating
这样,您可以单独存储每个单独的评级,并在您的sql语句中执行COUNT和GROUP BY。由于您正在进行基本的DB操作,因此可能会有点激烈。您可以随时执行SELECT *,然后根据以下内容增加您的电影评级。
但这不是回答问题的范围,如果您想留言/聊聊我,我可以帮助您。
以下的原始答案:
首先从数据库中获取结果
$sql = "SELECT * FROM ratings";
// fetch results
现在您已经获得了结果,您可以正确地构建它 我会做这样的事情。我选择这样做的原因是因为你有一个带有命名键的关联数组,你以后可以在foreach循环或类似的东西中调用它们。
$movieChoices = array(
array('title'=>"The Godfather Part 1",
'movieRatings' => array(
"great" => 0,
"good" => 0,
"ok" => 0,
"soso"=> 0,
"terrible" => 0,
),
'actorRatings' => array(
"great" => 0,
"good" => 0,
"ok" => 0,
"soso"=> 0,
"terrible" => 0,
)
),
array('title'=> "Men Of Honor",
'movieRatings' => array(
"great" => 0,
"good" => 0,
"ok" => 0,
"soso"=> 0,
"terrible" => 0,
),
'actorRatings' => array(
"great" => 0,
"good" => 0,
"ok" => 0,
"soso"=> 0,
"terrible" => 0,
)
),
array('title'=>"Three Days of Condor",
'movieRatings' => array(
"great" => 0,
"good" => 0,
"ok" => 0,
"soso"=> 0,
"terrible" => 0,
),
'actorRatings' => array(
"great" => 0,
"good" => 0,
"ok" => 0,
"soso"=> 0,
"terrible" => 0,
)
)
);
这会给你这样的东西:
Array
(
[0] => Array
(
[title] => The Godfather Part 1
[movieRatings] => Array
(
[great] => 0
[good] => 0
[ok] => 0
[soso] => 0
[terrible] => 0
)
[actorRatings] => Array
(
[great] => 0
[good] => 0
[ok] => 0
[soso] => 0
[terrible] => 0
)
)
[1] => Array
(
[title] => Men Of Honor
[movieRatings] => Array
(
[great] => 0
[good] => 0
[ok] => 0
[soso] => 0
[terrible] => 0
)
[actorRatings] => Array
(
[great] => 0
[good] => 0
[ok] => 0
[soso] => 0
[terrible] => 0
)
)
[2] => Array
(
[title] => Three Days of Condor
[movieRatings] => Array
(
[great] => 0
[good] => 0
[ok] => 0
[soso] => 0
[terrible] => 0
)
[actorRatings] => Array
(
[great] => 0
[good] => 0
[ok] => 0
[soso] => 0
[terrible] => 0
)
)
)
以相同的方式存储您的帖子变量,以便于代码使用。
$movieChoice = array($_POST['movie1'], $_POST['movie2'], $_POST['movie3']);
$actorChoice = array($_POST['actors1'], $_POST['actors2'], $_POST['actors3']);
使用循环存储两个项目。 两次循环同样的事情是没有意义的。重新使用代码。 我使用+ = 1,因为你对变量做了什么更明显。 它和++一样,只是我的偏好。
此外,当你在这里时,你可以编写一个SQL语句来更新数据库中的行。
$sql = "UPDATE movie_ratings SET RATING = RATING + 1 WHERE movie_id = MOVIE_ID";
但是,那是为了以后的事情。因此,请继续更新您已有的当前值。
for ($i = 0; $i < 3; $i++) {
if ($movieChoice[$i] == 1) {
//I increase the variable by 1 if it was selected.
$movieChoices[$i]['movieRatings']["great"] += 1;
} else if ($movieChoice[$i] == 2) {
$movieChoices[$i]['movieRatings']['good'] += 1;
} else if ($movieChoice[$i] == 3) {
$movieChoices[$i]['movieRatings']['ok'] += 1;
} else if ($movieChoice[$i] == 4) {
$movieChoices[$i]['movieRatings']['soso'] += 1;
} else {
$movieChoices[$i]['movieRatings']['terrible'] += 1;
}
if ($actorChoice[$i] == 1) {
//I increase the variable by 1 if it was selected.
$movieChoices[$i]['actorRatings']["great"] += 1;
} else if ($actorChoice[$i] == 2) {
$movieChoices[$i]['actorRatings']['good'] += 1;
} else if ($actorChoice[$i] == 3) {
$movieChoices[$i]['actorRatings']['ok'] += 1;
} else if ($actorChoice[$i] == 4) {
$movieChoices[$i]['actorRatings']['soso'] += 1;
} else {
$movieChoices[$i]['actorRatings']['terrible'] += 1;
}
}
现在,当您想要打印结果时,它更容易实现。您可以使用foreach()
循环浏览每个电影结果,并打印出各个值。比你正在做的更容易,代码更少。
print '
<table align="center">
<tr>
<th colspan="3">Movie</th>
<th>Question</th>
<th>Great</th>
<th>Good</th>
<th>Ok</th>
<th>So-So</th>
<th>Terrible</th>
</tr>';
foreach($movieChoices as $choice) {
print '
<tr>
<td colspan="3">'.$choice['title'].'</td>
<td>Quality of Movie</td>
<td>'.$choice['movieRatings']['great'].'</td>
<td>'.$choice['movieRatings']['good'].'</td>
<td>'.$choice['movieRatings']['ok'].' </td>
<td>'.$choice['movieRatings']['soso'].'</td>
<td>'.$choice['movieRatings']['terrible'].'</td>
</tr>
<tr>
<td colspan="3"> </td>
<td>Quality of Actors</td>
<td>'.$choice['actorRatings']['great'].'</td>
<td>'.$choice['actorRatings']['good'].'</td>
<td>'.$choice['actorRatings']['ok'].' </td>
<td>'.$choice['actorRatings']['soso'].'</td>
<td>'.$choice['actorRatings']['terrible'].'</td>
</tr>
';
}
print '</table>';
老实说,对于这段代码来说,这是一个非常长的结果。如果我有更多的工作,我可以把它分解为更简单的你。但是,这将有助于您尝试做的事情。
答案 1 :(得分:0)
我有空闲时间,决定玩弄你的想法。您应该能够将其复制/粘贴到单个文件中,它应该可以工作。
我不打算将我的答案作为最终解决方案,但我希望它能为您提供一些关于构建程序的新想法。随意挑选您想要的东西。
这里的主要思想是将用户的选择存储到会话中,以便我们可以增加它并在最后一个表中显示运行总计。我试图记录代码,但请随时提问。
我还尝试使其相对动态,因此您可以添加电影,问题,选择以及表单和表单处理器应该处理它,只要您保持数据结构相同即可。存储电影,选项和问题的数组也使用关联键(诸如&#39; 1&#39;,&#39; 2&#39;等字符串),这些字符串略微模仿关系数据库将使用的内容。向这些数组添加/删除元素以查看表单的更改方式。
最后,有一些语言结构(??,square arrays [])需要运行PHP 7。还有一些代码行......丑陋。我没有尝试找到一种更好的方法来编码它们。
防爆。 java.time
。
如果您遇到问题,请告诉我。
list($movieId, $questionId) = array_pad(explode('_', $key, 2), 2, null);