最近,我们有一个项目由于我做得不好。我知道这不会对我目前的成绩有所帮助,但是从长远来看,我认为理解这些概念对我来说是极为有益的。任务是创建电影调查。

这是到目前为止我的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].'&nbsp;</td>
    <td>'.$movieChoice[$soso].'</td>
    <td>'.$movieChoice[$terrible].'</td>
    </tr>

    <tr><td  colspan= "3">&nbsp;</td>
    <td>Quality of Actors</td>)
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>

    <tr><td colspan= "3">2: Men of Honor</td>
    <td>Quality of Movie</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>

    <tr><td  colspan= "3">&nbsp;</td>
    <td>Quality of Actors</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>

    <tr><td colspan= "3">3: Three Days of the Condor</td>
    <td>Quality of Movie</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>

    <tr><td  colspan= "3">&nbsp;</td>
    <td>Quality of Actors</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
</table>');


?>


这是我的表格
    

<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">&nbsp;</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">&nbsp;</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">&nbsp;</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>


我遇到的真正问题是填写电影选择和演员选择的数组,然后为用户打印该数据。

This is what I get when I run my code:

This is what I should be getting

最佳答案

数据库设置:

电影桌
栏位:movie_id,movie_title

movie_ratings表
字段:movie_ratings_id,movie_id,user_id,评分

actor_ratings表
栏位:actor_ratings_id,movie_id,user_id,评分

这样,您可以分别存储每个单独的等级,并在sql语句上执行COUNT和GROUP BY。由于您正在执行基本的数据库操作,因此可能会有点紧张。您始终可以执行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
                )

        )

)


以易于代码使用的相同方式存储post变量。

$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'].'&nbsp;</td>
        <td>'.$choice['movieRatings']['soso'].'</td>
        <td>'.$choice['movieRatings']['terrible'].'</td>
    </tr>

    <tr>
        <td colspan="3">&nbsp;</td>
        <td>Quality of Actors</td>
        <td>'.$choice['actorRatings']['great'].'</td>
        <td>'.$choice['actorRatings']['good'].'</td>
        <td>'.$choice['actorRatings']['ok'].'&nbsp;</td>
        <td>'.$choice['actorRatings']['soso'].'</td>
        <td>'.$choice['actorRatings']['terrible'].'</td>
    </tr>
    ';
}
print '</table>';


老实说,这对于这段代码来说确实是一个漫长的结果。如果有更多需要合作的地方,我可以将其分解为更简单的方法。但是,这将有助于您尝试做的事情。

10-07 19:59
查看更多